linux命令-awk


熟悉awk和print语法

fuhui@ubuntu:~$ ls -lh | awk '{print $5 }'

BEGIN和END被包裹在一个单引号内

fuhui@ubuntu:~$ ls -lh | awk 'BEGIN { count=0; print "start:" count}' 'END { print "total:" , count}'
start:0
fuhui@ubuntu:~$ ls -lh | awk 'BEGIN { count=0; print "start:" count} END { print "total:" , count}'
start:0
total: 0

在BEGIN和END之间插入语句

fuhui@ubuntu:~$ ls -lh | awk 'BEGIN { count=0; print "start:" count} {count=count+$5; print count} END { print "total:" , count}'

逻辑段之间使用分号相隔。END和BEGIN后紧跟{}比较美观

awk -F ':' 'BEGIN{ count=0 } { name[count]=$1; count=count+1 } END{ for(i=0;i<nr;i++) print="" i="" name[i]}'="" etc="" passwd

相关内容