Sed Awk 日常使用總結


Sed命令語法
sed [option] {sed-commands}{input-file}
sed首先從input-file中讀取第一行,然后執行所有的sed-commands;再讀取第二行,執行所有sed-commands,
重復這個過程,直到input-file結束。
sed的執行過程:讀取內容,執行命令,打印結果,重復循環。

打印模式空間(命令p)
$-代表最后一行
修改地址范圍:逗號,加號,波浪號。
1,4-第一到第四行
2,$-第二到最后一行
n+m-從第n行開始后的m行
1~2匹配1,3,5,7,......
2~2匹配2,4,6,......
1~3匹配1,4,7,......
2~3匹配2,5,8,11,......
[root@node130 ~]# sed 'p' ./employee.txt
101,John Doe,CEO
101,John Doe,CEO
102,Jason Smith,IT Manager
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
104,Anand Ram,Developer
105,jane Miller,Sales Manager
105,jane Miller,Sales Manager
[root@node130 ~]# vim employee.txt
[root@node130 ~]# sed 'p' employee.txt
101,John Doe,CEO
101,John Doe,CEO
102,Jason Smith,IT Manager
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
104,Anand Ram,Developer
105,jane Miller,Sales Manager
105,jane Miller,Sales Manager
[root@node130 ~]# 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
[root@node130 ~]# sed -n  employee.txt
^C
[root@node130 ~]# 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
[root@node130 ~]# sed -n '2p' employee.txt
102,Jason Smith,IT Manager
[root@node130 ~]# sed -n '2 p' employee.txt
102,Jason Smith,IT Manager
[root@node130 ~]# sed -n '1,2 p' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
[root@node130 ~]# sed -n '1,24 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
[root@node130 ~]# sed -n '1,4p' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
[root@node130 ~]# sed -n '1,4 p' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
[root@node130 ~]# sed -n '2,$ p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,jane Miller,Sales Manager
[root@node130 ~]# sed -n '2,+2 p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
[root@node130 ~]# sed -n '1~2 p' employee.txt
101,John Doe,CEO
103,Raj Reddy,Sysadmin
105,jane Miller,Sales Manager
[root@node130 ~]# sed -n '2~2 p' employee.txt
102,Jason Smith,IT Manager
104,Anand Ram,Developer
[root@node130 ~]# sed -n '1~3 p' employee.txt
101,John Doe,CEO
104,Anand Ram,Developer
[root@node130 ~]# sed -n '/Jane/p' employee.txt
[root@node130 ~]# sed -n '/Jane/ p' employee.txt
[root@node130 ~]# cat employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,jane Miller,Sales Manager
[root@node130 ~]# vim employee.txt
[root@node130 ~]# sed -n '/Jane/p' employee.txt
105,Jane Miller,Sales Manager
[root@node130 ~]# sed -n '/Jason/,4p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
[root@node130 ~]# sed -n '/Raj/,$ p' employee.txt
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# sed -n '/Raj/,/Jane/ p' employee.txt
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# sed -n '/Jason,+2 p' employee.txt
sed: -e expression #1, char 11: unterminated address regex
[root@node130 ~]# sed -n '/Jane,+2 p' employee.txt
sed: -e expression #1, char 10: unterminated address regex
[root@node130 ~]# sed -n '/Jane,+2 p' employee.txt
sed: -e expression #1, char 10: unterminated address regex
刪除行
命令d用來刪除行;如果不提供地址范圍,sed默認匹配所有行,但不修改原始文件的內容。
[root@node130 ~]# sed 'd' employee.txt
[root@node130 ~]# cat employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# sed '2d' employee.txt
101,John Doe,CEO
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# sed '2,3d' employee.txt
101,John Doe,CEO
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# sed '2,4d' employee.txt
101,John Doe,CEO
105,Jane Miller,Sales Manager
[root@node130 ~]# sed '1,4d' employee.txt
105,Jane Miller,Sales Manager
[root@node130 ~]# sed '2,$d' employee.txt
101,John Doe,CEO
把模式空間內容寫到文件中(w命令)
命令w可以把當前模式空間內容保存到文件中。默認情況下模式空間的內容每次都會
打印到標准輸出,如果要把輸出保存到文件同時不顯示到屏幕上,還需要使用—n選項
[root@node130 ~]# ls
anaconda-ks.cfg  Documents  employee.txt  hosts2       install.log.syslog                                 Music     Public     test             tutor
Desktop          Downloads  hosts         install.log  KingbaseAnalyticsDB-3.0-build1-CENTOS6-x86_64.run  Pictures  Templates  test-script.sed  Videos
[root@node130 ~]# sed 'w output.txt' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# ls 
anaconda-ks.cfg  Documents  employee.txt  hosts2       install.log.syslog                                 Music       Pictures  Templates  test-script.sed  Videos
Desktop          Downloads  hosts         install.log  KingbaseAnalyticsDB-3.0-build1-CENTOS6-x86_64.run  output.txt  Public    test       tutor
[root@node130 ~]# vim output.txt
[root@node130 ~]# cat output.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# sed 'w output000.txt' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# sed -n 'w output007.ext' employee.txt
[root@node130 ~]# cat output00
output000.txt  output007.ext 
[root@node130 ~]# cat output007.ext
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# sed -n '2 w output2_line.txt' employee.txt
[root@node130 ~]# cat output2_line.txt
102,Jason Smith,IT Manager
[root@node130 ~]# sed -n '1,4 w output_1_4.txt' employee.txt
[root@node130 ~]# cat output_1_4.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
[root@node130 ~]# sed -n '2,$ w output_2_$.txt' employee.txt
[root@node130 ~]# cat output_2_\$.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# sed -n '1~2 w output_1~2.txt' employee.txt
[root@node130 ~]# ls
anaconda-ks.cfg  Downloads     hosts2              KingbaseAnalyticsDB-3.0-build1-CENTOS6-x86_64.run  output007.ext   output2_line.txt  Pictures   test             Videos
Desktop          employee.txt  install.log         Music                                              output_1~2.txt  output_2_$.txt    Public     test-script.sed
Documents        hosts         install.log.syslog  output000.txt                                      output_1_4.txt  output.txt        Templates  tutor
[root@node130 ~]# cat output_1~2.txt
101,John Doe,CEO
103,Raj Reddy,Sysadmin
105,Jane Miller,Sales Manager
[root@node130 ~]# sed -n '/Jane/ w output.Jane.txt' employee.txt
[root@node130 ~]# vim output.Jane.txt
[root@node130 ~]# vim output.Jane.txt
[root@node130 ~]# sed -n '/Jason/,4 w output.Jason.4.txt' employee.txt
[root@node130 ~]# vim output.Jason.4.txt
[root@node130 ~]# sed -n '/Raj/,$ w output.Raj.$.txt' employee.txt
[root@node130 ~]# vim output.Raj.\$.txt
[root@node130 ~]# sed -n '/Raj/,/Jane/ w output.Raj.Fane' employee.txt
[root@node130 ~]# vim output.Raj.Fane
[root@node130 ~]# sed -n '/Jason/,+2 w output.Jason.2.txt' employee.txt
[root@node130 ~]# vim output.Ja
output.Jane.txt     output.Jason.2.txt  output.Jason.4.txt 
[root@node130 ~]# vim output.Ja
output.Jane.txt     output.Jason.2.txt  output.Jason.4.txt 
[root@node130 ~]# vim output.Jason.2.txt
sed替換命令
sed中最強大的功能就是替換(substitute),功能強大。

