awk的常见用法

1. 对一列数据求和

awk  'BEGIN {sum=0} {sum+=$1} END {print sum}'  data.txt


2. 对一列数据求和、求平均值

awk -F ',' 'BEGIN {sum=0 ;count=0} {sum +=$5; count++;} END {print "sum="sum" count="count " avg="sum/count}'


3. 成批rename文件

find * -type f | awk '{print "mv "$1" "$1".new"}'

4. 当一列匹配某一字符串时打印

$ awk '$4 ~/Technology/' employee.txt

200  Jason   Developer  Technology  $5,500

300  Sanjay  Sysadmin   Technology  $7,000

500  Randy   DBA        Technology  $6,000

5. printf

awk '{ printf "%-10s %s\n", $1, $2 }' BBS-list

你可能感兴趣的:(awk)