【複習】sed用法,有的不常用已忘記了

sed 的詳細用法

sed:stream editor 流編輯器
sed的工作模式;sed是一個行文本編輯器,默認每次處理文本中所匹配到一行內容到模式空間,然後用後面的命令進行操作,操作完成之後,會把模式空間
裏面的內容實現在屏幕上,然後把模式空間的中的內容刪除,同時把下一行所匹配到內容讀入模式空間

爲了下面演示方便,我們創建一個文本文件a.text

#cat a.text
zz xx tom xx tom rhce xx rh
ce xx xx tom xx rhce xx tom xx 
xx xx tom xx rhce xx xx 
xxx xxx tom rhce xx xx 
yyyy rhce yyyyy

sed 命令的語法格式:

sed [options] 'addresscommand' file (1)
sed [options] -f scriptfile file (2)

(2)是sed的腳本用法後面會介紹到
先介紹語法(1)種用法:**


address(匹配的條件)的表示法:

linenumber:指定特定行號
startline,endline:指定起始行號,和結束行號
linenumber,+N :N數字,表示從指定行號向後N行
/patten/:以正則表達式的匹配模式
/patten1/,/patten2/:從模式匹配1到模式匹配2


options:選項

-n 靜默模式 常使用p命令時會用
-i 改變原文件
-f file 跟腳本文件
-e 可以執行多個命令語句


command:命令(簡單用法)

p:打印模式空間中行
d:刪除,刪除選擇的行
r file:讀取file中的內容追加到匹配的行後面
R file:讀取file中的內容的第一行追加到匹配行的後面
w file:將匹配到的行,保存到file中
W file:將匹配到的行的第一行,保存到file中
a \string 在當前行下面插入文本。
i \string 在當前行上面插入文本。
! 表示後面的命令對所有沒有被選定的行發生作用.
y 和s用法類似,但只能替換大小寫
s/patten/string/ 把patten匹配到的換成string字符


sed替換標記:

g表示全局替換
\1表示前面第一個左括號所表示的內容,\2表示前面第二個左括號中表示的內容,以此類推
&表示前面匹配到的內容


sed 元字符