sed '[address-range|pattern-range] s/original-string/replacement-string/[substitute-flags]' input-file
s-即執行替換命令substitute
original-string-是被sed搜索然后被替換的字符串,可以是一個正則表達式
replacement-string-替換后的字符串
substitute-flags-可選的,全局標志
全局標志g
g代表全局(global)默認情況下,sed只會替換每行中第一次出現的original-string;如果要替換每行中出現的
所有original-string,就需要使用g.
數字標志(1,2,3,......,512)
使用數字可以指定original-string出現的次序。只有第n次出現的original-string才會觸發替換。
打印標志p(print)
命令p代表print。當替換操作完成后,打印替換后的行;
sed中比較有用的方法是和-n一起使用以抑制默認的打印操作。
寫標志w
標志w代表write,當替換成功后,它把替換后的結果保存在文件中。
忽略大小寫標志i(ignore)
替換標識i代表忽略大小寫。可以使用i來以小寫字符的模式匹配original-string。只有GNU sed中才有。
執行命令標志e(excute)
替換標志e代表執行(execute).該標志可以將模式空間中的任何內容當作shell命令執行,並把命令執行的結果
返回到模式空間。只有GNU sed中才可使用。
[root@node130 ~]# 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
[root@node130 ~]# sed 's/Manager/Director/' employee.txt ;cat employee.txt
101,John Doe,CEO
102,Jason Smith,IT Director
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Director
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# sed '/Sales/s/Manager/Director' employee.txt
sed: -e expression #1, char 25: unterminated `s' command
[root@node130 ~]# 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
[root@node130 ~]# sed '/s/a/A' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
/A
103,Raj Reddy,Sysadmin
/A
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
/A
[root@node130 ~]# sed 's/a/A' employee.txt
sed: -e expression #1, char 5: unterminated `s' command
[root@node130 ~]# sed 's/a/A/' employee.txt
101,John Doe,CEO
102,JAson Smith,IT Manager
103,RAj Reddy,Sysadmin
104,AnAnd Ram,Developer
105,JAne Miller,Sales Manager
[root@node130 ~]# 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
[root@node130 ~]# 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
[root@node130 ~]# vim substitute-locate.txt
[root@node130 ~]# sed 's/locate/find/2' substitute-locate.txt
locate command is used to find files
locate command uses database to find files
locate command can also use regex for searching
[root@node130 ~]# sed -n 's/John/Johnny' employee.txt
sed: -e expression #1, char 13: unterminated `s' command
[root@node130 ~]# sed -n 's/John/Johnny/' employee.txt
[root@node130 ~]# sed  's/John/Johnny/' employee.txt
101,Johnny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# sed -n 's/John/Johnny/p' employee.txt
101,Johnny Doe,CEO
[root@node130 ~]# sed -n 's/locate/find/2p' substitute-locate.txt
locate command is used to find files
locate command uses database to find files
[root@node130 ~]# sed -n 's/John/Johnny/w Johnny.txt' employee.txt
[root@node130 ~]# vim Johnny.txt
[root@node130 ~]# sed 's/locate/find/2w locate_find.txt' substitute-locate.txt
locate command is used to find files
locate command uses database to find files
locate command can also use regex for searching
[root@node130 ~]# vim locate_find.txt
[root@node130 ~]# vim locate_find.txt
[root@node130 ~]# sed 's/john/Johnny/' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# sed 's/john/Johnny/i' employee.txt
101,Johnny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# ls
anaconda-ks.cfg  hosts               KingbaseAnalyticsDB-3.0-build1-CENTOS6-x86_64.run  output_1~2.txt    output.Jason.2.txt  Pictures               test-script.sed
Desktop          hosts2              locate_find.txt                                    output_1_4.txt    output.Jason.4.txt  Public                 tutor
Documents        install.log         Music                                              output2_line.txt  output.Raj.Fane     substitute-locate.txt  Videos
Downloads        install.log.syslog  output000.txt                                      output_2_$.txt    output.Raj.$.txt    Templates
employee.txt     Johnny.txt          output007.ext                                      output.Jane.txt   output.txt          test
[root@node130 ~]# mkdir sed
[root@node130 ~]# cd sed/
[root@node130 sed]# ls
[root@node130 sed]# touch files.txt
[root@node130 sed]# ;s
bash: syntax error near unexpected token `;'
[root@node130 sed]# ls
files.txt
[root@node130 sed]# cp /etc/passwd .
[root@node130 sed]# ls
files.txt  passwd
[root@node130 sed]# cp /etc/gr
group      group-     grub.conf 
[root@node130 sed]# cp /etc/group .
[root@node130 sed]# ls
files.txt  group  passwd
[root@node130 sed]# vim group
[root@node130 sed]# sed 's/^/ls -l/' files.txt
[root@node130 sed]# vim files.txt
[root@node130 sed]# ls -l /etc/passwd
-rw-r--r--. 1 root root 1703 Jan 10 02:20 /etc/passwd
[root@node130 sed]# sed 's/^/ls -l/e' files.txt
[root@node130 sed]# sed -n 's/manager/Director/igpw output.txt' employee.txt
102,Jason Smith,IT Director
105,Jane Miller,Sales Director
[root@node130 sed]# cat output.txt
102,Jason Smith,IT Director
105,Jane Miller,Sales Director
[root@node130 sed]# vim path.txt
sed替換命令分界符
sed默認分界符/,即s/original-string/replacement-string/g
如果在original-string或replacement-string中有/,那么需要使用反斜杠\來轉義。
可以使用任何一個字符作為sed替換命令的分節符,如|或^或@或!。
單行內容上執行多個命令
[root@node130 sed]# sed '{
> s/Developer/IT Manager/
> s/Manager/Director/
> }' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Director
103,Raj Reddy,Sysadmin
104,Anand Ram,IT Director
105,Jane Miller,Sales Director
[root@node130 sed]# sed '{
> s@Developer@IT Manager@
> s@Manager@Director@
> }' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Director
103,Raj Reddy,Sysadmin
104,Anand Ram,IT Director
105,Jane Miller,Sales Director
&的作用-獲取匹配到的模式
當在replacement-string中使用&時,它會被替換成匹配到的original-string或正則表達式。
[root@node130 sed]# sed 's$^[0-9][0-9][0-9]$[&]$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
把每一行放進<>中
[root@node130 sed]# sed 's$^.*$<&>$' employee.txt
<101,John Doe,CEO>
<102,Jason Smith,IT Manager>
<103,Raj Reddy,Sysadmin>
<104,Anand Ram,Developer>
<105,Jane Miller,Sales Manager>

正則表達式
行的開頭(^)
^匹配每一行的開頭;只有^出現在正則表達式開頭,他才匹配行的開頭;所以^N匹配所有以N開頭的行。
行的結尾($)
[root@node130 sed]# sed -n '/r$/p' employee.txt
102,Jason Smith,IT Manager
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
單個字符(.)
元字符.匹配除換行符之外的任意單個字符
.匹配單個字符
..匹配兩個字符
...匹配三個字符
[root@node130 sed]# sed -n '/J... /p' employee.txt
101,John Doe,CEO
105,Jane Miller,Sales Manager
匹配0次或多次(*)
星號*匹配0個或多個其前面的字符。如:1*匹配0個或多個1.
[root@node130 sed]# sed -n '/log: *./p' log.txt
log:input.txt
log: testing resumed
log:output created
匹配一次或多次(\+)
"\+"匹配一次或多次它前面的字符,例如 空格\+ 或"\+"匹配至少一個或多個空格。
匹配0次或一次(\?)
\?匹配0次或一次它前面的字符。如:
[root@node130 sed]# sed -n '/log: \?/p' log.txt
log:input.txt
log:
log: testing resumed
log:
log:output created
轉義字符(\)
如果要在正則表達式中搜尋特殊字符(如:*,.),必須使用\來轉義它們。
[root@node130 sed]# sed  -n '/127\.0\.0\.1/p' /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
字符集([0-9])
字符集匹配方括號中出現的任意一個字符;
方括號中,可以使用連接符-指定一個字符范圍。如[0123456789]可以用[0-9]表示,字母可以用
[a-z],[A-Z]表示。
[root@node130 sed]# sed -n '/[2-4]/p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
其他正則表達式
或操作符(|)
管道符號|用來匹配兩邊任意一個子表達式。子表達式1|子表達式2|子表達式3
[root@node130 sed]# sed -n '/101\|102/p' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
[root@node130 sed]# sed -n '/[2-3]\|105/p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
105,Jane Miller,Sales Manager
精確匹配m次({m})
正則表達式后面跟上{m}標明精確匹配該正則m次。
[root@node130 sed]# cat numbers.txt
1
12
123
1234
12345
123456

[root@node130 sed]# sed -n '/^[0-9]\{5}$/p' numbers.txt
sed: -e expression #1, char 13: Unmatched \{
[root@node130 sed]# sed -n '/^[0-9]\{5\}$/p' numbers.txt
12345
[root@node130 sed]# sed -n '/^[0-9]\{3\}$/p' numbers.txt
123
匹配m至n次({m,n})
正則表達式后面跟上{m,n}表明精確匹配該正則至少m,最多n次。m,n不能是負數,並且要小於255.
{m,}匹配至少m;最多不限;{,n}表明最多匹配n次,最少一次。
打印有3至5個數字組成的行
[root@node130 sed]# sed -n '/^[0-9]\{3,5\}$/p' numbers.txt
123
1234
12345
字符邊界(\b)
[root@node130 sed]# cat words.txt
word matching using:the
word matching using:thethe
word matching using:they

[root@node130 sed]# sed -n '/\bthe\b/p' words.txt
word matching using:the
[root@node130 sed]# sed -n '/\bthe/p' words.txt
word matching using:the
word matching using:thethe
word matching using:they
在sed替換中使用正則表達式
[root@node130 sed]# cat employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 sed]# sed -n 's@..$@,Not Defined@p' employee.txt
101,John Doe,C,Not Defined
102,Jason Smith,IT Manag,Not Defined
103,Raj Reddy,Sysadm,Not Defined
104,Anand Ram,Develop,Not Defined
105,Jane Miller,Sales Manag,Not Defined
[root@node130 sed]# sed -n 's/..$/,Not Defined/p' employee.txt
101,John Doe,C,Not Defined
102,Jason Smith,IT Manag,Not Defined
103,Raj Reddy,Sysadm,Not Defined
104,Anand Ram,Develop,Not Defined
105,Jane Miller,Sales Manag,Not Defined
清楚html標簽
[root@node130 sed]# cat test.html
<html><body><h1>Hello World!</h1></body></html>
[root@node130 sed]# sed 's/<[^>]*>//g' test.html
Hello World!
刪除所有注釋行和空行
[root@node130 sed]# sed -e 's/#.*//;/^$/d' /etc/profile
單行內執行多個sed命令
[root@node130 sed]# sed -e 'command1' -e 'command2' -e 'command3'
[root@node130 sed]# sed -n -e '/^root/p' -e '/^nobody/p' -e '/^mail/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
[root@node130 sed]# sed -n '{
> /^root/p
> /^nobody/p
> /^mail/p
> }' /etc/passwd
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
sed腳本命令及注釋
sed注釋以#開頭
[root@node130 sed]# cat mycommand.sed
#交換第一列和第二列
s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1,\3/g
#將整行內容放入<>中
s/^.*/<&>/
#把Developer替換為IT Manager
s/Developer/IT Manager/
#把Manager替換為Director
s/Manager/Director/
[root@node130 sed]# sed -f mycommand.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當作命令解釋器使用
需要在sed腳本最開始加入"#!/bin/sed -f".
[root@node130 sed]# cat myscript.sed
#!/bin/sed -f
s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1,\3/g
s/^.*/<&>/
s/Developer/IT Manager/
s/Manager/Director/
[root@node130 sed]# chmod u+x myscript.sed
[root@node130 sed]# ./myscript.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>
[root@node130 sed]# chmod u+x testscript.sed
[root@node130 sed]# cat testscript.sed
#!/bin/sed -nf
/root/p
/nobody/p
/mail/p
[root@node130 sed]# ./testscript.sed /etc/passwd
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

sed直接修改輸入文件 -i選項可以直接修改輸入文件
[root@node130 sed]# sed -i 's/John/Johnny' employee.txt

[root@node130 ibak]# ls
employee.txt
[root@node130 ibak]# sed -ibak 's/John/Johnnyyyy/' employee.txt
[root@node130 ibak]# ls
employee.txt  employee.txtbak
[root@node130 ibak]# cat employee.txt
101,Johnnyyyy Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ibak]# cat employee.txtbak
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
sed附加命令
追加命令(命令a)
命令a可以在指定位置的后面插入新行
語法:
sed '[address] a the-line-to-append' input-file
[root@node130 sed]# sed '2a 203,Jack Johnson,Engineer' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
203,Jack Johnson,Engineer
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 sed]# sed '$a 106,Jack Johnson,Engineer' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
106,Jack Johnson,Engineer
追加多行
[root@node130 sed]# sed '/Jason/a 203,Jack Johnson,Engineer\n204,Mark Smith,Sales Engineer' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
203,Jack Johnson,Engineer
204,Mark Smith,Sales Engineer
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

[root@node130 sed]# sed '/Jason/a\
> 203,Jack Johnson,Engineer\
> 204,Mark Smith,Sales Engineer' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
203,Jack Johnson,Engineer
204,Mark Smith,Sales Engineer
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
插入命令(命令i)
插入命令insert命令和追加命令類似,只是在指定位置之前插入行。
語法:
sed '[address] i the-line-to-insert' input-file
[root@node130 sed]# sed '2i 203,Jack Johnson,Engineer' employee.txt
101,John Doe,CEO
203,Jack Johnson,Engineer
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 sed]# sed '$i 108,Jack Johnson,Engineer' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
108,Jack Johnson,Engineer
105,Jane Miller,Sales Manager
修改命令(命令c)
修改命令changek可以用新行取代舊行
[root@node130 sed]# sed '2c 202,Jack,Johnson,Engineer' employee.txt
101,John Doe,CEO
202,Jack,Johnson,Engineer
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 sed]# sed '/Raj/c 203,Jack Johnson,Engineer\n204,Mark Smith,Sales Engineer' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
203,Jack Johnson,Engineer
204,Mark Smith,Sales Engineer
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 sed]# sed '/Jason/{
> a\
> 204,Jack Johnson,Engineer
> i\
> 202,Mark Smith,Sales Engineer
> c\
> 203,Joe Mason,Sysadmin
> }' employee.txt
101,John Doe,CEO
202,Mark Smith,Sales Engineer
203,Joe Mason,Sysadmin
204,Jack Johnson,Engineer
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
打印不可見的字符
[root@node130 sed]# sed -n 'l' tabfile.txt
fname\tFirst Name$
lname\tLast Name$
mname\tMiddle Name$
[root@node130 sed]# cat tabfile.txt
fname First Name
lname Last Name
mname Middle Name
打印行號
[root@node130 sed]# sed '=' employee.txt
1
101,John Doe,CEO
2
102,Jason Smith,IT Manager
3
103,Raj Reddy,Sysadmin
4
104,Anand Ram,Developer
5
105,Jane Miller,Sales Manager
打印文件總行數
[root@node130 sed]# sed -n '$=' employee.txt
5
sed模擬Unix命令(cat,grep,read)
[root@node130 sed]# cat employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 sed]# sed 's/JUNK/&/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
[root@node130 sed]# 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
[root@node130 sed]# sed 'n' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 sed]# sed 'N' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
grep
[root@node130 sed]# grep Jane employee.txt
105,Jane Miller,Sales Manager
[root@node130 sed]# sed -n 's/Jane/&/p' employee.txt
105,Jane Miller,Sales Manager
[root@node130 sed]# sed -n '/Jane/p' employee.txt
105,Jane Miller,Sales Manager
[root@node130 sed]# head -10 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
[root@node130 sed]# sed '11,$d' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
[root@node130 sed]# sed -n '1,10p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
[root@node130 sed]# sed '10q' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
-n(--quiet,--silent):屏蔽sed的默認輸出
[root@node130 sed]# 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
[root@node130 sed]# sed --quiet '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
[root@node130 sed]# sed --silent '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
-f:調用由多個sed命令組成的sed腳本文件
[root@node130 sed]# cat testscript.sed
#!/bin/sed -f
/root/p
/nobody/p
/mail/p
[root@node130 sed]# sed -n -f testscript.sed /etc/passwd
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
[root@node130 sed]# sed -n --file=testscript.sed /etc/passwd
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
[root@node130 sed]# sed -n -e '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@node130 sed]# sed -n --expression '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@node130 ibak]# sed --in-place=bak 's/102/gooogle/' employee.txt
[root@node130 ibak]# ls
employee.txt  employee.txtbak  employee.txt.orig
[root@node130 ibak]# cat employee.txt
1000001,Johnnyyyyy Doe,CEO
gooogle,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ibak]# sed -ibak -c 's/John/helloworld/' employee.txt
[root@node130 ibak]# ls
employee.txt  employee.txtbak
[root@node130 ibak]# cat employee.txt
101,helloworldyyyy Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ibak]# cat employee.txtbak
101,Johnyyyy Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
打印模式空間命令(n)
命令n打印當前模式空間的內容,然后從輸入文件中讀取下一行。如果在命令執行過程中
遇到n,那么它會改變正常的執行流程。
[root@node130 sed]# sed n employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 sed]# sed -n n employee.txt
sed有兩個內置的存儲空間
模式空間:模式空間用於sed執行的正常流程中。該空間sed內置一個緩沖區,用來存放、修改從文件讀取的內容。
保持空間:保持空間是另外一個緩沖區,用來存放臨時數據。sed可以在保持空間和模式空間交換數據,但是
不能在保持空間上執行普通的sed命令。
每次循環讀取數據過程中,模式空間的內容都會被清空,然而保持空間的內容則保持不變,不會在循環中被刪除。


sed 's/^Managet.*//' employee.txt
[root@node130 sed]# sed -n '/^103/p' employee.txt
103,Raj Reddy,Sysadmin

[root@node130 sed]# sed 's/\/usr\/local\/bin/\/usr/
> \^C
[root@node130 sed]# sed 's/\/usr\/local\/bin/\/usr/
> ^C
[root@node130 sed]# sed 's/\/usr\/local\/bin/\/usr/
> ^C
[root@node130 sed]# sed 's/\/usr\/local\/bin/\/usr\/bin/' path.txt
reading /usr/bin directory
[root@node130 sed]# sed 's|/usr/local/bin|/usr/bin|' path.txt
reading /usr/bin directory
[root@node130 sed]# sed 's^/usr/local/bin^/usr/bin^' path.txt
reading /usr/bin directory
[root@node130 sed]# sed 's@/usr/local/bin@/usr/bin@' path.txt
reading /usr/bin directory
[root@node130 sed]# sed 's!/usr/local/bin!/usr/bin!' path.txt
reading /usr/bin directory
使用替換標志組合
[root@node130 sed]# sed -n 's/manager/Director/igpw output_1_22.txt' employee.txt
102,Jason Smith,IT Director
105,Jane Miller,Sales Director
[root@node130 sed]# cat output_1_22.txt
102,Jason Smith,IT Director
105,Jane Miller,Sales Director

==========================================================================
awk內置變量:
FS-輸入字段分隔符(待處理文件);FS只能在BEGIN區域中使用。
awk 默認的字段分隔符是空格,如果輸入文件中不是一個空格作為字段分隔符,使用-F指定。
OFS-輸出字段分隔符。OFS會被打印在輸出行的字段之間;默認情況下,awk在輸出字段中間以空格分開。
RS-記錄分隔符;RS只能在BEGIN區域中使用。
ORS-輸出記錄分隔符;ORS只能在BEGIN區域中使用。
NR-記錄序號;在block區域時代表記錄的序號(Nunber of the Record)代表awk當前處理的行號;用於END區域時,代表輸入文件的總記錄數。
FIELNAEM-當前處理的文件名;如果awk從標准輸入獲取內容,FILENAME的值將會是“-”。

FS是輸入字段分隔符(待處理文件),OFS是輸出字段分隔符(待輸出文件);OFS會被打印在輸出行的連續的字段之間。
默認情況下awk輸出字段中間以空格分開。 
[root@node130 ~]# sed -n -e '/^root/ p' -e '/^nobody/ p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
nobody:x:99:99:Nobody:/:/sbin/nologin
[root@node130 ~]# sed -n \
> -e '/^root/ p' /etc/passwd \
> -e '/^nobody/ p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
nobody:x:99:99:Nobody:/:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
nobody:x:99:99:Nobody:/:/sbin/nologin
[root@node130 ~]#
[root@node130 ~]# sed -n '{/^root/ p /^nobody/ p}' /etc/passwd
sed: -e expression #1, char 12: extra characters after command
[root@node130 ~]# sed -n '{/^root/ p
> /^nobody/ p
> }'/etc/passwd
sed: -e expression #1, char 25: extra characters after command
[root@node130 ~]# sed /etc/passwd
sed: -e expression #1, char 7: extra characters after command
[root@node130 ~]# sed 'p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
rhel:x:500:500:rhel:/home/rhel:/bin/bash
rhel:x:500:500:rhel:/home/rhel:/bin/bash
kaadmin:x:501:501::/home/kaadmin:/bin/bash
kaadmin:x:501:501::/home/kaadmin:/bin/bash
ilanni:x:502:502::/home/ilanni:/bin/bash
ilanni:x:502:502::/home/ilanni:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:496:493:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:496:493:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
test:x:503:503::/home/test:/bin/bash
test:x:503:503::/home/test:/bin/bash

 

[root@node130 ~]# sed 'p' employee.txt
101,John Doe,CEO
101,John Doe,CEO
102,Jason Smith,IT Manager
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
105,Jane Miller,Sales Manager
[root@node130 ~]# 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
[root@node130 ~]# sed '1~2d' employee.txt
102,Jason Smith,IT Manager
104,Anand Ram,Developer
[root@node130 ~]# sed '2~2d' employee.txt
101,John Doe,CEO
103,Raj Reddy,Sysadmin
105,Jane Miller,Sales Manager
[root@node130 ~]# sed '/Manager/ d' employee.txt
101,John Doe,CEO
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
[root@node130 ~]# sed '/Jason/,4d' employee.txt
101,John Doe,CEO
105,Jane Miller,Sales Manager
[root@node130 ~]# sed '/Raj/,$ d' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
[root@node130 ~]# sed '/Jason/,+2,d' employee.txt
sed: -e expression #1, char 11: unknown command: `,'
[root@node130 ~]# sed '/Jason/,+2 d' employee.txt
101,John Doe,CEO
105,Jane Miller,Sales Manager
[root@node130 ~]# sed '/^$/ d' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# sed '/^#/' employee.txt
sed: -e expression #1, char 4: missing command
[root@node130 ~]# sed '/^#/ d' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 ~]# ls
anaconda-ks.cfg  Documents  employee.txt  hosts   install.log         KingbaseAnalyticsDB-3.0-build1-CENTOS6-x86_64.run  Pictures  Templates  test-script.sed  Videos
Desktop          Downloads  hello         hosts2  install.log.syslog  Music                                              Public    test       tutor
[root@node130 ~]# vim hello
[root@node130 ~]# rm -rf hello
[root@node130 ~]# l\
> ^C

 


