修改数值域取值--修改M . Ta n s l e y的目前级别分域,使其数值从 4 0减为3 9,使用赋值语句$ 6 = $ 6 - 1,当然在实施修改前首先要匹配域名。
oracle@ser168:~/qilin/demo> awk '{if($1=="M.Tansley") $6=$6-1;print $1,$6,$7}' grade.txt
M.Tansley 39 44
J.Lulu 24 26
P.Bunny 35 28
J.Troll 26 26
L.Tansley 30 28
修改文本域--修改文本域即对其重新赋值。需要做的就是赋给一个新的字符串。在 J . Tr o l l中加入字母,使其成为J . L . Tr o l l,表达式为$ 1 = " J . L . Tr o l l ",记住字符串要使用双秒号( " ") ,并用圆括号括起整个语法。
oracle@ser168:~/qilin/demo> awk '{if($1=="J.Troll")($1="J.L.Troll");print $1}' grade.txt
M.Tansley
J.Lulu
P.Bunny
J.L.Troll
L.Tansley
. 只显示修改记录
oracle@ser168:~/qilin/demo> awk '{if($1=="J.Troll") {($1="J.L.Troll");print $1}}' grade.txt
J.L.Troll
增加列值--
oracle@ser168:~/qilin/demo> awk '(tot+=$6);END{print "Club student total points:"tot}' 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
Club student total points:155
如果文件很大,你只想打印结果部分而不是所有记录,在语句的外面加上圆括号()即可。
oracle@ser168:~/qilin/demo> awk '{(tot+=$6)}; END{print "Club student total points:"tot}' grade.txt
Club student total points:155
使用此模式打印文件名及其长度,然后将各长度相加放入变量 t o t中。
oracle@ser168:~/qilin/demo> ls -l | awk '/^[^d]/ {print $9"\t"$5}{tot+=$5} END {print "TOTAL KB:"tot}'
78
1657
1721
199
199
78
88
TOTAL KB:8116
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/20976446/viewspace-684539/ ,如需转载,请注明出处,否则将追究法律责任。