awk 'BEGIN{} pattern {} END {}' #pattern {} 部分是針對每行進行循環處理的,有pattern表示對匹配到的行處理,沒有pattern表示對全部行處理
[root@test88 etc]# echo -e "line1\nline2" | awk 'BEGIN {print "Start"} {print} END {print "End"}' Start line1 line2 End [root@test88 etc]# echo | awk '{var1="v1";var2="v2";var3="v3";print var1,var2,var3;}' // ,表示用空格分割 v1 v2 v3 [root@test88 etc]# echo | awk '{var1="v1";var2="v2";var3="v3";print var1"-"var2"-"var3;}' // "" 用於拼接字符串 v1-v2-v3
[root@test88 etc]# echo -e "line1 f2 f3\nline2 f4 f5\nline3 f6 f7" | awk '{print "Line no:"NR",No of fields:"NF,"$0="$0,"$1="$1,"$2="$2,"$3="$3}' Line no:1,No of fields:3 $0=line1 f2 f3 $1=line1 $2=f2 $3=f3 Line no:2,No of fields:3 $0=line2 f4 f5 $1=line2 $2=f4 $3=f5 Line no:3,No of fields:3 $0=line3 f6 f7 $1=line3 $2=f6 $3=f7
[root@test88 etc]# echo -e "line1 f2 f3\nline2 f4 f5\nline3 f6 f7" | awk '{print $NF}' // 能夠用$NF表示最後一個字段 f3 f5 f7
[root@test88 etc]# awk 'END{print NR}' file //統計行數
[root@test88 etc]# seq 5 | awk 'BEGIN {sum=0;print "Summation:"}{print $1"+";sum+=$1}END{print "==";print sum}' Summation: 1+ 2+ 3+ 4+ 5+ == 15
[root@test88 etc]# VAR=100000 [root@test88 etc]# echo | awk -v VARIABLE=$VAR '{print VARIABLE}' // 使用外部變量 100000 [root@test88 etc]# var1="Variable1";var2="Variable2" [root@test88 etc]# echo | awk '{print v1,v2}' v1=$var1 v2=$var2 Variable1 Variable2 [root@test88 etc]# awk '{print v1,v2}' v1=$var1 v2=$var2 filename
[root@test88 ~]# awk '{print v1,v2}' v1=$var1 v2=$var2 /etc/hosts
Variable1 Variable2
Variable1 Variable2
Variable1 Variable2
Variable1 Variable2
[root@test88 ~]# cat file.txt 1 2 3 4 5 linux linux win win [root@test88 ~]# awk 'NR < 5' file.txt 1 2 3 4 [root@test88 ~]# awk 'NR==1,NR==4' file.txt 1 2 3 4 [root@test88 ~]# awk '/linux/' file.txt linux linux win [root@test88 ~]# awk '!/linux/' file.txt 1 2 3 4 5 win
[root@test88 ~]# awk -F : '{print $NF}' /etc/passwd /bin/bash /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /bin/sync [root@test88 ~]# awk 'BEGIN{FS=":"}{print $NF}' /etc/passwd /bin/bash /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /bin/sync
[root@test88 ~]# echo | awk '{"grep root /etc/passwd"|getline cmdout;print cmdout}' // getline得到命令輸出並賦給cmdout變量,默認讀取第一行輸出 root:x:0:0:root:/root:/bin/bash
[root@test88 ~]# awk -F: '/root/{for(i=0;i<10;i++) {print $i}}' /etc/passwd root:x:0:0:root:/root:/bin/bash root x 0 0 root /root /bin/bash
[root@test88 ~]# echo "linux" | awk '{print length($0)}' 5
[root@test88 ~]# echo "linux" | awk '{print index($0,"i")}' 2
[root@test88 ~]# seq 9 | awk '{lifo[NR]=$0}END{for(lino=NR;lino>0;lino--){print lifo[lino];}}' //把內容放入數組,結束後再倒着輸出一遍 9 8 7 6 5 4 3 2 1 [root@test88 ~]# seq 9 | tac 9 8 7 6 5 4 3 2 1
[root@test88 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:D2:82:D9 inet addr:10.0.0.88 Bcast:10.0.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fed2:82d9/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:20531 errors:0 dropped:0 overruns:0 frame:0 TX packets:12613 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:26297973 (25.0 MiB) TX bytes:768281 (750.2 KiB) [root@test88 ~]# ifconfig | awk -F "[ :]+" 'NR==2{print $4}' 10.0.0.88
關於awk腳本,咱們須要注意兩個關鍵詞BEGIN和END。linux
假設有這麼一個文件(學生成績表):數組
$ cat score.txt Marry 2143 78 84 77 Jack 2321 66 78 45 Tom 2122 48 77 71 Mike 2537 87 97 95 Bob 2415 40 57 62
咱們的awk腳本以下:bash
$ cat cal.awk #!/bin/awk -f #運行前 BEGIN { math = 0 english = 0 computer = 0 printf "NAME NO. MATH ENGLISH COMPUTER TOTAL\n" printf "---------------------------------------------\n" } #運行中 { math+=$3 english+=$4 computer+=$5 printf "%-6s %-6s %4d %8d %8d %8d\n", $1, $2, $3,$4,$5, $3+$4+$5 } #運行後 END { printf "---------------------------------------------\n" printf " TOTAL:%10d %8d %8d \n", math, english, computer printf "AVERAGE:%10.2f %8.2f %8.2f\n", math/NR, english/NR, computer/NR }
咱們來看一下執行結果blog
$ awk -f cal.awk score.txt NAME NO. MATH ENGLISH COMPUTER TOTAL --------------------------------------------- Marry 2143 78 84 77 239 Jack 2321 66 78 45 189 Tom 2122 48 77 71 196 Mike 2537 87 97 95 279 Bob 2415 40 57 62 159 --------------------------------------------- TOTAL: 319 393 350 AVERAGE: 63.80 78.60 70.00
[root@C ~]# ls -l *.txt | awk '{sum+=$5}END{print sum}' 420
#打印長度大於4的文本行 [root@C ~]# awk 'length>4' test.txt liyong oldboy