-v參數設置內部變量調用外部變量數組
[root@test awk]# a=44 [root@test awk]# echo "ABCD" | awk -v GET_A=$a '{print GET_A}' 44
更復雜的應用:bash
[root@test awk]# cat awk1.sh #!/bin/bash # sort先排序,awk截取第一個字段,uniq取獨 sort -n file | awk -F ':' '{print $1}' | uniq > id.txt for id in `cat id.txt`;do echo "[$id]" # 使用-v設置內部變量,用於打印時的判斷 awk -v id2=$id -F ":" '$1==id2 {print $2}' file done [root@test awk]# cat file 1111111:13443253456 2222222:13211222122 1111111:13643543544 3333333:12341243123 2222222:12123123123 # 效果 [root@test awk]# sh awk1.sh [1111111] 13443253456 13643543544 [2222222] 13211222122 12123123123 [3333333] 12341243123
將兩個文件中,第一列相同的行合併到同一行中函數
[root@test awk]# cat 1.txt 1 aa 2 bb 3 ee 4 ss [root@test awk]# cat 2.txt 1 ab 2 cd 3 ad 4 bd 5 de [root@test awk]# awk 'NR==FNR{a[$1]=$2}NR>FNR{print $0, a[$1]}' 1.txt 2.txt 1 ab aa 2 cd bb 3 ad ee 4 bd ss 5 de 解釋:NR表示讀取的行數,FNR表示讀取的當前行數 因此其實NR==FNR 就表示讀取2.txt的時候。 同理NR>FNR表示讀取1.txt的時候 數組a其實就至關於一個map
awk將文件內容鏈接爲單行工具
[root@test awk]# cat file.txt 13443253456 13211222122 13643543544 12341243123 12123123123 [root@test awk]# awk '{printf("%s+",$0)} END{printf("\n")}' file.txt 13443253456+13211222122+13643543544+12341243123+12123123123+ # %s表明了file.txt內的單行內容# END塊在前面結束後添加換行符
咱們能夠使用sed輕鬆實現全局替換功能,一樣的awk也能夠實現,這裏須要使用一個函數:gsub函數spa
# sed方法比較簡單 [root@test awk]# sed 's/x/passwd/g'p /etc/passwd | head root:passwd:0:0:root:/root:/bin/bash root:passwd:0:0:root:/root:/bin/bash bin:passwd:1:1:bin:/bin:/sbin/nologin bin:passwd:1:1:bin:/bin:/sbin/nologin daemon:passwd:2:2:daemon:/sbin:/sbin/nologin daemon:passwd:2:2:daemon:/sbin:/sbin/nologin adm:passwd:3:4:adm:/var/adm:/sbin/nologin adm:passwd:3:4:adm:/var/adm:/sbin/nologin lp:passwd:4:7:lp:/var/spool/lpd:/sbin/nologin lp:passwd:4:7:lp:/var/spool/lpd:/sbin/nologin # awk使用gsub函數一樣實現相同效果 [root@test awk]# awk -F ':' 'gsub(/x/,"passwd"){print $0}' /etc/passwd | head root:passwd:0:0:root:/root:/bin/bash bin:passwd:1:1:bin:/bin:/sbin/nologin daemon:passwd:2:2:daemon:/sbin:/sbin/nologin adm:passwd:3:4:adm:/var/adm:/sbin/nologin lp:passwd:4:7:lp:/var/spool/lpd:/sbin/nologin sync:passwd:5:0:sync:/sbin:/bin/sync shutdown:passwd:6:0:shutdown:/sbin:/sbin/shutdown halt:passwd:7:0:halt:/sbin:/sbin/halt mail:passwd:8:12:mail:/var/spool/mail:/sbin/nologin operator:passwd:11:0:operator:/root:/sbin/nologin
awk中一些特殊符號能夠使用\的方式轉義,如";可是有些則不能如'3d
# 單引號的使用比較複雜:這個單引號先用雙引號包括,外圍再使用單引號包括。 [root@test awk]# awk -F ':' '{print "This'"'"'s my number: "$2}' file This's my number: 13443253456 This's my number: 13211222122 This's my number: 13643543544 This's my number: 12341243123 This's my number: 12123123123 [root@test awk]# awk -F ':' '{print "This is \"my\" number: "$2}' fileThis is "my" number: 13443253456 This is "my" number: 13211222122 This is "my" number: 13643543544 This is "my" number: 12341243123 This is "my" number: 12123123123
咱們能夠使用grep -E 'a|b' file來過濾出file中知足a或b的內容;一樣,使用awk命令也能夠實現相同功能:awk '/a|b/' filecode
[root@test awk]# cat 1.txt1 aa 2 bb 3 ee 4 ss [root@test awk]# awk '/a|b/ {print $0}' 1.txt 1 aa 2 bb # 還能夠指定分割符,打印指定字段 [root@test awk]# awk -F ' ' '/a|b/ {print $2}' 1.txt aa bb
awk功能十分強大,能夠將字符串按格式輸出:1,01,001,0001,00001,000001,0000001,20171124202055 輸出10行,排序
末尾爲時間(%Y%m%d%H%M%S);這種格式輸出跟C語言很類似。字符串
[root@test awk]# awk 'BEGIN{for(i=1;i<=10;i++)printf("%01d,%02d,%03d,%04d,%05d,%06d,%07d,%d\n",i,i,i,i,i,i,i,strftime("%Y%m%d%H%M%S"))}' 1,01,001,0001,00001,000001,0000001,20171124202055 2,02,002,0002,00002,000002,0000002,20171124202055 3,03,003,0003,00003,000003,0000003,20171124202055 4,04,004,0004,00004,000004,0000004,20171124202055 5,05,005,0005,00005,000005,0000005,20171124202055 6,06,006,0006,00006,000006,0000006,20171124202055 7,07,007,0007,00007,000007,0000007,20171124202055 8,08,008,0008,00008,000008,0000008,20171124202055 9,09,009,0009,00009,000009,0000009,20171124202055 10,10,010,0010,00010,000010,0000010,20171124202055