[root@node130 awk]# awk '{print}' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 awk]# 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
[root@node130 awk]# cat employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
awk默認使用空格作為分隔符
[root@node130 awk]# awk '{print $2}' employee.txt
Doe,CEO
Smith,IT
Reddy,Sysadmin
Ram,Developer
Miller,Sales
awk指定逗號為分隔符
[root@node130 awk]# awk -F ',' '{print $2}' employee.txt
John Doe
Jason Smith
Raj Reddy
Anand Ram
Jane Miller

[root@node130 awk]# awk -F ',' '{print $2}' employee.txt
John Doe
Jason Smith
Raj Reddy
Anand Ram
Jane Miller
[root@node130 awk]# awk -F "," '{print $2}' employee.txt
John Doe
Jason Smith
Raj Reddy
Anand Ram
Jane Miller
[root@node130 awk]# awk -F , '{print $2}' employee.txt
John Doe
Jason Smith
Raj Reddy
Anand Ram
Jane Miller

$0 代表整條記錄,print默認打印整條記錄
[root@node130 awk]# awk '{print}' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 awk]# awk '{print $0}' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

[root@node130 awk]# awk -F ',' '/Manager/{print $2,$3}' employee.txt
Jason Smith IT Manager
Jane Miller Sales Manager
[root@node130 awk]# awk -F ',' '/^102/{print "Emp id 102 is",$2}' employee.txt
Emp id 102 is Jason Smith

