标签:awk基础用法
继续看看awk吧,本来准备看看就算了,还是记下来以免白费时间
awk
内置变量 NF、NR、FS
[root@localhost /data/backup]#cat > studyawk
name chinese english math average
lucy 80 60 92 773
malei 75 55 80 70
andy 60 90 70 73.33
[root@localhost /data/backup]#sed -i ‘s/773/77.33/g‘ studyawk
[root@localhost /data/backup]#cat studyawk
name chinese english math average
lucy 80 60 92 77.33
malei 75 55 80 70
andy 60 90 70 73.33
准备一下文件,我只是单纯的写错了,然鹅键盘输入下退格变成了^H,正好顺便练习一下替换吧
[root@localhost /data/backup]#awk ‘{print $1 "\t " $5}‘ studyawk
name average
lucy 77.33
malei 70
andy 73.33
显示指定 列(默认以空格或tab分隔的一行的第几字段,可以用如FS=":"来指定以冒号为分隔符),$0代表整行,awk默认首先读入第一行,并且定义$0、$1、$2...内容,然后在根据用户指定命令、条件来处理,我们可以自定义BEGIN、END,来从第一行开始就指定分隔符FS,而NF则代表每行的字段书,NR指相邻的前一命令处理的是第几行数据)
上述"\t"表示字段间用tab分开,事实证明也可以直接键入空格键" "
[root@localhost /data/backup]#awk ‘{print $1 "\t line" NR " " $5 "\t" "sumkinds" NF-2}‘ studyawk
name line1 average sumkinds3
lucy line2 77.33 sumkinds3
malei line3 70 sumkinds3
andy line4 73.33 sumkinds3
显示第一、第五字段,另外显示行号linen以及一共有几科目,减去第一列名字和最后一列均值,科目数为字段数-2.我们实际敲命令时可以得知,awk后接‘{ }‘括号内写命令、参数,要显示的文字、字符和\t要用双引号,而内置变量NR、NF不可以放到双引号中,否则会被视为字符而不是变量。上述命令还发现可以通过NF-2实现运算!!
如下部分为printf的一些练习,之后会结合两者写个命令:
[root@localhost /data/backup]#printf ‘%5s %2i %2i %2i %4.2f‘ $(cat studyawk)
-bash: printf: chinese: invalid number
-bash: printf: english: invalid number
-bash: printf: math: invalid number
-bash: printf: average: invalid number
name 0 0 0 0.00 lucy 80 60 92 77.33malei 75 55 80 70.00 andy 60 90 70 73.33
纳尼??怎么会这样? i是integer数字类型,而第一行的单词都不是数字,所以会报错,而之后是通过合适的数据类型显示的,但是都在一行
[root@localhost /data/backup]#printf ‘%5s %2i %2i %2i %4.2f \n‘ $(cat studyawk)
-bash: printf: chinese: invalid number
-bash: printf: english: invalid number
-bash: printf: math: invalid number
-bash: printf: average: invalid number
name 0 0 0 0.00
lucy 80 60 92 77.33
malei 75 55 80 70.00
andy 60 90 70 73.33
这次顺眼点了吧
[root@localhost /data/backup]#printf ‘%5s %2i %2i %2i %4.2f \n‘ $(cat studyawk | grep -v name)
lucy 80 60 92 77.33
malei 75 55 80 70.00
andy 60 90 70 73.33
符号$()表示执行括号内命令,同样作用还有如下的``反单引号
[root@localhost /data/backup]#printf ‘%5s %2i %2i %2i %4.2f \n‘ `cat studyawk | grep -v name`
lucy 80 60 92 77.33
malei 75 55 80 70.00
andy 60 90 70 73.33
[root@localhost /data/backup]#cat studyawk | awk ‘NR==1{printf "%8s %10s %8s %8s %8s \n",$1,$2,$3,$4,$5};NR>=2{printf "%8s %2d %2d %2d %4.2f\n",$1,$2,$3,$4,$5}‘
name chinese english math average
lucy 80 60 92 77.33
malei 75 55 80 70.00
andy 60 90 70 73.33
[root@localhost /data/backup]#cat studyawk | awk ‘NR==1{printf "%8s %10s %8s %8s %8s \n",$1,$2,$3,$4,$5};NR>=2{printf "%8s %8d %8d %8d %4.2f\n",$1,$2,$3,$4,$5}‘
name chinese english math average
lucy 80 60 92 77.33
malei 75 55 80 70.00
andy 60 90 70 73.33
[root@localhost /data/backup]#cat studyawk | awk ‘NR==1{printf "%8s %10s %8s %8s %8s \n",$1,$2,$3,$4,$5};NR>=2{printf "%8s %8d %8d %8d %8.2f\n",$1,$2,$3,$4,$5}‘
name chinese english math average
lucy 80 60 92 77.33
malei 75 55 80 70.00
andy 60 90 70 73.33
awk的‘‘内还可以做逻辑判断来执行命令,将第一行和其他行设置不同的数据类型就可以正常显示了。可以看到定义的数据长度会直接影响显示的字段间距。
下面我们再每行最后加入一列:
[root@localhost /data/backup]#cat studyawk | awk ‘NR==1{printf "%8s %10s %8s %8s %8s \n",$1,$2,$3,$4,$5,"total"};NR>=2{total=$2+$3+$4;printf "%8s %8d %8d %8d %8.2f\n",$1,$2,$3,$4,$5,total}‘
name chinese english math average
lucy 80 60 92 77.33
malei 75 55 80 70.00
andy 60 90 70 73.33
加了total为啥看不到,没有显示到屏幕
[root@localhost /data/backup]#cat studyawk | awk ‘NR==1{printf "%8s %10s %8s %8s %8s %8s\n",$1,$2,$3,$4,$5,"total"};NR>=2{total=$2+$3+$4;printf "%8s %8d %8d %8d %8.2f %3d\n",$1,$2,$3,$4,$5,total}‘
name chinese english math average total
lucy 80 60 92 77.33 232
malei 75 55 80 70.00 210
andy 60 90 70 73.33 220
printf没有定义显示它的数据类型
[root@localhost /data/backup]#cat studyawk | awk ‘NR==1{printf "%8s %10s %8s %8s %8s %8s\n",$1,$2,$3,$4,$5,"total"};NR>=2{total=$2+$3+$4;printf "%8s %8d %8d %8d %8.2f %8d\n",$1,$2,$3,$4,$5,total}‘
name chinese english math average total
lucy 80 60 92 77.33 232
malei 75 55 80 70.00 210
andy 60 90 70 73.33 220
调整一下长度,美观了点,好像有点太长了,不管了。。。
可能待续...
标签:awk基础用法
原文地址:http://lybliangliang.blog.51cto.com/3808302/1952038