split用法
echo hello_xiao_lan | awk '{split($0,b,_);print b[3]}'
//substr用法
awk '{a=substr($1,2);print a}' file2
//求均值
awk '{ sum = $1+sum ;count++ } end {print count, sum,sum/count}' aa.txt
awk '{max=($2>max?$2:max)} end {print max}' test.txt
分组求最大值
awk -f',' '{max[$1]=$2>max[$1]?$2:max[$1]} end {for (i in max) print i,max[i]}' t.txt
分组求和
awk -f',' '{sum[$1]=sum[$1]+$2} end {for (i in sum) print i,sum[i]}' t.txt
//传入变量
awk ' { if ($1 == '$a') print $0 }' test.txt
sub用法:
awk -f/ '{sub(/[a-z]+./,,$3);print $3}' i.txt //用空替换掉连续字符串再加.(第一次匹配上的) 改成gsub所有的都替换掉
//包含某字符的行数
awk 'begin {count=0} {if ($0~/hello/) count++} end {print count}' test.txt
//匹配再打印,两种写法
awk -f | '{if ($3~/cc/) print $0}' aa.txt
awk -f | '$3~/cc/ {print $0}' aa.txt
//next用法,如果调用next,那么next之后的命令就都不执行了
awk '{if(nr==1){next} print $1,$2}' data //第一行的数据不展示
awk -f '$1==i0012{next}{print $0}' file2
//getline用法,与next不同,当调用getline,后面的命令会执行,用下一行数据
awk -f '$1==i0012{getline;print $0}' file2
else用法
awk -f'|' '{if ($1 > 100) {print $1 ;} else {print ok}}' test1.txt
strftime用法
awk -f',' '{ if ($2==98b8e35530ab) print $2,$3,$4,strftime(%y-%m-%d %t,$5)}' test.log | head -3
//打印当前行号,及最后一列
echo 1234/1234/bb234xx/134 | awk -f/ '{print nr,$nf}'
//指定多个分隔符
awk -f'[ :/t]' '{print $1,$3}' test
//不是以hello开头的行
awk '!/^hello/' test.txt
//while用法
echo 1234/1234/bb234xx/134 | awk -f'/' '{ i=1;while(i
//for用法
echo 1234/1234/bb234xx/134 | awk -f'/' '{ for(i=1;i
