x^y x的y次幂
x**y 同上
x%y 计算x/y的余数(求模)
x+y x加y
x-y x减y
x*y x乘y
x/y x除y
-y 负y(y的开关符号);也称一目减
++y y加1后使用y(前置加)
y++ 使用y值后加1(后缀加)
–y y减1后使用y(前置减)
y– 使用后y减1(后缀减)
x=y 将y的值赋给x
x+=y 将x+y的值赋给x
x-=y 将x-y的值赋给x
x*=y 将x*y的值赋给x
x/=y 将x/y的值赋给x x%=y 将x%y的值赋给x
x^=y 将x^y的值赋给x
x**=y 将x**y的值赋给x
awk的两道练习题:
1. 将两个文件合并
源文件:
for i in $(ls comb*); do echo "$i"; cat $i; done comb1 --daihao --zhanghao 101 aa 102 bb 103 cc comb2 --daihao --zhanghao 101 aaaa 103 bbbb 104 dddd
awk 'NR==FNR {a[$1]=$2} NR>FNR {printf("%s\t%s\t%s\n",$1,a[$1],$2); delete a[$1]} END {for(i in a) printf("%s\t%s\t%s\n",i,a[i],"")}' comb*
2. 文本统计
源文件:
cat case Mike Harrington:[510] 548-1916:250:100:175 Christian Dobbins:[408] 538-2358:155:90:201 Susan Dalsass:[206] 654-6279:250:60:50 Archie McNichol:[206] 548-1348:250:100:175 Jody Savage:[206] 548-1278:15:188:150 Guy Quigley:[916] 343-6410:250:100:175 Dan Savage:[406] 298-7744:450:300:275 Nancy McNeil:[206] 548-1278:250:80:75 John Goldenrod:[916] 348-4278:250:100:175 Chet Main:[510] 548-5258:50:95:135 Tom Savage:[408] 926-3456:250:168:200 Elizabeth Stachelin:[916] 440-1763:175:75:300
要求输出成:***CAMPAIGN 1998 CONTRIBUTIONS*** ----------------------------------------------------------------------------- NAME PHONE Jan Feb Mar Total Donated ----------------------------------------------------------------------------- Mike Harrington [510] 548-1916 250 100 175 525 Christian Dobbins [408] 538-2358 155 90 201 446 Susan Dalsass [206] 654-6279 250 60 50 360 Archie McNichol [206] 548-1348 250 100 175 525 Jody Savage [206] 548-1278 15 188 150 353 Guy Quigley [916] 343-6410 250 100 175 525 Dan Savage [406] 298-7744 450 300 275 1025 Nancy McNeil [206] 548-1278 250 80 75 405 John Goldenrod [916] 348-4278 250 100 175 525 Chet Main [510] 548-5258 50 95 135 280 Tom Savage [408] 926-3456 250 168 200 618 Elizabeth Stachelin [916] 440-1763 175 75 300 550 ----------------------------------------------------------------------------- SUMMARY ----------------------------------------------------------------------------- The campaign received a total of $6137 for this quarter. The average donation for the 12 contributors was 511.417. The highest contribution was $1025. The lowest contribution was $280.
使用的AWK脚本如下:#!/usr/bin/awk BEGIN{ FS = "[ :]"; print "\t\t\t***CAMPAIGN 1998 CONTRIBUTIONS***"; print "-----------------------------------------------------------------------------"; print "NAME\t\t\tPHONE\t\tJan\tFeb\tMar\tTotal Donated"; print "-----------------------------------------------------------------------------"; sum = 0; highest = 0; lowest = 100000; } { $8 = $5+$6+$7 sum += $8 printf("%s %-12s\t%s %s\t%-3s\t%-3s\t%-3s\t%-4s\n", \ $1, $2, $3, $4, $5, $6, $7, $8) if(highest < $8) highest = $8 if(lowest > $8) lowest = $8 } END{ print "-----------------------------------------------------------------------------"; print "\t\t\t\tSUMMARY"; print "-----------------------------------------------------------------------------"; printf("The campaign received a total of $%s for this quarter.\n", sum); printf("The average donation for the %d contributors was %s.\n", NR, sum/NR); printf("The highest contribution was $%s.\n", highest); printf("The lowest contribution was $%s.\n", lowest); }
注意BEGIN、END模块后的左括号要在同一行。