sed基本使用

sed [options] sed-commands input-file

options:

-n: 靜默模式,不輸出模式空間中的內容

-e script –e script: 指定多個腳本命令

-f script_file: 指定腳本文件

-i: 直接編輯原文件

-r: 使用擴展的正則表達式

 

sed-commands:

d: 刪除

p: 打印

i \text: 在模式匹配行的前面插入

a \text: 在模式匹配行的後面追加

r /path/to/somefile: 在指定位置讀取另外一個文件插入

w /path/to/somefile: 把符合條件的所有行保存至指定的文件中

=: 打印行號

s/查找條件/替換文本/: 查找替換

 

地址定界:

1,3: 匹配第一行到第三行

2,$:  匹配第二行到最後一行

/he/,$: 匹配he所在的行到最後一行

/he/,/yy/:匹配he所在的行到yy所在的行

/li/: 匹配所有li的行


sed查找替換命令格式:

    sed '[address-range | pattern-range] s/original-string/replacement-string/[flags]' input-file

    flags:

        g: 替換一行中所有匹配的模式

        [1|2|3|..n]: 規劃換一行中第n次匹配的模式

        p: 打印替換後的模式

        w output.txt: 把替換後的文本寫入文件output.txt

        i: 忽略搜索的模式的大小寫

        

sed的工作模式:

    1.讀取一行到模式空間(模式空間是一個sed命令的buffer空間)

    2.在此模式空間中執行sed 命令

    3.打印模式空間中的行

    4.重複以上步驟


實例:

     以employee.txt爲input-file

     

    # vim employee.txt 
    101,John Doe,CEO
    102,Jason Smith,IT Manager
    103,Raj Reddy,Sysadmin
    104,Anand Ram,Developer
    105,Jane Miller,Sales Manager

     

     打印所有行

# sed -n 'p' employee.txt 
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager


從文件中讀取sed-commands

# vim script.sed 
/admin/ p
/IT/ p
# sed -n -f script.sed employee.txt 
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin


使用-e指定多個sed-commands(方法一)

# sed -n -e '/admin/p' -e '/IT/p' employee.txt 
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin


使用-e指定多個sed-commands(方法二)

# sed -n \
> -e '/admin/p' \
> -e '/IT/p' \
> employee.txt 
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin


使用-e指定多個sed-commands(方法二)


打印第二行

# sed -n '2 p' employee.txt 
102,Jason Smith,IT Manager


打印第一行到第四行

# sed -n '1,4 p' employee.txt  
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer


打印第二行到最後一行

# sed -n '2,$ p' employee.txt    
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager


打印從第一行開始的2行

# sed -n '1,+2 p' employee.txt  
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin


打印奇數行

# sed -n '1~2 p' employee.txt    
101,John Doe,CEO
103,Raj Reddy,Sysadmin
105,Jane Miller,Sales Manager


打印偶數行

# sed -n '2~2 p' employee.txt  
102,Jason Smith,IT Manager
104,Anand Ram,Developer


刪除第二行

# sed '2 d' employee.txt 
101,John Doe,CEO
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager


刪除第三行到最後一行

# sed '3,$ d' employee.txt 
101,John Doe,CEO
102,Jason Smith,IT Manager


刪除空行

# sed '/^$/ d' employee.txt


刪除以#開始的註釋行

sed '/^#/ d' employee.txt


把employee.txt的偶數行寫入到output.txt

# sed -n '2 w output.txt' employee.txt 
# cat output.txt
102,Jason Smith,IT Manager


把Manager改成Director

# sed 's/Manager/Director/' employee.txt 
101,John Doe,CEO
102,Jason Smith,IT Director
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Director


搜索Sales所在行的Manager爲Director

# sed '/Sales/s/Manager/Director/' employee.txt 
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Director


搜索替換一行中所有的小寫字母a爲大寫字母A

# sed 's/a/A/g' employee.txt 
101,John Doe,CEO
102,JAson Smith,IT MAnAger
103,RAj Reddy,SysAdmin
104,AnAnd RAm,Developer
105,JAne Miller,SAles MAnAger


搜索替換一行中第二個出現的小寫字母a爲大寫字母A

[root@unp ~]# sed 's/a/A/2' employee.txt  
101,John Doe,CEO
102,Jason Smith,IT MAnager
103,Raj Reddy,SysAdmin
104,Anand RAm,Developer
105,Jane Miller,SAles Manager


搜索包含John的行並替換爲Jonny,僅打印此行

# sed -n 's/John/Jonny/p' employee.txt 
101,Jonny Doe,CEO


搜索包含John的行並替換爲Jonny,寫入到文件output.txt

# sed -n 's/John/Jonny/w output.txt' employee.txt  
# cat output.txt 
101,Jonny Doe,CEO


搜索包含John的行(忽略大小寫)並替換爲Jonny,寫入到文件output.txt

# sed -n 's/john/Jonny/iw output.txt' employee.txt 
# cat output.txt 
101,Jonny Doe,CEO


在第1到2行的前面的行首插入<<,行尾插入>>

