因經常要在伺服器上修改差不多的配置檔案,比如新搭建一臺web環境,透過映象啟動後,某些配置檔案需要修改,
需求:有超過10多個php專案的。env檔案需要把資料庫連結地址、使用者名稱更換為新的。
如果一兩個修改修的話透過vim很方便修改,但如果10多個甚至幾十個的配置修改。一個個改就不是程式設計師的風格了。那能不能不開啟檔案就直接修改呢。你能想到的90%都被人實現了。那就看看SED吧。下文是老外都總結好了的,我機器(翻譯)成了中文。但例子還是得自己來一邊的。這樣自己才能學會。let‘s go。
基礎知識要求:瞭解linux正則、管道符、cat命令
sed例項介紹
sed是GNU / Linux的有用的文字處理功能。sed的完整形式是Stream Editor。透過使用sed命令可以很容易地完成許多型別的簡單和複雜的文字處理任務。可以使用帶有sed命令的正則表示式來搜尋,替換和刪除文字或檔案中的任何特定字串。但是此命令會臨時執行所有型別的修改,並且預設情況下不會更改原始檔案內容。如果需要,使用者可以將修改後的內容儲存到另一個檔案中。sed命令的基本用法在本教程中透過使用50個獨特的示例進行了說明。
為了使大家方便閱讀對輸出進行縮排,
[root@zjt]$ cat os。txt Windows Linux Android OS
真正的輸出為
[root@zjt]$ cat os。txtWindowsLinuxAndroidOS
在開始本教程之前,您必須透過執行以下命令來檢查作業系統中sed的安裝版本。本教程基於GNU sed設計。因此,需要使用此版本的sed來練習本教程中顯示的示例。
[root@zjt]$ sed ——versionsed (GNU sed) 4。2。2。。。。。
我機器上的是4。2。2
sed [options]… [script] [file]sed [選項] … [指令碼] [ 檔案]
如果sed命令沒有提供檔名,那麼該指令碼將在標準輸入資料上執行。sed指令碼無需任何選擇即可執行。
示例列表
1、使用“ sed”進行基本文字替換
[root@zjt]$ echo “php是最好的語言” | sed ’s/php/Python/‘Python是最好的語言
sed命令也可以用來替換檔案內容的任何部分。建立一個名為
weekday.txt
的文字檔案,其內容如下。
weekday.txt
MondayTuesdayWednesdayThursdayFridaySaturdaySunday
cat weekday。txt[root@zjt]$ sed ’s/Sunday/Sunday is holiday/‘ weekday。txt
輸出:
執行上述`sed`命令後,“ dayday”存在於weekday。txt檔案中,該詞被文字“ Sunday is holiday”替換。但檔案內容並沒有真正的變化
2、使用“ g”選項替換檔案特定行中所有文字例項
’sed‘命令中使用’g‘選項來替換所有匹配模式。使用以下內容建立一個名為
python.txt
的文字檔案,以瞭解’g‘選項的用法。該檔案包含多個
“ Python”
。
為了方便學習,我把原來的python。txt的內容修改為下面的了。不用介意語法。
python.txt
Python is a very popular language。 Python is easy extended。Python is easy to use。 Python is easy to learn。 Python is usefull。Python is a cross-platform language。
只替換第一次出現 的位置
php is a very popular language。 Python is easy extended。php is easy to use。 Python is easy to learn。 Python is usefull。php is a cross-platform language。
在上子命令g
Python is a very popular language。 Python is easy extended。Python is easy to use。 Python is easy to learn。 Python is usefull。Python is a cross-platform language。 [root@zjt-baidu-server sed]# sed ’s/Python/Php/g‘ python。txtPhp is a very popular language。 Php is easy extended。Php is easy to use。 Php is easy to learn。 Php is usefull。Php is a cross-platform language。
以下命令將替換檔案
python.txt
第二行中所有出現的“
Python
” 。在這裡,
“ Python”
在第二行中出現了三次次。
輸出:
[root@zjt]$ cat python。txt Python is a very popular language。 Python is easy extended。 Python is easy to use。 Python is easy to learn。 Python is usefull。 Python is a cross-platform language。 [root@zjt]$ sed ’2 s/Python/perl/g‘ python。txt Python is a very popular language。 Python is easy extended。 perl is easy to use。 perl is easy to learn。 perl is usefull。 Python is a cross-platform language。
3、僅替換第二個以後的匹配項
如果某個單詞在檔案中多次出現,則可以透過使用帶有出現編號的“ sed”命令來替換每行中該單詞的特定出現。下面的“ sed”命令將替換檔案
python.txt
中每一行中第二次以後出現的搜尋模式。
為了更清楚的演示功能,我把python。txt修改為以下內容
替換第二個以後的
替換第二個以後的Python is a very popular language Python。Python is easy to use。 Python is easy to learn。 Python is usefull。Python is a cross-platform language[root@zjt-baidu-server sed]# cat python。txt Python is a very popular language Python。Python is easy to use。 Python is easy to learn。 Python is usefull。Python is a cross-platform language替換第二個以後的[root@zjt-baidu-server sed]# sed ’s/Python/php/g2‘ python。txt Python is a very popular language php。Python is easy to use。 php is easy to learn。 php is usefull。Python is a cross-platform language
替換第三個以後的
[root@zjt]$ cat python。txt Python is a very popular language。 Python is easy extended。 Python is easy to use。 Python is easy to learn。 Python is usefull。 Python is a cross-platform language。 [root@zjt]$ sed ’s/Python/php/g3‘ python。txt Python is a very popular language。 Python is easy extended。 Python is easy to use。 Python is easy to learn。 php is usefull。 Python is a cross-platform language。
4、僅替換最後一行匹配項,當然如果這行只有一個,也會被替換掉
$ sed ’s/\(。*\)Python/\1Php/‘ python。txt Python is a very popular language。 Php is easy extended。 Python is easy to use。 Python is easy to learn。 Php is usefull。 Php is a cross-platform language。
5、僅替換第一齣現的匹配項
sed ’s/\(。*\)Python/\1Php/‘ python。txt
6、匹配最後行出現的匹配項,也就是匹配最後一行的,第一個匹配項.
“ $”
符號用於匹配模式的最後一行
[root@zjt]$ cat python。txt Python is a very popular language。 Python is easy extended。 Python is easy to use。 Python is easy to learn。 Python is usefull。 Python is a cross-platform language。 [root@zjt]$ sed ’$ s/Python/php/‘ python。txt Python is a very popular language。 Python is easy extended。 Python is easy to use。 Python is easy to learn。 Python is usefull。 php is a cross-platform language。當我添加了一個Python以後並沒有被替換掉[root@zjt]$ cat python。txt Python is a very popular language。 Python is easy extended。 Python is easy to use。 Python is easy to learn。 Python is usefull。 Python is a cross-platform language。 Python[root@zjt]$ sed ’$ s/Python/php/‘ python。txt Python is a very popular language。 Python is easy extended。 Python is easy to use。 Python is easy to learn。 Python is usefull。 php is a cross-platform language。 Python
7、
反斜槓來轉義路徑,其實正則分隔符可以指定,看著就沒有那麼鬧心了,比如用;
[root@zjt]$ echo /home/ubuntu/code/perl/add。pl | sed ’s/\//——-/g‘ ——-home——-ubuntu——-code——-perl——-add。pl[root@zjt]$ echo /home/ubuntu/code/perl/add。pl | sed ’s;/;——-;g‘ ——-home——-ubuntu——-code——-perl——-add。pl
8、將檔案絕對路徑更換為無目錄,只剩下檔名
[root@zjt]$ echo “/home/ubuntu/temp/myfile。txt” | sed ’s/。*\///‘myfile。txt
9、替換中的判斷,當某些檔案存在才替換
建立文字檔案
dept.txt
在包含文字
“ CSE
” 的行中把Count替換為
100,
包含“
EEE”
的行中替換為
70
CSE - CountEEE - CountCivil - Countbogon:yy-api zhangjuntao$ sed -e ’/CSE/ s/Count/100/; /EEE/ s/Count/70/;‘ dept。txtCSE - 100EEE - 70Civil - Count
10、判斷不存在的時候替換
不包含文字“ CSE”的行把“
Count
”替換為80。
[root@zjt]$ cat dept。txt CSE - Count EEE - Count Civil - Count[root@zjt]$ sed -e’/CSE/! s/Count/80/;‘ dept。txt CSE - Count EEE - 80 Civil - 80
11、在匹配模式之前和之後使用'\ 1'新增字串
[root@zjt]$ echo “Bash language” | sed ’s/\(Bash\)/Learn \1 programming/‘ Learn Bash programming language
12、刪除匹配的行
d選項可以刪除匹配的行
[root@zjt]$ cat os。txt Windows Linux Android OS[root@zjt]$ sed ’/OS/d‘ os。txt Windows Linux Android
13、刪除匹配行和匹配行之後的2行
[root@zjt]$ cat os。txt Windows Linux Android OS[root@zjt]$ sed ’/Linux/,+2d‘ os。txt Windows
14、刪除文字行末尾的所有空格
[root@zjt]$ cat -E os。txt Windows$ Linux 1$ Android $ OS$[root@zjt]$ sed -i ’s/[[:blank:]]*$//‘ os。txt[root@zjt]$ cat -E os。txt Windows$ Linux 1$ Android$ OS$
15、刪除該行兩次匹配的所有行
“ PHP”文字在檔案
input.txt
第二行
中
包含兩次。在此示例中,使用了兩個`sed’命令來兩次刪除包含模式‘
php
’的那些行。第一個“ sed”命令將每行中第二個出現的“ php”替換為“
dl
”,並作為輸入傳送到第二個“ sed”命令中。第二條“ sed”命令將刪除包含文字“
dl
”的行。
[root@zjt]$ cat input。txt PHP is a server-side scripting language。 PHP is an open-source language and PHP is case-sensitive。 PHP is platform-independent。[root@zjt]$ sed ‘s/php/dl/i2;t’ input。txt | sed ‘/dl/d’ PHP is a server-side scripting language。 PHP is platform-independent。
16、刪除所有隻有空格的行
[root@zjt-baidu-server sed]# vim os。txt 1 Windows$ 2 ^I $ 3 Linux$ 4 ^I $ 5 Android$ 6 $ 7 OS$ 8 $~ sed ‘/^$/d’ input。txt
17、刪除所有不可列印的字元
透過將不可列印字元替換為空,可從任何文字中刪除不可列印字元。在此示例中,使用[:print:]類來查詢不可列印的字元。‘\ t’是不可列印的字元,不能由`echo`命令直接解析。為此,在‘echo’命令中使用的$tab變數中分配了‘\ t’字元。echo命令的輸出在sed命令中傳送,該命令將從輸出中刪除字元‘\ t’。
[root@zjt]$ tab=$‘\t’[root@zjt]$ echo Hello“$tab”World Hello World[root@zjt]$ echo Hello“$tab”World | sed ‘s/[^[:print:]]//g’ HelloWorld
18、向匹配行的行末新增內容
以下命令將在
os.txt
檔案中包含文字“ Windows”的行的末尾追加“10”
[root@zjt]$ cat os。txt Windows Linux Android OS[root@zjt]$ sed ‘/Windows/ s/$/ 10/’ os。txt Windows 10 Linux Android OS
19、在匹配行之前插入一行
[root@zjt]$ cat input。txt PHP is a server-side scripting language。 PHP is an open-source language and PHP is case-sensitive。 PHP is platform-independent。[root@zjt]$ sed ‘/PHP is platform-independent/ s/^/PHP 是世界上最好的語言。\n/’ input。txt PHP is a server-side scripting language。 PHP is an open-source language and PHP is case-sensitive。 PHP 是世界上最好的語言。 PHP is platform-independent
20、在匹配行之後插入一行
[root@zjt]$ cat os。txt Windows Linux Android OS[root@zjt]$ sed ‘s/Linux/&\nUbuntu/’ os。txt Windows Linux Ubuntu Android OS
21、向不匹配的行末尾新增內容
以下“ sed”命令將在
os.txt
中搜索不包含
“ Linux”
文字的行,並在每行末尾附加文字“
Operating System
”。在這裡,“
$
”符號用於標識將要附加新文字的行。
[root@zjt]$ cat os。txt Windows Linux Android OS[root@zjt]$ sed ‘/Linux/! s/$/ Operating System/’ os。txt Windows Operating System Linux Android Operating System OS Operating System
22、刪除不匹配的行
[root@zjt]$ cat web。txt HTML 5 JavaScript CSS PHP[root@zjt-baidu-server sed]# sed ‘/CSS/!d’ web。txt CSS
23、在匹配的單詞後面新增空格
在文字中尋找to單詞,如果找到
“&”
符號用於附加重複的文字。
to後面變成了兩個空格
[root@zjt]$ cat python。txt Python is a very popular language。 Python is easy extended。 Python is easy to use。 Python is easy to learn。 Python is usefull。 Python is a cross-platform language。 [root@zjt]$ sed -e ‘s/to /& to/g’ python。txt Python is a very popular language。 Python is easy extended。 Python is easy to touse。 Python is easy to tolearn。 Python is usefull。 Python is a cross-platform language。
24、用新字串替換一個字串列表
這個功能太有用了,以前都是寫程式陣列迴圈的。這個邏輯有點繞。
[root@zjt]$ cat list1。txt 1001 => 濤哥 1023 => 李四 1067 => 王五[root@zjt]$ cat list2。txt 1001 北大 平均分-3。63 1002 北大 平均分-3。24 1023 北大 平均分-3。11 1067 北大 平均分-3。84[root@zjt]$ sed `cat list1。txt | awk ‘{print “-e s/”$1“/”$3“/”}’`<<<“` cat list2。txt`” 濤哥 北大 平均分-3。63 1002 北大 平均分-3。24 李四 北大 平均分-3。11 王五 北大 平均分-3。84
25、用包含換行符的字串替換匹配的字串
其實就是用\n表示換行
[root@zjt]$ echo “Bash Perl Python Java PHP ASP”| sed ‘s/Python/Added Text\n/’Bash Perl Added Text Java PHP ASP[root@zjt-baidu-server sed]# echo “Bash Perl Python Java PHP ASP”| sed ‘s/Python/Added Text\n\n\n/’Bash Perl Added Text Java PHP ASP
26、從檔案中刪除換行符,並在每行末尾插入一個逗號
知識點和上一個一樣都是\n換行知識點。以前都是用編輯器替換的,以後不用開啟檔案直接搞定。
[root@zjt]$ cat os。txt Windows Linux Android OS[root@zjt]$ sed -z ‘s/\n/,/g’ os。txt Windows,Linux,Android,OS,
27、刪除逗號並新增換行符以將文字分成多行
和26執行相反的操作,把逗號替換為換行。
[root@zjt]$ echo “Kaniz Fatema,30th,batch” | sed “s/,/\n/g”
28、查詢不區分大小寫的匹配並刪除行
知識點字母I不區分大小寫
[root@zjt]$ cat os。txt Windows Linux Android OS[root@zjt]$ sed ‘/linux/Id’ os。txt Windows Android OS
29、查詢不區分大小寫的匹配項並替換為新文字
[root@zjt]$ echo “我喜歡bash程式設計” | sed ‘s/Bash/PHP/i’我喜歡PHP程式設計
30、查詢不區分大小寫的匹配項,並替換為相同文字的所有大寫字母
sed中使用
'\ U'
將任何文字轉換為全部大寫字母。
[root@zjt]$ cat os。txt Windows Linux Android OS[root@zjt]$ sed ‘s/\(linux\)/\U\1/Ig’ os。txt Windows LINUX Android OS
31、查詢不區分大小寫的匹配項,並替換為相同文字的所有小寫字母
sed中使用
'\ L'
將任何文字轉換為所有小寫字母。
[root@zjt]$ cat os。txt Windows Linux Android OS[root@zjt]$ sed ‘s/\(linux\)/\L\1/Ig’ os。txt Windows linux Android OS
32、用小寫字元替換文字的所有大寫字元
[root@zjt]$ echo ‘Windows Linux’ |sed ‘s/\(。*\)/\L\1/’
33、在行中搜索數字,並在數字之前附加貨幣符號
[root@zjt]$ cat items。txt 硬碟 1000 顯示器 800 滑鼠 100[root@zjt]$ sed ‘s/\([1-9]\)/¥\1/g’ items。txt 硬碟 ¥1000 顯示器 ¥800 滑鼠 ¥100
34、在數字上每超過3位後新增逗號
[root@z]$ echo “5098673” | sed -e :a -e ‘s/\(。*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta’5,098,673
35、用4個空格字元替換製表符
[root@zjt-baidu-server sed]#echo -e “1\t2\t3” | sed $‘s/\t/ /g’
36、用製表符替換連續的四個空格字元
# echo -e “1 2” | sed $‘s/ /\t/g’1 2
37、將所有行截斷為前10個字元
[root@zjt]$ cat in。txt PHP是伺服器端指令碼語言。 PHP是一種開源語言,PHP區分大小寫。PHP是與平臺無關的。 以下“ sed”命令會將in。txt檔案的每一行截斷為80個字元。[root@zjt]$ sed ‘s/\(^。\{1,10\}\)。*/\1/’ in。txt PHP是伺服器端指令碼 PHP是一種開源語言 以下“ sed”命令
38、搜尋字串正則表示式,並在其後附加一些標準文字
[root@zjt-baidu-server sed]#$ echo “hello, how are you?” | sed ‘s/\(hello\)/\1 John/’
39、搜尋字串正則表示式,並在每行的第二個匹配項之後附加一些文字
[root@zjt]$ cat input。txt PHP is a server-side scripting language。 PHP is an open-source language and PHP is case-sensitive。 PHP is platform-independent。[root@zjt]$ sed ‘s/\(PHP\)/\1 (我是新加的)/2’ input。txt PHP is a server-side scripting language。 PHP is an open-source language and PHP (我是新加的) is case-sensitive。 PHP is platform-independent。
40、透過指定檔案執行多個SED命令
知識點透過-f 指定命令檔案
[root@zjt]$ cat sedcmd s/PHP/ASP/ s/independent/dependent/[root@zjt]$ cat input。txt PHP is a server-side scripting language。 PHP is an open-source language and PHP is case-sensitive。 PHP is platform-independent。[root@zjt]$ sed -f sedcmd input。txt ASP is a server-side scripting language。 ASP is an open-source language and PHP is case-sensitive。 ASP is platform-dependent。
41、匹配多行模式並替換為新的多行文字
下面的“ sed”命令將搜尋多行文字
“ Linux \ nAndroid”
,如果模式匹配,則匹配行將被多行文字
“ Ubuntu \ nAndroid Lollipop
”
替換
。此處,P和D用於多行處理。
42、替換文字中與模式匹配的兩個單詞的順序
[root@zjt]$ echo “perl python” | sed -e ‘s/\([^ ]*\) *\([^ ]*\)/\2 \1/’
43、從命令列執行多個“ sed”命令
[root@zjt]$ echo “Ubuntu Centos Debian” | sed -e ‘s/Ubuntu/Kubuntu/; s/Centos/Fedora/’
44、將sed與其他命令結合
先透過cat命令讀取檔案,作為下個sed的輸入,然後執行完,再傳給下個sed
管道符的學習和應用
[root@zjt-baidu-server sed]# cat os。txt | sed ‘s/Linux/Fedora/’| sed ‘s/windows/Windows 10/i’Windows 10FedoraAndroidOS
45、在檔案中插入空行
‘G’選項用於在檔案中插入空行。
這個在我的linux伺服器上執行不成功,應該是版本低,4。2。2的在我的mac上成功了。
而且mac不支援sed ——version 算了不折騰他,畢竟都是在linux上使用呢。僅供參考
bogon:tmp zhangjuntao$ cat 1。txt111222333bogon:tmp zhangjuntao$ sed G 1。txt111222333
46、替換掉字母數字符。
$ cat stdlist #ID #Name[101] -zhangsan[102] -zhangsanfeng$ sed ‘s/[A-Za-z0-9]//g’ stdlist # #[] -[] -
47、使用“&”列印匹配的字串
以下命令將搜尋以“ L”開頭的單詞,並透過使用“&”符號將
“匹配的字串為–
” 附加到匹配的單詞,從而替換文字。在此,“ p”用於列印修改後的文字。
[root@zjt]$ cat os。txt Windows Linux Android OS[root@zjt]$ sed -n ‘s/^L/Matched String is - &/p’ os。txtMatched String is - Linux
48、把檔案中的兩列對調
[root@zjt-baidu-server sed]# cat course。txt PHP ASPMySQL OracleCodeIgniter Laravel[root@zjt-baidu-server sed]# sed ‘s/\([^ ]*\) *\([^ ]*\)/\2 \1/’ course。txtASP PHPOracle MySQLLaravel CodeIgniter擴充套件下、只保留PHP ASP javaMySQL Oracle db2CodeIgniter Laravel yii[root@zjt-baidu-server sed]# sed ‘s/\([^ ]*\) *\([^ ]*\)*\([^ ]*\)/\1 \3/’ course。txtPHP javaMySQL db2CodeIgniter yii
49、大寫每個單詞的第一個字元
[root@zjt]$ echo “I like bash programming” | sed ‘s/\([a-z]\)\([a-zA-Z0-9]*\)/\u\1\2/g’I Like Bash Programming
50、列印檔案的行號
[root@zjt]$ cat os。txt Windows Linux Android OS[root@zjt]$ sed ‘=’ os。txt 1 Windows 2 Linux 3 Android 4 OS
上面的教程透過使用非常簡單的示例說明了sed命令的不同用法。這裡提到的所有`sed`指令碼的輸出都是臨時生成的,原始檔案的內容保持不變。可以使用sed命令的–i或–in-place選項修改原始檔案。
[root@zjt]$ sed -i ‘=’ os。txt[root@zjt]$ cat os。txt 1Windows2Linux3Android4OS
sed使用好了可以大幅度提高檔案的修改速度,但如果要加-i選項對伺服器檔案修改,有一定危險性。
一定提前備份;
一定提前備份;
一定提前備份;
替換步驟
對檔案備份
用臨時檔案做測試
替換或者先輸出檢查
用vimdiff等對比工具對比
總結 格式
sed [options] ‘command’ file(s)
sed [options] -f scriptfile file(s)
歡迎點贊,評論,轉發,收藏!!!
有不對的感謝指正!!!