[root@node130 awk]# awk -F ',' '{print $2,$3}' employee.txt
John Doe CEO
Jason Smith IT Manager
Raj Reddy Sysadmin
Anand Ram Developer
Jane Miller Sales Manager
[root@node130 awk]# awk 'BEGIN {FS=","} {print $2,$3}' employee.txt
John Doe CEO
Jason Smith IT Manager
Raj Reddy Sysadmin
Anand Ram Developer
Jane Miller Sales Manager
[root@node130 awk]# awk 'BEGIN{FS=",";\
> print "---------------------------------\nName\tTitle\n-------------------------------"}\
> {print $2,"\t",$3;}\
> END{print "-------------------------------------"}' employee.txt

[root@node130 awk]# cat employee-multiple-fs.txt
101,John Doe;CEO%10000
102,Jason Smith;IT Manager%5000
103,Raj Reddy;Sysadmin%4500
104,Anand Ram;Developer%4500
105,Jane Miller:Sales Manager%3000
[root@node130 awk]# awk 'BEGIN{FS="[,;%]"}{print $2,$3}' employee-multiple-fs.txt
John Doe CEO
Jason Smith IT Manager
Raj Reddy Sysadmin
Anand Ram Developer
Jane Miller:Sales Manager 3000
FS是輸入字段分隔符(待處理文件),OFS是輸出字段分隔符(待輸出文件);OFS會被打印在輸出行的連續的字段之間。
默認情況下awk輸出字段中間以空格分開。
[root@node130 awk]# cat employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@node130 awk]# awk 'BEGIN{FS=",";OFS="\n";ORS="\n---\n"}{print $1,$2,$3}' employee.txt
101
John Doe
CEO
---
102
Jason Smith
IT Manager
---
103
Raj Reddy
Sysadmin
---
104
Anand Ram
Developer
---
105
Jane Miller
Sales Manager
---
NR-(Number of the Record)記錄序號;代表awk當前處理的記錄的行號。
NR非常有用,在循環內部標識記錄序號。用於END區域時,代表輸入文件的總記錄數。
[root@node130 awk]# awk 'BEGIN {FS=","}{print "Emp ld of record number",NR,"is",$1;}END{print "Total number of records:",NR}' employee.txt
Emp ld of record number 1 is 101
Emp ld of record number 2 is 102
Emp ld of record number 3 is 103
Emp ld of record number 4 is 104
Emp ld of record number 5 is 105
Total number of records: 5
FILENAME -當前出來的文件名
[root@node130 awk]# awk -F"," '{print FILENAME, $1,$2,$3}' employee.txt
employee.txt 101 John Doe CEO
employee.txt 102 Jason Smith IT Manager
employee.txt 103 Raj Reddy Sysadmin
employee.txt 104 Anand Ram Developer
employee.txt 105 Jane Miller Sales Manager
FNR-文件中的NR
[root@node130 awk]# awk 'BEGIN {FS=","}{print FIELNAME ":record number",NR,"is",$1;}END{print "Total number of records:",NR}' employee.txt employee-multiple-fs.txt
:record number 1 is 101
:record number 2 is 102
:record number 3 is 103
:record number 4 is 104
:record number 5 is 105
:record number 6 is 101
:record number 7 is 102
:record number 8 is 103
:record number 9 is 104
:record number 10 is 105
Total number of records: 10

