1. 程式人生 > >linux三劍客之sed入門詳解

linux三劍客之sed入門詳解

linux 三劍客 sed

sed介紹
sed流編輯器(stream editor),在三劍客中排行老二,是一款簡單的文本編輯語言。sed並不直接處理源文件,而是逐行讀取源文件的內容到內存(稱模式空間)中,然後在模式空間中使用sed命令處理,再打印模式空間處理後的內容到標準輸出。
sed的能夠實現的功能:增刪改查、替換、過濾、取行。
sed文本處理原理圖
技術分享圖片


sed命令的語法:
sed [選項] ‘AddressCommand [修飾符]’inputfile(輸入文件)
sed命令語法各參數解釋


實驗環境

[root@bogon tmp]# cat userpasswd.txt 
1  stu01
2  9cab8
3  stu02
4  28f37
5  stu03
6  cd4ef
7  stu04
8  221ec
9  stu05
10  f7a98
[root@bogon tmp]#

選項

-n: 取消默認輸出,不打印(輸出)模式空間的內容
常與p連用,只打印符合條件的行(取消默認輸出,但打印符合條件的行)

[root@bogon tmp]# sed -n ‘s#stu01#stu#g‘p userpasswd.txt 
1  stu
[root@bogon tmp]#  

-i:直接修改源文件(謹慎使用,以免操作失誤導致源文件損壞)
-e:多個操作同時進行
-r:使用擴展表達式


Address:地址範圍

1、LineNumber:特定的行,指定第幾行,即n;$:表示最後一行

[root@bogon tmp]# sed -n 2p userpasswd.txt 
2  9cab8
[root@bogon tmp]#
[root@bogon tmp]# sed -n ‘$p‘ userpasswd.txt 
10  f7a98
[root@bogon tmp]#

2、Start,End:起始行,結束行,例如“2,5”,從第2行到第5行
[root@bogon tmp]# sed -n 2,5p userpasswd.txt

2  9cab8
3  stu02
4  28f37
5  stu03
[root@bogon tmp]#

3、mode1,mode2:從第一次被模式1匹配的行開始,到第一次被模式2匹配的行結束

[root@bogon tmp]# sed -n ‘/stu02/,6p‘ userpasswd.txt  
3  stu02
4  28f37
5  stu03
6  cd4ef
[root@bogon tmp]#

4、StartLine,+n:從StartLine開始往後n行,例如:“5,+2”,從第5行開始往後2行,即從第5行到第7行

[root@bogon tmp]# sed -n 5,+2p userpasswd.txt 
5  stu03
6  cd4ef
7  stu04
[root@bogon tmp]#

5、/^root/:地址範圍可以使用正則表達式,但必須要用符號“/ /”括起來,此處的含義是找出以root開頭的行

[root@bogon tmp]# sed -n ‘/.*stu.*/p‘ userpasswd.txt 
1  stu01
3  stu02
5  stu03
7  stu04
9  stu05
[root@bogon tmp]#

Command: sed執行命令
1、d:刪除符合條件的行

[root@bogon tmp]# sed 2,3d userpasswd.txt    
1  stu01
4  28f37
5  stu03
6  cd4ef
7  stu04
8  221ec
9  stu05
10  f7a98
[root@bogon tmp]#

2、p:打印符合條件的行(輸出)

[root@bogon tmp]# sed -n 3,4p userpasswd.txt 
3  stu02
4  28f37
[root@bogon tmp]#

3、’a 內容’:在指定的行後面追加新的內容

[root@bogon tmp]# sed ‘2a 11  hello‘ userpasswd.txt    
1  stu01
2  9cab8
11  hello
3  stu02
4  28f37
…

4、’i 內容’:在指定的行前追加新的內容

[root@bogon tmp]# sed ‘2i 11  hello‘ userpasswd.txt  
1  stu01
11  hello
2  9cab8
3  stu02
4  28f37
…

5、r file:將指定的文件內容添加到符合條件的行後面

[root@bogon tmp]# cat test 
who
[root@bogon tmp]# sed ‘2r /tmp/test‘ userpasswd.txt 
1  stu01
2  9cab8
who
3  stu02
4  28f37
…

6、w file:將符合條件的行的內容另存為指定文件中

[root@bogon tmp]# sed -n ‘4w /tmp/test1‘ userpasswd.txt 
[root@bogon tmp]# cat test1
4  28f37
[root@bogon tmp]#

7、s/模式/字符/:查找替換,也可以寫成s#模式#字符#、s@模式@字符【s/需要查找的內容/替換的內容】
修飾符:
g:替換所有(全局替換)

[root@bogon tmp]# sed ‘s#stu01#stu#g‘ userpasswd.txt 
1  stu
2  9cab8
3  stu02
…
[root@bogon tmp]# sed -n ‘s#stu01#stu#g‘p userpasswd.txt (只打印符合條件的行)
1  stu
[root@bogon tmp]#

i:忽略字符的大小寫
[root@bogon tmp]# sed ‘s#sTu01#stu#g‘ userpasswd.txt      
1  stu01
2  9cab8
3  stu02
…
[root@bogon tmp]# sed ‘s#sTu01#stu#gi‘ userpasswd.txt  
1  stu
2  9cab8
3  stu02
…

8、l:打印不可見字符

[root@bogon tmp]# sed -n l userpasswd.txt 
1  stu01$
2  9cab8$
3  stu02$
4  28f37$
5  stu03$
…
 [root@bogon tmp]#

特殊符號:
&:代表被替換的內容

[root@bogon tmp]# sed ‘s#t#--&--#g‘ userpasswd.txt 
1  s--t--u01
2  9cab8
3  s--t--u02
4  28f37
5  s--t--u03
…

linux三劍客之sed入門詳解