^ 匹配行開始,如:/^sed/匹配所有以sed開頭的行。
$ 匹配行結束,如:/sed$/匹配所有以sed結尾的行。
. 匹配一個非換行符的任意字符,如:/s.d/匹配s後接一個任意字符,最後是d。

  • 匹配0個或多個字符,如:/*sed/匹配所有模板是一個或多個空格後緊跟sed的行。
    [] 匹配一個指定範圍內的字符,如/[ss]ed/匹配sed和Sed。 
    [^] 匹配一個不在指定範圍內的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一個字母開頭,緊跟ed的行。
    (..) 匹配子串,保存匹配的字符,如s/(love)able/\1rs,loveable被替換成lovers。
    & 保存搜索字符用來替換其他字符,如s/love/&/,love這成love
    \< 匹配單詞的開始,如:/\<love/匹配包含以love開頭的單詞的行。
    \> 匹配單詞的結束,如/love\>/匹配包含以love結尾的單詞的行。
    x{m} 重複字符x,m次,如:/0{5}/匹配包含5個0的行。
    x{m,} 重複字符x,至少m次,如:/0{5,}/匹配至少有5個0的行。
    x{m,n} 重複字符x,至少m次,不多於n次,如:/0{5,10}/匹配5~10個0的行。

下面我們看例子:

address 匹配條件的用法

1,只顯示a.text中的2行

root@debian9:~# sed -n '2p' a.text 
ce xx xx tom xx rhce xx tom xx

注意,我們也裏面要用到-n 選項,如果不用-n 靜默模式的話,
它會顯示全部內容(這是sed的默認動作,可以看一下工作模式),匹配到的內容會顯示兩遍

2,刪除a.text 中2-4行

root@debian9:~# sed "2,4d" a.text 
zz xx tom xx tom rhce xx rh
yyyy rhce yyyyy 

3,上面刪除a.text中2-4行,也可以這樣表示

root@debian9:~# sed "2,+2d" a.text 
zz xx tom xx tom rhce xx rh
yyyy rhce yyyyy 

4,刪除以x開頭的行。

root@debian9:~# sed "/^x/d" a.text 
zz xx tom xx tom rhce xx rh
ce xx xx tom xx rhce xx tom xx 
yyyy rhce yyyyy 

5,刪除a.text中以第一個zz開頭的行到第一個xx開頭的行結束。

root@debian9:~# sed "/^zz/,/^xx/d" a.text
xxx xxx tom rhce xx xx 
yyyy rhce yyyyy

command 命令的用法

1,i \string 的用法,在a.text中ce開頭的行前面追加 i love linux 的新行

root@debian9:~# sed "/^ce/i \i love linux" a.text   
zz xx tom xx tom rhce xx rh
i love linux
ce xx xx tom xx rhce xx tom xx 
xx xx tom xx rhce xx xx 
xxx xxx tom rhce xx xx 
yyyy rhce yyyyy 

2,a \string 的用法和i \string 類似 在a.text中ce開頭的行後面追加i love linux 的新行

root@debian9:~# sed "/^ce/a \i love linux" a.text 
zz xx tom xx tom rhce xx rh
ce xx xx tom xx rhce xx tom xx 
i love linux
xx xx tom xx rhce xx xx 
xxx xxx tom rhce xx xx 
yyyy rhce yyyyy

3,r file的用法,把/etc/fstab 追加到a.text中含有y的行。

root@debian9:~# sed "/.*y.*/r /etc/fstab" a.text 
zz xx tom xx tom rhce xx rh
ce xx xx tom xx rhce xx tom xx 
xx xx tom xx rhce xx xx 
xxx xxx tom rhce xx xx 
yyyy rhce yyyyy 
/etc/fstab: static file system information.
<file system> <mount point>   <type>  <options>   <dump>  <pass>
/ was on /dev/sda1 during installation
UUID=d12e3f89-049d-45c1-8b31-3a1cf551585f /   ext4    errors 0       1
swap was on /dev/sda5 during installation
UUID=07308ec5-870b-46f0-b983-ebc2a56fb0f2 none  swap    sw   0       0
/dev/sr0  /media/cdrom0   udf,iso9660 user,noauto     0       0

4,w file 的用法,把a.text中含有tom的行,保存到/root/b.text

root@debian9:~# sed "/tom/w /root/b.text" a.text
root@debian9:~# cat b.text 
zz xx tom xx tom rhce xx rh
ce xx xx tom xx rhce xx tom xx 
xx xx tom xx rhce xx xx 
xxx xxx tom rhce xx xx 

5,s/patten/string/的使用。把a.text 中第一個tom換成jerry .

root@debian9:~# sed "s/tom/jerry/" a.text 
zz xx jerry xx tom rhce xx rh
ce xx xx jerry xx rhce xx tom xx 
xx xx jerry xx rhce xx xx 
xxx xxx jerry rhce xx xx 
yyyy rhce yyyyy 

提示;sed默認只替換第一個匹配到的patten.如果想替換隻替換第二tom爲jerry
這時替換標記就用作用了
這條命令可以這樣寫:sed "s/tom/jerry/2" a.text 以此類推。可以替換想要替換的,

如果要想替換全部的可以寫成
sed "s/tom/jerry/g" a.text

root@debian9:~# sed "s/tom/jerry/g" a.text
zz xx jerry xx jerry rhce xx rh
ce xx xx jerry xx rhce xx jerry xx 
xx xx jerry xx rhce xx xx 
xxx xxx jerry rhce xx xx 
yyyy rhce yyyyy

