awk favorite

来源:互联网 发布:泳衣 知乎 编辑:程序博客网 时间:2024/05/17 00:52
#!/bin/awkawk -F '|' '$3=="ECOP_SERVICE_0009" {printf("phone:%s retype:%d retcode:%d msg:%s lineno:%d\n",$4,$6,$7,$8,FNR)}' log.log.2014-09-28 | awk -F ' ' 'BEGIN {total=0} $2=="retype:0" {print $1 $2 $3 $4;total+=1}END{print total}'#awk 'BEGIN{if(ARGC<2){printf("please input log file path\n");exit;}else printf("%s",ARGV[1]);} {printf $1"\n"}'#awk -F '|' 'BEGIN {count=0} {if($6=="0"){count+=1;printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n",substr($1,index($1,"[")+1,23),$2,$3,$4,$5,$6,$7,$8,$9,$10);}} END {print count;}' ECOP_SERVICE_0009 > log.ECOP_SERVICE_0009.0


awk '/sys/ {printf("%s:%d\n",$0,FNR)}' 20141001-sys


awk -F '|' '{arr[$3":"$6]+=1} END {for (i in arr) print i,arr[i]}' log.log.2014-09-28

awk '{var="hello,world";cmd=sprintf("echo %s",var);cmd|getline aaa;print aaa;}'


多个awk通过管道|串联起来针对不同字条处理行总是不行,只能先生成多个中间文件,再写多个处理了。

awk '{n=$1;for(i=2;i<=NF;++i){n=n" "substr($i,0,index($i,"=")-1)};print n;}' log.url.2 > log.url.3

awk -F '&' '{n=$1;for(i=2;i<=NF;++i){n=n" "$i};print n;}' log.url.1 > log.url.2

awk -F '?' '{arr[$1]=arr[$1]" "$2}END{for(i in arr)print i,index(arr[i]," ")?substr(arr[i],2):arr[1]}' log.url > log.url.1


读入第一个文件时,FNR==NR于是执行第一个花括号,当读入第二个文件时FNR==NR为假,执行第二个花括号

http://xxxl.com/item.htm 2204369535341

http://h.com/ore/detail.htm 44116813564105

22043695353

用第二个文件的内容过滤第一个文件的内容

输出 http://xxxl.com/item.htm 2204369535341

awk -F '\t' 'FNR==NR{arr[$2]=$0;next}{if(arr[$0]) print arr[$0];}' url_id_pv_uv.txt id.txt > url_id_pv_uv_filt.txt


将多个标签文件的内容拼上标签合并到一个文件去,文件名是标签名来的

awk 'BEGIN{OFS="\t"}{print FILENAME,$0}' * >  tags




0 0