来自公众号:入门小站 链接:https://rumenz.com/rumenbiji/linux-awk-skills.html
> awk '{print $1}' rumenz.txt
> awk '{print $1,$2}' rumenz.txt
> awk '{print $NF}' rumenz.txt
> awk 'END{print NR}' rumenz.txt
> awk 'NR==1{print}' rumenz.txt
> sed -n '3,1p' rumenz.txt | awk '{print $2}'
> awk 'NF' rumenz.txt
> awk 'b=!b' rumenz.txt
#
分割后,行长度为3的所有行> awk -F '#' 'if(NF==3){print}' rumenz.txt
> cat /etc/passwd | awk -F ":" '{print $1" : "$7}'
> cat /etc/passwd | awk '{count++}END{ print count}'
> ls -l | awk 'BEGIN{size=0}{size=size+$5}END{print size}'
> ls -l | awk 'BEGIN{size=0}{size=size+$5}END{print size}'
> netstat -tunlp | awk '/^tcp/{++a[$6]}END{for(i in a) print i,a[i]}'
> awk '/^[^$]/ {print $0}' rumenz.txt
> cat 1.txt
1
2
3
> cat 1.txt | awk '{a+=$1}END{print a}'
> cat 1.txt | awk '{a+=$1}END{print a/NR}'
> cat 1.txt | awk 'BEGIN{a=0}{if($1>a) a=$1 fi}END{print a}'
---END---