[root@node130 awk]# cat employee-sal.txt
101,John Doe,CEO,10000
102,Jason Smith,IT Manager,5000
103,Raj Reddy,Sysadmin,4500
104,Anand Ram,Developer,4500
105,Jane Miller,Sales Manger,3000
[root@node130 awk]# cat total-company-salary.awk
BEGIN{
 FS=",";
 total=0;
}
{
 print $2"'s salary is:"$4;
 total=total+$4;
}
END{
 print "---\nTotal company salary=$"total;
}
[root@node130 awk]# awk -f total-company-salary.awk employee-sal.txt
John Doe's salary is:10000
Jason Smith's salary is:5000
Raj Reddy's salary is:4500
Anand Ram's salary is:4500
Jane Miller's salary is:3000
---
Total company salary=$27000
[root@node130 awk]# awk -F "," '{print $4}' employee-sal.txt
10000
5000
4500
4500
3000
[root@node130 awk]# awk -F "," '{print -$4}' employee-sal.txt
-10000
-5000
-4500
-4500
-3000
[root@node130 awk]# awk -F , '{print -$4}' employee-sal.txt
-10000
-5000
-4500
-4500
-3000
[root@node130 awk]#  awk -F, '{print ++$4}' employee-sal.txt
10001
5001
4501
4501
3001
[root@node130 awk]# awk -F, '{print --$4}' employee-sal.txt
9999
4999
4499
4499
2999
[root@node130 awk]# awk -F, '{print $4++}' employee-sal.txt
10000
5000
4500
4500
3000
[root@node130 awk]# awk -F, '{print $4--}' employee-sal.txt
10000
5000
4500
4500
3000
[root@node130 awk]# awk -F, '{$4++;print $4}' employee-sal.txt
10001
5001
4501
4501
3001
[root@node130 awk]# awk -F, '{$4--;print $4}' employee-sal.txt
9999
4999
4499
4499
2999