# sed -e '1,2s/^/<< /' -e '1,2s/$/ >>/' employee.txt          
<< 101,John Doe,CEO >>
<< 102,Jason Smith,IT Manager >>


sed搜索定界符也可使用以下幾種形式

sed 's|/usr/local/bin|/usr/bin|' input-file
sed 's^/usr/local/bin^/usr/bin^' input-file
sed 's@/usr/local/bin@/usr/bin@' input-file
sed 's!/usr/local/bin!/usr/bin!' input-file


&用於替換匹配的oritinal-string

# sed 's/^[0-9]\{3\}/[&]/g' employee.txt 
[101],John Doe,CEO
[102],Jason Smith,IT Manager
[103],Raj Reddy,Sysadmin
[104],Anand Ram,Developer
[105],Jane Miller,Sales Manager
# sed -r 's/^[0-9]{3}/[&]/g' employee.txt   
[101],John Doe,CEO
[102],Jason Smith,IT Manager
[103],Raj Reddy,Sysadmin
[104],Anand Ram,Developer
[105],Jane Miller,Sales Manager


單分組引用

# sed 's/\([^,]*\).*/\1/g' employee.txt   
101
102
103
104
105


多分組引用

# sed 's/\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\3/g' employee.txt 
101,CEO
102,IT Manager
103,Sysadmin
104,Developer
105,Sales Manager


\l表示把後面的單個字符N視爲小寫n,\L表示將後面的所有字符HNNY視爲小寫hnny

# sed -n 's/John/JO\LHNNY/p' employee.txt  
101,JOhnny Doe,CEO
# sed -n 's/John/JO\lHNNY/p' employee.txt  
101,JOhNNY Doe,CEO


\u表示把後面的單個字符n視爲大寫N,\U表示將後面的所有字符hnny視爲大寫HNNY

# sed -n 's/John/JO\uhnny/p' employee.txt        
101,JOHnny Doe,CEO
# sed -n 's/John/JO\Uhnny/p' employee.txt  
101,JOHNNY Doe,CEO


\E和\L \U配合使用,作爲定界符

# sed -n 's/John/JO\Uhnn\Ey/p' employee.txt    
101,JOHNNy Doe,CEO


把\3改爲大寫

# sed 's/\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\U\3\E/g' employee.txt 
101,CEO
102,IT MANAGER
103,SYSADMIN
104,DEVELOPER
105,SALES MANAGER


把sed作爲一個解釋器來用

# cat script.sed 
#!/bin/sed -f
s/\([^,]*\),\([^,]*\),\([^,]*\).*/\2,\1,\3/g
s/^.*/<&>/
s/Developer/IT Manager/
s/Manager/Director/
# ./script.sed employee.txt 
<John Doe,101,CEO>
<Jason Smith,102,IT Director>
<Raj Reddy,103,Sysadmin>
<Anand Ram,104,IT Director>
<Jane Miller,105,Sales Director>


直接修改原文件

# sed -i 's/John/Johnny/' employee.txt
# cat employee.txt
101,Johnny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager


直接修改原文件,並備份原文件

# sed -ibak 's/John/Johnny/' employee.txt                
# ll employee.txt*
-rw-r--r-- 1 root root 123 Sep 15 18:05 employee.txt
-rw-r--r-- 1 root root 121 Sep 15 18:05 employee.txtbak


直接修改原文件,並指定文件後綴名

# sed --in-place=.bak 's/John/Johnny/' employee.txt
# ll employee.txt*
-rw-r--r-- 1 root root 123 Sep 15 18:08 employee.txt
-rw-r--r-- 1 root root 121 Sep 15 18:07 employee.txt.bak


追加a和插入i

# sed -e '1 i \Staff Information\n-------------' -e '$ a \END\n-------------' employee.txt   
Staff Information
-------------
101,Johnny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
END
-------------


替換c

# sed '/Sales/ c \105,Jane Miller,Sales Director' employee.txt
101,Johnny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Director


打印隱藏字符

# sed -n 'l' employee.txt
101,Johnny Doe,CEO$
102,Jason Smith,IT Manager$
103,Raj Reddy,Sysadmin$
104,Anand Ram,Developer$
105,Jane Miller,Sales Manager$


打印行號

# sed '=' employee.txt
1
101,Johnny Doe,CEO
2
102,Jason Smith,IT Manager
3
103,Raj Reddy,Sysadmin
4
104,Anand Ram,Developer
5
105,Jane Miller,Sales Manager


轉換大小寫字母

# sed  'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' employee.txt 
101,JOHNNY DOE,CEO
102,JASON SMITH,IT MANAGER
103,RAJ REDDY,SYSADMIN
104,ANAND RAM,DEVELOPER
105,JANE MILLER,SALES MANAGER


多個input-file

# sed -n '/^root\>/ p' /etc/{passwd,group}  
root:x:0:0:root:/root:/bin/bash
root:x:0:


退出命令q

# sed 'q' employee.txt
101,Johnny Doe,CEO
# sed '2q' employee.txt
101,Johnny Doe,CEO
102,Jason Smith,IT Manager


命令n:用於打印當前模式空間,並從input-file中讀入下一行

# sed  'n' employee.txt  
101,Johnny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager



     

    

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