awk

Basic

  • build-in variables
    • $0 当前记录(这个变量中存放着整个行的内容)
    • $1~$n 当前记录的第n个字段,字段间由FS分隔
    • FS 输入字段分隔符 默认是空格或Tab
    • NF 当前记录中的字段个数,就是有多少列
    • NR 已经读出的记录数,就是行号,从1开始,如果有多个文件话,这个值也是不断累加中。
    • FNR 当前记录数,与NR不同的是,这个值会是各个文件自己的行号
    • RS 输入的记录分隔符, 默认为换行符
    • OFS 输出字段分隔符, 默认也是空格
    • ORS 输出的记录分隔符,默认为换行符
    • FILENAME 当前输入文件的名字

reg

  • [:alnum:] 字母数字字符 [:alpha:] 字母字符

Skills

  • awk FS'' 'condition1{operator1}condition2{operator2}...' filename

  • awk 'NR==1{print $0}'

  • arr=($(awk -F'#' '{print $1,$2,$3,$4}' $conf_file))

  • ps aux | awk 'NR==1{print $0}$3>10{print $0}'

  • awk -F'<|>' '{if(NF>3){print $2 ":" $3}}' /tmp/test.xml parse xml file

  • awk -F'=' '/HWADDR/{print $2}' /etc/sysconfig/network-scripts/ifcfg-eth0

  • ip -o link show eth0 |awk '{ print toupper(gensub(/.*link\/[^ ]* ([[:alnum:]:]*).*/,"\\1", 1)); }'

  • awk 'END{print $0}' /root/test.log print the last line

  • lvdisplay |awk '/LV Name/{n=$3} /Block device/{d=$3; sub(".*:","dm-",d); print d,n;}'
    print the mapping relationship of dm-*

  • statistics

    count the total size of all c files ls -l src/*.c |awk '{sum+=$5} END {print sum}'

  • BEGIN, END

the origin content:

$ 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

the awk script:

$ 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
}

execute the script:

$ 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
  • ls -l |awk ‘$6 == “Dec”‘
  • netstat -apn |grep 3260 |awk '{if($2>0||$3>0){print $0}}' print lines if Recv-Q
    or Send-Q large than 0
  • cat /tmp/.nic |awk '{print $1,$2}'|awk '{aa[$1]=aa[$1]","$2;asorti(aa,tA);}END{for(i in tA)print aa[tA[i]]}'|sed 's/^,//'
  • echo -e "12345\na25\nt123" | awk '/a25/{print a;}{a=$0}' print the previous line of match line

For more

Reference