awk ‘{print NR}‘ aa.txt 变量NR,记录号
awk ‘END {print}‘ aa.txt 最后一行
awk ‘/22$/‘ aa.txt 以22结尾的
awk ‘/^birdman/‘ aa.txt 以birdman开头的的
awk ‘/\<^birdman.*22$\>/‘ aa.txt 以birdman 开头 22结尾的行
awk ‘/\<^birdman.*22$\>/{print $2}‘ aa.txt 以birdman 开头 22结尾的行打印第二列
awk -F‘[._]‘ ‘{print $2}‘ aa.txt 指定.和_为域分隔符
awk ‘/\<^(birdman|aa).*(22|33)$\>/‘ aa.txt 以birdman或aa开头,22或33结尾的行
awk ‘/\<^[78].*[23]$\>/‘ aa.txt
awk ‘/^[89].*3$/‘ aa.txt 匹配以8或9开头 以3结束的行
awk ‘/birdman/{print $5+10}‘ aa.txt 匹配birdman的行的第五列加10
awk ‘/birdman/{print ($5 > 1633 ? "ok" : "error")}‘ aa.txt 匹配birdman的行,如果第五列大于1633则打印?后面的ok,否则大于:后面的error
awk ‘$1 != "birdman_0013"‘ aa.txt $1不等于birdman_0013则打印
awk ‘/^<SITES>/,/^<\/SITES>/‘ /game/server/webapp/client/config.xml 打印以<SITES>开头的行,到</SITES>开头的行的所有内容
sed 可用
sed -n "/<SITES\>/,/<\/SITES>/p" config.xml
awk ‘{if ($3=="/dev/sda2" && $9 =="/usr/local") print $0}‘ 7z.log
awk ‘{if ($3=="/dev/sda2" || $9 =="/usr/local") print $0}‘ 7z.log
awk ‘$1 ~/^[0-9].*[0-3]$/‘ aa.txt
awk 变量
awk ‘/birdman/{count=$5*3;print count}‘ aa.txt 匹配birdman的行,赋予一个变量count,为第三列*3,并打印
awk ‘BEGIN {for(x=0;x<=100;x+=1) print x}‘
a=‘$1,$2,$3,$4,$5‘;TMP=`mktemp`;echo ‘{print ‘$aa‘}‘>$TMP;awk -F"\t" -f $TMP aa.txt
awk ‘{sum+=$1}END{print sum}‘ file 将第一列相加
if else:
awk ‘{if ($5>22) print $5 " bad ";else print $5 " ok"}‘ aa.txt
awk ‘{if ($5>100) {count++;print $5} else {count--; print $1}}‘ aa.txt
if 多重判断
df -h|awk ‘{if ($5>70) {count++;print ""} else if ($5>100) {count++;print "cc"} else { print "cc"}}‘
df -h|grep -P "/$"|sed ‘s/%//‘|awk ‘{if ($4>90) {count++;print "Error"} else if ($4>70) {count++;print "Warning"} else { print "Ok"}}‘
将外部变量传入到 awk 中
将外部变量传入到awk + if 多重判断
SITE=$1
df -h|grep -P "/$"|sed ‘s/%//‘|awk -v SITE=$SITE ‘{if ($4>90) {count++;printf SITE;print "Error"} else if ($4>70) {printf SITE;print
" Warning "} else { printf SITE;print " Ok "}}‘