[root@node130 awk]# cat negative.txt
-1
-2
-3
[root@node130 awk]# awk '{print +$1}' negative.txt
-1
-2
-3
[root@node130 awk]# awk '{print -$1}' negative.txt
1
2
3
[root@node130 awk]# awk -F ':' '$NF ~/\/bin\/bash/{n++};END {print n}' /etc/passwd
5
[root@node130 awk]# cat items.txt
101,HD Camcorder,Video,210,10
102,Refrigerator,Applian,850,2
103,MP3 Player,Audio,270,15
104,Tennis Racket,Sports,190,20
105,Laser Printer,Office,475,5
[root@node130 awk]# cat arithmetic.awk
BEGIN{
 FS=",";
 OFS=",";
 item_discont=0;
}
{
 item_discount=$4*20/100;
 print $1,$2,$3,$4-item_discount,$5-1;
}
[root@node130 awk]# awk -f arithmetic.awk items.txt
101,HD Camcorder,Video,168,9
102,Refrigerator,Applian,680,1
103,MP3 Player,Audio,216,14
104,Tennis Racket,Sports,152,19
105,Laser Printer,Office,380,4
只打印偶數行
[root@node130 awk]# awk 'NR % 2 == 0' items.txt
102,Refrigerator,Applian,850,2
104,Tennis Racket,Sports,190,20
字符串操作符-空格是連接字符串的操作符
[root@node130 awk]# cat string.awk
BEGIN{
 FS=",";
 OFS=",";
 string1="Audio";
 string2="Video";
 numberstring="100";
 string3=string1 string2;
 print "Concatenate string is:" string3;
 numberstring=numberstring+1;
 print "String to number:" numberstring;
}
[root@node130 awk]# awk -f string.awk items.txt
Concatenate string is:AudioVideo
String to number:101
賦值操作符
[root@node130 awk]# cat assignment.awk
BEGIN{
 FS=",";
 OFS=",";
 total1 = total2 = total3 = total4 = total5 = 10;
 total1 += 5;print total1;
 total2 -= 5;print total2;
 total3 *= 5;print total3;
 total4 /= 5;print total4;
 total5 %= 5;print total5;
}
[root@node130 awk]# awk -f assignment.awk
15
5
50
2
0
[root@node130 awk]# cat items.txt
101,HD Camcorder,Video,210,10
102,Refrigerator,Applian,850,2
103,MP3 Player,Audio,270,15
104,Tennis Racket,Sports,190,20
105,Laser Printer,Office,475,5
[root@node130 awk]# awk -F , 'BEGIN{total=0}{total+=$5}END{print "Total Quantity:"total}' items.txt
Total Quantity:52
[root@node130 awk]# cat items.txt
101,HD Camcorder,Video,210,10
102,Refrigerator,Applian,850,2
103,MP3 Player,Audio,270,15
104,Tennis Racket,Sports,190,20
105,Laser Printer,Office,475,5
[root@node130 awk]# awk -F ',' 'BEGIN {total=0}{total+=NF}END{print total}' items.txt
25
awk比較操作
[root@node130 awk]# awk -F',' '$5<=5' items.txt
102,Refrigerator,Applian,850,2
105,Laser Printer,Office,475,5
[root@node130 awk]# awk -F ',' '$1==103' items.txt
103,MP3 Player,Audio,270,15
[root@node130 awk]# awk -F "," '$1==103 {print $2}' items.txt
MP3 Player
[root@node130 awk]# awk -F "," '$3!="Video"' items.txt
102,Refrigerator,Applian,850,2
103,MP3 Player,Audio,270,15
104,Tennis Racket,Sports,190,20
105,Laser Printer,Office,475,5
[root@node130 awk]# awk -F "," '$3!="Video"{print $2}' items.txt
Refrigerator
MP3 Player
Tennis Racket
Laser Printer
[root@node130 awk]# awk -F"," '$4<900 && $5<=5' items.txt
102,Refrigerator,Applian,850,2
105,Laser Printer,Office,475,5
[root@node130 awk]# awk -F"," '$4<900&&$5<=5{print $2}' items.txt
Refrigerator
Laser Printer
[root@node130 awk]#  awk -F"," '$4<900 || $5<=5' items.txt
101,HD Camcorder,Video,210,10
102,Refrigerator,Applian,850,2
103,MP3 Player,Audio,270,15
104,Tennis Racket,Sports,190,20
105,Laser Printer,Office,475,5
[root@node130 awk]# awk -F"," '$4<900||$5<=5{print$2}' items.txt
HD Camcorder
Refrigerator
MP3 Player
Tennis Racket
Laser Printer
正則表達式操作符
~匹配
!~ 不匹配
==精確匹配
[root@node130 awk]# awk -F"," '$2=="Tennis"' items.txt
[root@node130 awk]# awk -F"," '$2~"Tennis"' items.txt
104,Tennis Racket,Sports,190,20
[root@node130 awk]# awk -F "," '$2 !~ "Tennis"' items.txt
101,HD Camcorder,Video,210,10
102,Refrigerator,Applian,850,2
103,MP3 Player,Audio,270,15
105,Laser Printer,Office,475,5
[root@node130 awk]# awk -F',' '{if ($5<=5)print "Only",$5,"qty of",$2"is available"}' items.txt
Only 2 qty of Refrigeratoris available
Only 5 qty of Laser Printeris available
[root@node130 awk]# awk -F',' '{if(($4>=500&&$4<=1000)&&($5<=5))print "Only",$5,"qty of",$2,"is availiable"}' items.txt
Only 2 qty of Refrigerator is availiable
[root@node130 awk]# cat if-else.awk
BEGIN{
 FS=",";
}
{
 if($5<=5)
  print "Buy More:Order",$2,"immediately!"
 else
  print "Shell More:Give discount on",$2,"immediately!"
}
[root@node130 awk]# awk -f if-else.awk items.txt
Shell More:Give discount on HD Camcorder immediately!
Buy More:Order Refrigerator immediately!
Shell More:Give discount on MP3 Player immediately!
Shell More:Give discount on Tennis Racket immediately!
Buy More:Order Laser Printer immediately!
[root@node130 awk]# awk 'ORS=NR%2?",":"\n"' items.txt
101,HD Camcorder,Video,210,10,102,Refrigerator,Applian,850,2
103,MP3 Player,Audio,270,15,104,Tennis Racket,Sports,190,20
105,Laser Printer,Office,475,5,[root@node130 awk]#
[root@node130 awk]# awk '{if(NR%2==0)ORS="\n";else ORS=",";print}' items.txt
101,HD Camcorder,Video,210,10,102,Refrigerator,Applian,850,2
103,MP3 Player,Audio,270,15,104,Tennis Racket,Sports,190,20
105,Laser Printer,Office,475,5,[root@node130 awk]#
[root@node130 awk]# awk 'BEGIN{while(count++<50)string=string"x";print string}'
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[root@node130 awk]# cat while.awk
{
 i=2;total=0;
 while(i<=NF){
  total = total + $i;
  i++;
 }
 print "Item",$1,":",total,"quantities sold";
}
[root@node130 awk]# awk -f while.awk items-sold.txt
Item 101 : 47 quantities sold
Item 102 : 10 quantities sold
Item 103 : 65 quantities sold
Item 104 : 20 quantities sold
Item 105 : 42 quantities sold

