设有文件grade.txt,
$pg grade.txt
M.Tansley 05/99 48311 Green 8 40 44
J.Lulu 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26
L.Tansley 05/99 4712 Brown-2 12 30 28
显示其内容,并输入到delete_me_and_die中
awk '{print $0}' grade.txt | tee delete_me_and_die,$0表示打印所有域
打印1,4域
awk '{print $1,$4}' grade.txt
显示:
M.Tansley Green
J.Lulu green
P.Bunny Yellow
J.Troll Brown-3
L.Tansley Brown-2
打印报告头
awk 'BEGIN {print "Name Belt\n------------------------"} {print $1"\t" $4}' grade.txt
打印信息尾
awk 'BEGIN {print "Name\n-------"} {print $1} END {"end"}' grade.txt
在第4域匹配含有字符串Brown的记录,~后紧跟正则表达式的模式
awk '{if ($4~/Brown/) print $0}' grade.txt
精确匹配第3域为48的记录,用==号
awk '$3=="48" {print $0}' grade.txt
不配陪,用!~
awk '$0 !~ /Brown/' grade.txt
查看第6域小于第7域的记录,并显示姓名 Try better at the next comp
awk '{if ($6<$7) print $0 "$1 Try better at the next comp"}' grade.txt
查看记录个数,NF为浏览的记录的域个数
awk 'END {print NR}' grade.txt
打印学生所有记录,并带有记录号
awk '{print NF,NR,$0} END {print FILENAME}' grade.txt