&和\1的用法,&和\1的相同之處,和不同點
把she like my love 中 like和love後面都加上一個r.變成she liker my lover

root@debian9:~# echo "she like my love" | sed "s/l..e/&r/g"
she liker my lover

註釋,&表示前一個/patten/中的內容

root@debian9:~# echo "she like my love" | sed  "s/\(l..e\)/\1r/g"
she liker my lover

可以用分組的方法來實現後向引用。

如果把she like my love 中like和love中的l變成L。變成she Like my Love

root@debian9:~# echo "she like my love" | sed  "s/l\(..e\)/L\1/g"
she Like my Love

這樣的只能用分組來實現。不能用&來引用。

sed的高級用法

command:命令、

=:顯示行號
n:讀取匹配模式的行的下一行到模式空間中.注:模式空間中匹配模式的行被刪除。
N:讀取匹配模式的行,和匹配模式行的下一行。
h 拷貝模板塊的內容到內存中的緩衝區。如果原來緩衝區有內容被覆蓋
H 追加模板塊的內容到內存中的緩衝區。
g 獲得內存緩衝區的內容,並替代當前模板塊中的文本。
G 獲得內存緩衝區的內容,並追加到當前模板塊文本的後面

高級command的用法

1,= 的用法。顯示以ce開頭的是第幾行。

root@debian:~#  sed "/^ce/=" a.text 
zz xx tom xx tom rhce xx rh
2
ce xx xx tom xx rhce xx tom xx 
xx xx tom xx rhce xx xx 
xxx xxx tom rhce xx xx 
yyyy rhce yyyyy 

2,n 的用法。把zz開頭的行,下一行中的tom全部換成TOM

root@debian:~# sed "/^zz/{n;s/tom/TOM/g}" a.text
zz xx tom xx tom rhce xx rh
ce xx xx TOM xx rhce xx TOM xx 
xx xx tom xx rhce xx xx 
xxx xxx tom rhce xx xx 
yyyy rhce yyyyy 

3,N的用法,把zz開頭的行和下一行中的rhce全部換成RHCA.

root@debian:~# sed "/^zz/{N;s/rhce/RHCA/g;s/rh\nce/RH\nCA/}" a.text 
zz xx tom xx tom RHCA xx RH
CA xx xx tom xx RHCA xx tom xx 
xx xx tom xx rhce xx xx 
xxx xxx tom rhce xx xx 
yyyy rhce yyyyy 

首先,要用N讀取zz開頭的行和它的下一行,可以看到zz行的結尾處rh和ce開頭的行是兩行,
用s/rhce/RHCA/g不會被替換。所以我們還要s/rh\nce/RH\nCA/來替換。

如果我們使用多行模式,^就不是表示行的開頭,而是模式空間裏的開頭,$也不是行的結尾了,而是行的結尾

root@debian:~# sed "/^xx/{N;s/^xx/qq/g}" a.text 
zz xx tom xx tom rhce xx rh
ce xx xx tom xx rhce xx tom xx 
qq xx tom xx rhce xx xx 
xxx xxx tom rhce xx xx 
yyyy rhce yyyyy

分析:如果不是模式空間的開頭,那麼下面兩個xx也會被替換成qq.

h和G的用法:
我們新建一個文本b

#cat b
a
b
aa
bb

4,通過模式空間和保持空間的轉化實現的效果

root@debian:~# sed "/a/{h;d};/b/G" b
b
a
bb
aa

sed [options] -f scriptfile file (2)
通常通過腳本對文件批量處理;
如一個文件中需要多出用sed 修改可以使用腳本
腳本文件格式爲:'addresscommand'
'addresscommand'
......

匹配整行替換

sed 's/^.*匹配的字符串.*$/新的字符串/g' /etc/shadow

sed '/匹配的字符串/c 新的字符串' /etc/shadow

行尾添加字符串:

sed '/^.*vmlinuz.*$/s//& test/g' grub.cfg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章