一: sed 是什麼?
sed 行編輯工具. sed , awk , grep 被稱作文本三劍客.
二:sed 的命令語法
sed [option]... '地址 編輯命令' FILE...
optin 的選項有:
-n: 靜默模式,不顯示模式空間中的內容 -r: 支持使用擴展正則表達式 -i: 修改原文件; -e: sed -e "" -e "" -e "", sed "{COM1;COM2;COM3}"
填寫地址方式:
行範圍: start_line[, end_line] # sed -n '1,5p' test.txt #從第一行到第五行打印出來 # sed -n '1,$p' test.txt #從第一行到最後一行打印出來 /patten1/,/patten2/: 第一次被pattern1匹配到的行開始,到第一次被pattern2匹配到的行結束之間的所有行; # sed -n '/\<and\>/,/\<or\>/p' test.txt 特定行: /pattern/ : 符合條件的特定行. # sed -n '/all/p' test.pp 無地址:全文
編輯命令
編輯命令:命令可在之前加!取反 p:打印 d: 刪除 i \text: 行上方,text即爲插入的內容 a \text: 行下方,text即爲插入的內容 r /path/from/some_file: 讀入文件 w /path/to/some_file: 把符合條件的行保存至指定的文件中 =: 顯示符合條件行的行號 s///: s@@@ g:爲全局替換 s///g
一些常見的幾個命令
‘s / \ . $ / / g’ 刪除以句點結尾的. ‘-e /abcd/d’ 刪除包含a b c d的行. ‘s / [ ][ ]* / [ ] / g’ 刪除一個以上空格,用一個空格代替 此處的[] 指空格代替 ‘s / ^ [ ] [ ] * / / g’ 刪除行首空格 ‘s / \ . [ ] [ ] * / [ ] / g’ 刪除句點後跟一個或更多空格,代之以一個空格 ‘/ ^ $ / d’ 刪除空行 ‘s / ^ . / / g’ 刪除第一個字符 ‘s / ^ \ / / / g’ 從路徑中刪除第一個/ ‘S / ^ [ ] / / g’ 刪除行首所有t a b鍵 ‘s / [ ] * / / g’ 刪除所有t a b鍵
三:增刪換顯幾個練習
例:cat 1.txt
cat 1.txt 12332###DISO###45.12^M 00332###LPSO###23.14^M 01299###USPD###34.46^M 把所有是#的內容替換掉 sed 's/##*//' 1.txt 12332DISO45.12^M 00332LPSO23.14^M 01299USPD34.46^M 顯示第2行內容 sed -n '2p' 1.txt 00332###LPSO###23.14^M 刪除所有行首是0 的行內容 sed /^00*/d 1.txt 12332###DISO###45.12^M 替換行首是0的內容爲1 sed 's/^0/1/g' 1.txt 12332###DISO###45.12^M 10332###LPSO###23.14^M 11299###USPD###34.46^M 替換行尾是特殊字符^M sed 's/\^M$//g' 1.txt 12332###DISO###45.12 00332###LPSO###23.14 01299###USPD###34.46 操作集合 cat 1.txt |sed 's/##*//g' | sed 's/\^M//g' | sed '2d' | sed '/^0/i hello' | sed '/^0/a word' | sed '1p' 12332DISO45.12 12332DISO45.12 hello 01299USPD34.46 word 看到前兩行是同樣的內容輸出,如果我們想要輸出匹配的一行. 用-n 參數; 如果想要讓上面的內容生效用-i 參數
四:打印行號
例 :cat 2.txt
cat 2.txt The honeysuckle band played all night long for only $90. It was an evening of splendid art and company. The office Dibble art played well. The local nurse music P.Neave was in attendance. sed -e '/was/=' 2.txt The honeysuckle band played all night long for only $90. 2 It was an evening of splendid art and company. The office Dibble art played well. 4 The local nurse music P.Neave was in attendance. 如果只關心行號 把-e換成-n 只顯示匹配到的行和行號 sed -n -e '/was/p' -e '/was/=' 2.txt It was an evening of splendid art and company. 2 The local nurse music P.Neave was in attendance. 4
五:讀入文件內容,寫入文件內容
cat 1.txt 12332###DISO###45.12^M 00332###LPSO###23.14^M 01299###USPD###34.46^M cat 2.txt The honeysuckle band played all night long for only $90. It was an evening of splendid art and company. The office Dibble art played well. The local nurse music P.Neave was in attendance. 把2.txt中的內容讀入1.txt第一行下面 sed '1r 2.txt' 1.txt 12332###DISO###45.12^M The honeysuckle band played all night long for only $90. It was an evening of splendid art and company. The office Dibble art played well. The local nurse music P.Neave was in attendance. 00332###LPSO###23.14^M 01299###USPD###34.46^M 把1.txt第1行寫入test.txt中 sed '1w test.txt' 1.txt cat test.txt 12332###DISO###45.12^M
六:在內容後面插入內容
例 :cat 3.txt
cat 3.txt AC456 AC492169 AC9967 AC88345 在數字最後插入 passwd sed 's/[0-9][0-9]*/& passwd/g' 3.txt sed 's/[0-9][0-9]*/& passwd/g' 3.txt AC456 passwd AC492169 passwd AC9967 passwd AC88345 passwd