2. 比对两文件中号码异样部分# awk 'NR==FNR&&$2=2&&$3=5 {a[$1]=1};NR>FNR{if( a[$1]!=1) print $0}' file_a file_b >file_c
3. 每天号码访问统计# awk -F "|" '/^133|^153|^180|^189|^131/{a[$1]+=1};END{for(i in a) print i,a[i] | "sort -k2nr"}' file_a file_b >file_c
4. 每天号码访问产品ID统计# awk -F "|" '/^133|^153|^180|^189|^131/{a[$1" "$6]+=1};END{for(i in a) print i,a[i] |"sort -k3nr"}' file(s)
5. 每天产品被访问统计# awk -F "|" '{a[$6]+=1};END{for(i in a) print i,a[i] | "sort -k2nr"}' file_a >file_c
6. AWK多文件合并,根据指定字段列并排序awk '{a[$1]=$1; b[$1,ARGIND]=$2FS$3}END{c=asort(a); for(i=1;i<=c;i++){printf a[i]; for(j=1;j<=ARGIND;j++){d=b[a[i],j]; printf d?" "d:" - -" } print "" }}' file*
合并文件[root@wap1 test]# head t1 t2==> t1 <==userToken=000552a00486fe1d2bb74a1223d5dae6|2012-03-06 00:01:29|contentID=10033000000180000000012176253001|productID=1000000001|purchaseType=3userToken=0006de2e85351ec8d5e8663c3ff1346c|2012-03-06 00:02:00|contentID=10036000000200000000012379916001|productID=1000000001|purchaseType=3userToken=0007d25cf29f47747a5af752cc763a77|2012-03-06 00:02:37|contentID=<null>|productID=<null>|purchaseType=<null>userToken=000a93a8e47048f66574c75b180c0803|2012-03-06 00:03:31|contentID=10044000000260000000012948760001|productID=1000000031|purchaseType=0userToken=000a93a8e47048f66574c75b180c0803|2012-03-06 00:05:31|contentID=10044000000260000000012948760011|productID=1000000234|purchaseType=3userToken=00127f396e0fdf01ab076b93dc013716|2012-03-06 00:03:37|contentID=<null>|productID=<null>|purchaseType=<null>==> t2 <==userToken=000552a00486fe1d2bb74a1223d5dae6|userID=18948822320userToken=0006de2e85351ec8d5e8663c3ff1346c|userID=18974379270userToken=0007d25cf29f47747a5af752cc763a77|userID=18050825086userToken=000a93a8e47048f66574c75b180c0803|userID=18052210879userToken=00127f396e0fdf01ab076b93dc013716|userID=18008013904
# awk -F "|" 'NR==FNR {a[$1]=$2};NR>FNR{if($1 in a) print a[$1],$0}' t2 t1
[ywlscpl@freebsd ~]$ more 1RNC;PRIAM;TG;DIP1;245;5;61;265;4;71;315;8;9[ywlscpl@freebsd ~]$ more 2RNC;TG;DIP;PRIAM1;5;5;2721;4;3;3451;8;4;364[ywlscpl@freebsd ~]$ more 3RNC;TG;PRIAM;DIP1;5;245;21;4;265;81;8;315;3合并结果:RNC;PRIAM;DIP1;245;61;265;71;315;91;272;51;345;31;364;41;245;21;265;81;315;3[ywlscpl@freebsd ~]$ awk -F ';' 'BEGIN{print "RNC;PRIAM;DIP"}FNR==1{for (i=1;i<=NF;i++) a[$i]=i}FNR>1{print $a["RNC"]";"$a["PRIAM"]";"$a["DIP"]}' 1 2 3
原始文件:11111111111|MH@200000400111111111111|ISS@200000000313002211567|MH@100000004313011384877|ISS@200000000013012299799|MH@100000004911111111111|000113001161968|000013001384804|000013001576208|000013001666882|0000合并结果:11111111111|MH@2000004001|ISS@2000000003|000113002211567|MH@100000004313011384877|ISS@200000000013012299799|MH@100000004913001161968|000013001384804|000013001576208|000013001666882|0000[ywlscpl@freebsd ~]$ awk -F "|" '{if(a[$1]>0){a[$1]=a[$1]"|"$2}else{a[$1]=$2}}END{for(i in a)print i"|"a[i]}' urfile*or[ywlscpl@freebsd ~]$ awk -F "|" '{a[$1]=a[$1]", "$2}END{for(i in a)print a[i]}' urfile*or# awk -F "|" 'NR==FNR {a[$1]=$2};NR>FNR{if($1 in a) print a[$1],$0}' t2 t1
文件行列倒置原始文件[root@db ~]# more infile23 345 21 234 45 56 78a b c d e f g例1:[root@db ~]# awk '{for(i=1;i<=NF;i++) c[i" "NR]=$i} END{for(a=1;a<=NF;a++){for(b=1;b<=NR;b++) printf("%-4s ",c[a" "b]);printf("\n")}}' infile23 a 345 b 21 c 234 d 45 e 56 f 78 g 例2:[root@db ~]# awk '{for(i=1;i<=NF;i++) c[i" "NR]=$i} END{for(a=1;a<=NF;a++){for(b=NR;b>0;b--) printf("%-4s ",c[a" "b]);printf("\n")}}' infilea 23 b 345 c 21 d 234 e 45 f 56 g 78
两个文件列重组,打印新文件:(file_a+file_b)awk '{getline l<"file_a";split(l,arr,"\t");print arr[1],$2,arr[2],arr[3],$1,$2,arr[4]}' file_b > file_c 注:arr是文件a中的内容,$i是文件b的内容
对一个文件增加列awk '{printf "%s %s %s 0 0 %s\n",$1,$2,$3,$4}' file(s)
用awk求最大值awk '{max=(max>$1)?max:$1}END{print max}' file(s)
本文出自 “为技术而痴狂” 博客,请务必保留此出处http://ontherd.blog.51cto.com/2062140/1058746