您好,欢迎来到爱玩科技网。
搜索
您的当前位置:首页awk常见用法总结

awk常见用法总结

来源:爱玩科技网


split用法 echo hello_xiao_lan | awk '{split($0,b,_);print b[3]}' //substr用法 awk '{a=substr($1,2);print a}' file2 //求均 awk '{ sum = $1sum ;count } END {print count, sum,sum/count}' aa.txt awk '{max=($2max?$2:max)} END {print max}' test.t

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

Copyright © 2019- aiwanbo.com 版权所有 赣ICP备2024042808号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务