[root@node130 awk]# awk 'BEGIN{
count=1;
do
print "This gets printed at least once";
while(count!=1)
}'
This gets printed at least once
[root@node130 awk]# cat dowhile.awk
{
 i=2;
 total=0;
 do{
  total=total+ $i;
  i++;
 }
 while(i<=NF)
 print "Item",$1,":",total,"quantities sold";
}
[root@node130 awk]# cat items-sold.txt
101 2 10 5 8 10 12
102 0 1 4 3 0 2
103 10 6 11 20 5 13
104 2 3 4 0 6 5
105 10 2 5 7 12 6
[root@node130 awk]# awk -f dowhile.awk items-sold.txt
Item 101 : 47 quantities sold
Item 102 : 10 quantities sold
Item 103 : 65 quantities sold
Item 104 : 20 quantities sold
Item 105 : 42 quantities sold
[root@node130 awk]# echo "1 2 3 4"|awk '{for(i=1;i<=NF;i++)total=total+$i}END{print total}'
10
[root@node130 awk]# cat for.awk
{
 total=0;
 for(i=2;i<=NF;i++)
  total=total+$i
 print "Item",$1,":",total,"quantities sold"
}
[root@node130 awk]# awk -f for.awk items-sold.txt
Item 101 : 47 quantities sold
Item 102 : 10 quantities sold
Item 103 : 65 quantities sold
Item 104 : 20 quantities sold
Item 105 : 42 quantities sold


