#分组统计(次数/求和)awk'{a[$1]++}END{for(i in a)print i,a[i]}'file1
unix 931awk'/^unix/{a[$1]++}END{for(i in a)print i,a[i]}'file
unix 9awk'{a[$1]+=$2}END{for(i in a)print i,a[i]}'file0
unix 1930## - 多元分组统计(次数/求和)awk'/^unix/&&$2~/[0-9]/{a[$1,$5]++}END{for(i in a)print i,a[i]}'file
unix STREAM 4
unix DGRAM 4awk'/^unix/&&$2~/[0-9]/{a[$1,$5]+=$2}END{for(i in a)print i,a[i]}'file
unix STREAM 11
unix DGRAM 8## - 多元分组统计(求最值)#求最大awk'/^unix/&&$7!~/[a-zA-Z]/{print }'file|awk'{x=$7;if(a[$1,$5]+0<x+0){a[$1,$5]=x}}END{for(i in a)print i,a[i]}'
unix STREAM 2268
unix DGRAM
#说明:由于非数字开头字符及空值按0计算,所以首次出现负值时会出现空值#修复(过滤空行)awk'/^unix/&&$7!~/[a-zA-Z]/{print }'file|awk'{x=$7;if(a[$1,$5]+0<x+0){a[$1,$5]=x};if(a[$1,$5]==""){a[$1,$5]=$7}}END{for(i in a)print i,a[i]}'
unix STREAM 2268
unix DGRAM -717
#求最小awk'/^unix/&&$7!~/[a-zA-Z]/{print }'file|awk'{x=$7;if(a[$1,$5]+0>x+0){a[$1,$5]=x};if(a[$1,$5]==""){a[$1,$5]=$7}}END{for(i in a)print i,a[i]}'
unix STREAM -2270
unix DGRAM -717
去重
#方式一:awk'!a[$0]++{print}'file#输出省略awk'!a[$1]++{print $1}'file
unix
3#方式二(固定列)awk'{a[$1]++}END{for(i in a) print i}'file
unix
3#过滤空行awk'!/^$/{a[$1]++}END{for(i in a) print i}'file
unix
3
求和/平均/最值
#示例2cat file2
a x 2
a x
b x 0.01
b y 123abc4566
c x a34
d x aa
b x 3
a y 4
a x 3
b y 4
c x 1
a y 9
b x 8
c y 2#求和/平均awk'{sum+=$3}END{print sum}' file2
159.01awk'{sum+=$3}END{print sum/NR}' file2
10.6007#取两位小数awk'{sum+=$3}END{printf("%.2f\n",sum/NR)}' file2
10.60#过滤空行及包含字符的行后awk'!/^$/&&$3!~/[a-zA-Z]/{num++;sum+=$3}END{print num, sum/num}' file2
113.27364#求最大值awk'BEGIN{x=0}{if($3+0>x+0){x=$3}}END{print x}' file2
123abc4566
#过滤字符后处理awk'BEGIN{x=0}$3!~/[a-zA-Z]/{if($3+0>x+0){x=$3}}END{print x}' file2
9#说明:当列值中含有数字+字符时,取字符前数字部分,**如123abc4566作为数字123处理**,其他情况含字符作为0处理***
行转列(列转行)
#示例3cat file4
a bb ccc dddd iiiii
e fff kkkk ggggg oo pp
z xx y uuu
#列数固定awk'{for(i=1;i<=NF;i++) a[NR,i]=$i}END{for(i=1;i<=NF;i++){for(j=1;j<=NR;j++){printf("%s ",a[j,i])};printf("\n")}}' file4
a e z
bb fff xx
ccc kkkk y
dddd ggggg uuu
#列数不固定awk'BEGIN{max=0}{if(NF>max) max=NF;for(i=1;i<=NF;i++) a[NR,i]=$i}END{for(i=1;i<=max;i++){for(j=1;j<=NR;j++){printf("%s ",a[j,i])};printf("\n")}}' file4
a e z
bb fff xx
ccc kkkk y
dddd ggggg uuu
iiiii oo
pp
字段长度
#示例4cat file3
a bb ccc dddd iiiii
e fff kkkk ggggg
z xx y uuu
#输出一行awk'{for(i=1;i<=NF;i++){printf("%s ",NR,length($i));}}' file3
#每个字段的长度(按行列排列返回)#方式1:awk'{x=1;for(i=1;i<=NF;i++){if(x!=NR){printf("\n%s ",length($i))}else printf("%s ",length($i));x=NR}}' file3
1234513451213#方式2:需列数固定awk'{for(i=1;i<=NF;i++) a[NR,i]=length($i)}END{for(j=1;j<=NR;j++){for(i=1;i<=NF;i++){printf("%s ",a[j,i])};printf("\n")}}' file3
123413451213