日期:2014-05-16  浏览次数:20562 次

在线等,神奇的{}!!!
[ROOT@LOCALHOST yoyo]# cat lala | awk 'NR==1{printf "%5s %5s %5s %5s %5s\n",$1,$2,$3,$4,"Total"} NR>=2{total=$2+$3+$4} {printf "%5s %5d %5d %5d %5.2f\n",$1,$2,$3,$4,total}'

name 1st 2nd 3th total
name 1 2 3 0.00000
Vbird 2000 3000 4000 9000.00000
Yoyo 2000 3000 5000 10000.00000
lala 3000 2000 4000 9000.00000
  0 0 0 0.00000
0 0 0 0.00000
0 0 0 0.00000


此处,因为觉得total=$2+$3+$4是在第二行开始存在的,所以就放在NR>=2后面,然后分别对total=$2+$3+$4和后面的printf语句都加上{},得到上面的结果,但是多出一行我不想要的,就是:name 1 2 3 0.00000,不知道哪里来的。而且,明明是设置total是第二、三、四个参数相加的总和,如果有name 1 2 3 ,为什么后面的total是0.00000的。。。。不解。。。此处究竟错在哪里。。。呢

----------------------------------

[ROOT@LOCALHOST yoyo]# cat lala | awk 'NR==1{printf "%5s %5s %5s %5s %5s\n",$1,$2,$3,$4,"Total"} NR>=2{total=$2+$3+$4 printf "%5s %5d %5d %5d %5.2f\n",$1,$2,$3,$4,total}'

awk:NR==1{printf "%5s %5s %5s %5s %5s\n",$1,$2,$3,$4,"Total"} NR>=2{total=$2+$3+$4 printf "%5s %5d %5d %5d %5.2f\n",$1,$2,$3,$4,total}
awk:^syntax error

这个的出发点是因为觉得total=$2+$3+$4是在第二行开始存在的,所以直接把total=$2+$3+$4和printf语句包括在跟在NR>=2后面的{}里边,但是却提示出现语法错误,何解?又是{}搞怪么?

-----------------------------------

[ROOT@LOCALHOST yoyo]# cat lala | awk 'NR==1{printf "%5s %5s %5s %5s %5s\n",$1,$2,$3,$4,"Total"} {total=$2+$3+$4 } NR>=2{printf "%5s %5d %5d %5d %5.2f\n",$1,$2,$3,$4,total}'

name 1st 2nd 3th total
Vbird 2000 3000 4000 9000.00000
Yoyo 2000 3000 5000 10000.00000
lala 3000 2000 4000 9000.00000
  0 0 0 0.00000
0 0 0 0.00000
0 0 0 0.00000

这个呢,我是把total=$2+$3+$4 声明在NR>=2前面,理由是在第二行开始之前声明total的值,然后接下来第二行以后的total都是这个值,可以这样理解么,偶是菜鸟。。。。。基础非常不扎实。但是这样却得到了我想要的结果了。。。

------------------------------------

[ROOT@LOCALHOST yoyo]# cat lala | awk 'NR==1{printf "%5s %5s %5s %5s %5s\n",$1,$2,$3,$4,"Total"} total=$2+$3+$4 NR>=2{printf "%5s %5d %5d %5d %5.2f\n",$1,$2,$3,$4,total}'
name 1st 2nd 3th total
Vbird 2000 3000 4000 9000.00000
Yoyo 2000 3000 5000 10000.00000这一行神奇的消失了。。。。
lala 3000 2000 4000 9000.00000
  0 0 0 0.00000
0 0 0 0.00000
0 0 0 0.00000

我把total=$2+$3+$4的大括号给去掉了,得出的结果却是yoyo这一行不见了。。。。为啥米呢?



------解决方案--------------------
。。。一个{}内的语句之间加上;好不好