[root@node130 awk]# cat break.awk
{
 i=2;total=0;
 while(i++<=NF)
 {
  if($i==0)
  {
   print "Item",$0,"had a month without item sold"
   break
  }
 }
}
[root@node130 awk]# awk -f break.awk items-sold.txt
Item 102 0 1 4 3 0 2  had a month without item sold
Item 104 2 3 4 0 6 5 had a month without item sold
[root@node130 awk]# awk 'BEGIN{while(1)print "forever"}'
Continue語句跳過后面剩余的循環部分,立即進入下次循環;
Continue只能用在循環當中。
[root@node130 awk]# cat continue.awk
{
 i=1;total=0;
 while(i++<=NF)
 {
  if(i==1)
   continue
   total=total+$i
 }
 print "Item",$1,":",total,"quantities sold"
}
[root@node130 awk]# awk -f continue.awk items-sold.txt
Item 101 : 47 quantities sold
Item 102 : 10 quantities sold
Item 103 : 65 quantities sold
Item 104 : 20 quantities sold
Item 105 : 42 quantities sold
exit語句-exit命令立即停止腳本的運行,並忽略腳本中其余的命令
exit命令接受一個數字參數作為awk的推出狀態碼,如果不提供參數,默認的狀態碼是0;
[root@node130 awk]# cat exit.awk
{
 i=2;total=0;
 while(i++<=NF)
  if($i==0){
   print "Item",$1,"had a month with no item sold"
   exit
  }
}
[root@node130 awk]# awk -f exit.awk items-sold.txt
Item 102 had a month with no item sold
awk 數組
[root@node130 awk]# cat array-assign.awk
BEGIN{
 item[101]="HD Camcorder";
 item[102]="Refrigerator";
 item[103]="MP3 Player"
 item[104]="Tennis Racket";
 item[105]="Laser Printer";
 item[1001]="Tennis Ball";
 item[55]="Laptop";
 item["na"]="Not Available";
 print item["101"];
 print item["102"];
 print item["103"];
 print item["104"];
 print item["105"];
 print item[1001];
        print item["na"];
}
[root@node130 awk]# awk -f array-assign.awk
HD Camcorder
Refrigerator
MP3 Player
Tennis Racket
Laser Printer
Tennis Ball
Not Available
[root@node130 awk]# cat array-refer.awk
BEGIN{
 x=item[55];
 if(55 in item)
  print "Array index 55 contains",item[55];
 item[101]="HD Camcorder";
 if(101 in item)
  print "Array index 101 contains",item[101];
 if(1010 in item)
  print "Array index 1010 contains",item[1010];
}
[root@node130 awk]# awk -f array-refer.awk
Array index 55 contains
Array index 101 contains HD Camcorder
使用循環遍歷awk數組
語法:for(var in arrayname){
         actinos
   }
-:var 變量名
  in 關鍵字
  arrayname 數組名
  actions 一系列要執行的awk語句;通過把索引值賦給變量var,
  循環體可以把所有語句應用到數組中的所有元素上。
  例如:for(x in item) x:變量名;用來存放數組索引
  [root@node130 awk]# cat array-for-loop.awk
BEGIN{
 item[101]="HD Camcorder";
 item[102]="Refrigerator";
 item[103]="MP3 Player";
 item[104]="Tennis Racket";
 item[105]="Laser Printer";
 item[1001]="Tennis Ball";
 item[55]="Laptop";
 item["no"]="Not Available";

 for(x in item)
  print item[x]
}

[root@node130 awk]# awk -f array-for-loop.awk
Not Available
Laptop
HD Camcorder
Refrigerator
MP3 Player
Tennis Racket
Laser Printer
Tennis Ball
[root@node130 awk]# awk -f array-for-loop.awk
Index no contains Not Available
Index 55 contains Laptop
Index 101 contains HD Camcorder
Index 102 contains Refrigerator
Index 103 contains MP3 Player
Index 104 contains Tennis Racket
Index 105 contains Laser Printer
Index 1001 contains Tennis Ball
刪除數組元素
語法:delete arraynaem[index];
刪除數組內所有元素
for(var in array)
    delete array[var]
gawk中語法:Delete array
[root@node130 awk]# cat array-delete.awk
BEGIN{
 item[101]="HD Camcorder";
        item[102]="Refrigerator";
        item[103]="MP3 Player";
        item[104]="Tennis Racket";
        item[105]="Laser Printer";
        item[1001]="Tennis Ball";
        item[55]="Laptop";
        item["no"]="Not Available";
 
 delete item[102]
 item[103]=""
 delete item[104]
 delete item[1001]
 delete item["na"]
 
 for(x in item)
  print "Index",x,"contains",item[x]
}
[root@node130 awk]# awk -f array-delete.awk
Index no contains Not Available
Index 55 contains Laptop
Index 101 contains HD Camcorder
Index 103 contains
Index 105 contains Laser Printer


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM