在各大网站看到的 自己整理的
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释
* awk学习实例 [root@localhost opt]# cat grade.txt M.Tansley 05/99 48311 Green 8 40 44 J.Lulu 06/99 48317 green 9 24 26 P.Bunny 02/99 48 green 12 35 28 J.Troll 07/99 4842 Brown-3 12 26 26 L.Tansley 05/99 4712 Brown-2 12 30 28 [root@localhost opt]# awk '{if($4~/Brown/)print $0}' grade.txt J.Troll 07/99 4842 Brown-3 12 26 26 L.Tansley 05/99 4712 Brown-2 12 30 28 [root@localhost opt]# awk '{if($3~/48/) print $0}' grade.txt M.Tansley 05/99 48311 Green 8 40 44 J.Lulu 06/99 48317 green 9 24 26 P.Bunny 02/99 48 green 12 35 28 J.Troll 07/99 4842 Brown-3 12 26 26 [root@localhost opt]# awk '{if($3==48) print $0}' grade.txt P.Bunny 02/99 48 green 12 35 28 [root@localhost opt]# awk '{if($4 !~ /Brown/) print $0}' grade.txt M.Tansley 05/99 48311 Green 8 40 44 J.Lulu 06/99 48317 green 9 24 26 P.Bunny 02/99 48 green 12 35 28 [root@localhost opt]# awk '{if($6<$7) print $0 "$1 try better at the next comp"}' grade.txt M.Tansley 05/99 48311 Green 8 40 44$1 try better at the next comp J.Lulu 06/99 48317 green 9 24 26$1 try better at the next comp [root@localhost opt]# awk '{if($6<=$7) print $1}' grade.txt M.Tansley J.Lulu J.Troll [root@localhost opt]# awk '{if($6>$7) print $1}' grade.txt P.Bunny L.Tansley [root@localhost opt]# awk '{if($0 ~/[Gg]reen/) print $0 }' grade.txt M.Tansley 05/99 48311 Green 8 40 44 J.Lulu 06/99 48317 green 9 24 26 P.Bunny 02/99 48 green 12 35 28 [root@localhost opt]# awk '{if($1 ~/^...a/) print $0}' grade.txt M.Tansley 05/99 48311 Green 8 40 44 L.Tansley 05/99 4712 Brown-2 12 30 28 [root@localhost opt]# awk '$1 ~/^...a/' grade.txt M.Tansley 05/99 48311 Green 8 40 44 L.Tansley 05/99 4712 Brown-2 12 30 28 [root@localhost opt]# awk '{if ($0 ~/(Yellow|Brown)/) print $0}' grade.txt J.Troll 07/99 4842 Brown-3 12 26 26 L.Tansley 05/99 4712 Brown-2 12 30 28 [root@localhost opt]# awk '/^P/' grade.txt P.Bunny 02/99 48 green 12 35 28 [root@localhost opt]# awk '{if ($1=="P.Bunny" && $4=="Yellow")print $0}' grade.txt [root@localhost opt]# awk '{if ($4 ~/Brown/ || $4=="Yellow")print $0}' grade.txt J.Troll 07/99 4842 Brown-3 12 26 26 L.Tansley 05/99 4712 Brown-2 12 30 28 [root@localhost opt]# awk 'END{print NR}' grade.txt 5 [root@localhost opt]# cat -n grade.txt 1 M.Tansley 05/99 48311 Green 8 40 44 2 J.Lulu 06/99 48317 green 9 24 26 3 P.Bunny 02/99 48 green 12 35 28 4 J.Troll 07/99 4842 Brown-3 12 26 26 5 L.Tansley 05/99 4712 Brown-2 12 30 28 [root@localhost opt]# awk '{print NF,NR,$0}END{print FILENAME}' grade.txt 7 1 M.Tansley 05/99 48311 Green 8 40 44 7 2 J.Lulu 06/99 48317 green 9 24 26 7 3 P.Bunny 02/99 48 green 12 35 28 7 4 J.Troll 07/99 4842 Brown-3 12 26 26 7 5 L.Tansley 05/99 4712 Brown-2 12 30 28 grade.txt [root@localhost opt]# awk '{if (NR>0 && $4 ~/Brown/) print $0}' grade.txt J.Troll 07/99 4842 Brown-3 12 26 26 L.Tansley 05/99 4712 Brown-2 12 30 28 [root@localhost opt]# echo "/usr/local/etc/rc.sybase" | awk -F"/" '{print $NF}' rc.sybase [root@localhost opt]# awk '{name=$1;belts=$4;if(belts ~/Yellow/) print name "is belt" belts}' grade.txt P.Bunnyis beltYellow [root@localhost opt]# awk '{if($6<27)print $0}' grade.txt J.Lulu 06/99 48317 green 9 24 26 J.Troll 07/99 4842 Brown-3 12 26 26 [root@localhost opt]# awk 'BEGIN {BASELINE="27"}{if($6<BASELINE)print $0}' grade.txt J.Lulu 06/99 48317 green 9 24 26 J.Troll 07/99 4842 Brown-3 12 26 26 [root@localhost opt]# awk '{if($1=="M.Tansley") $6=$6-1;print $1,$6,$7}' grade.txt M.Tansley 39 44 J.Lulu 24 26 P.Bunny 35 28 J.Troll 26 26 L.Tansley 30 28 [root@localhost opt]# awk 'BEGIN{print "Name\t Difference"}{if ($6<$7) {$8=$7-$6; print $1,$8}}' grade.txt Name Difference M.Tansley 4 J.Lulu 2 [root@localhost opt]# awk '(tot+=$6);END{print "club student total points :" tot}' grade.txt M.Tansley 05/99 48311 Green 8 40 44 J.Lulu 06/99 48317 green 9 24 26 P.Bunny 02/99 48 Yellow 12 35 28 J.Troll 07/99 4842 Brown-3 12 26 26 L.Tansley 05/99 4712 Brown-2 12 30 28 club student total points :155 [root@localhost opt]# awk '{(tot+=$6)};END{print "club student total points :" tot}' grade.txt club student total points :155 [root@localhost opt]# ls -l | awk '/^[^d]/ {print $9"\t"$5} {tot+=$5}END{print "total KB:" tot}' 111 43 A.txt 43 B.txt 36 C.txt 36 grade.txt 231 mian.sh 257 sou.sh 180 test.sh 151 total KB:5073 [root@localhost opt]# cat grade.txt M.Tansley 05/99 48311 Green 8 40 44 J.Lulu 06/99 48317 green 9 24 26 P.Bunny 02/99 48 Yellow 12 35 28 J.Troll 07/99 4842 Brown-3 12 26 26 L.Tansley 05/99 4712 Brown-2 12 30 28 [root@localhost opt]# awk 'gsub(/4842/,4899) {print $0}' grade.txt J.Troll 07/99 4899 Brown-3 12 26 26 [root@localhost opt]# awk 'BEGIN{print index("Bunny","ny")}' grade.txt 4 [root@localhost opt]# awk '$1=="J.Troll" {print length($1)" " $1}' grade.txt 7 J.Troll [root@localhost opt]# awk 'BEGIN{print length("A FEW GOOD MEN"}' awk: BEGIN{print length("A FEW GOOD MEN"} awk: ^ syntax error [root@localhost opt]# awk 'BEGIN{print length("A FEW GOOD MEN")}' 14 [root@localhost opt]# awk 'BEGIN {print match("ANCD",/d/)}' 0 [root@localhost opt]# awk 'BEGIN {print match("ANCD",/C/)}' 3 [root@localhost opt]# awk '$1=="J.Lulu" {print match($1,"u")}' grade.txt 4 [root@localhost opt]# awk 'BEGIN {print split("123#456#789",myarray,"#")}' 3 [root@localhost opt]# awk '$1=="J.Troll" sub (/26/,"29",$0)' grade.txt [root@localhost opt]# awk '$1=="L.Tansley" {print substr($1,1,5)}' grade.txt L.Tan [root@localhost opt]# awk '{print substr($1,3)}' grade.txt Tansley Lulu Bunny Troll Tansley [root@localhost opt]# awk 'BEGIN{STR="A FEW GOOD MEN"}END{print substr(STR,7)}' grade.txt GOOD MEN [root@localhost opt]# echo "Stand-by" | awk '{print length($0)}' 8 [root@localhost opt]# str="mydoc.txt" [root@localhost opt]# echo $str | awk '{print substr($str,1,5)}' mydoc [root@localhost opt]# awk 'BEGIN{print "\n\May\tday\n\n\May \104\141\171"}' awk: warning: escape sequence `\M' treated as plain `M' May day May Day root@client1 ~]# netstat -ntlp | awk '$0 ~/tcp/{gsub(/:::/,":")split($4,a,":");print a[2]}' 807 111 631 25 22 root@master opt]# ls -l | awk '/^[^d]/ {print $9"\t"$5;tot+=$5} END {print "total KB:" tot}' grade.txt 173 t1 6 vc.sql 1597 total KB:1776 [root@master opt]# ls -l total 32 -rw-r--r-- 1 root root 173 Sep 12 00:29 grade.txt -rw-r--r-- 1 root root 6 Sep 6 19:39 t1 drwxr-xr-x 2 root root 4096 Sep 6 19:47 test -rw-r--r-- 1 root root 1597 Sep 9 23:22 vc.sql [root@master opt]# awk 'BEGIN{print split("123,456,789",may,",");for(i in may) {print i,may[i]}}' 3 1 123 2 456 3 789 root@master opt]# awk '{if($1=="J.Troll") sub(/26/,"29",$0);print $0}' grade.txt M.Tans 5/99 48311 Green 8 40 44 J.Lulu 06/99 48317 green 9 24 26 P.Bunny 02/99 48 Yellow 12 35 28 J.Troll 07/99 4842 Brown-3 12 29 26 L.Tansl 05/99 4712 Brown-2 12 30 28 awk 如题 [root@test ~]# cat a.log 1 a 1 b 2 c 2 d 2 e 3 f 变成如下格式 [root@master opt]# cat a.log |awk '{a[$1]=a[$1]? a[$1]"," $2:$2}END{for(i in a)print i,a[i]}' 1 a,b 2 c,d,e 3 f ########### file: a001 1 100 a001 2 700 a002 1 500 a002 2 100 a003 1 100 形成: a001 2 600 a002 1 400 a003 1 100 sort -k3 file|awk '{a[$1]=$1FS$2;b[$1]=$3-b[$1]}END{for(i in a)print a[i],b[i]}' a001 2 600 a002 1 400 a003 1 100 [root@master opt]# awk 'NR>1{if(x==$1){if($3>z){$3=$3-z}else{$3=z-$3;$2=y}}else{print x,y,z}}{x=$1;y=$2;z=$3}' file a001 2 600 a002 1 400 ############### vim infile t lines init a'101-1 ' b 6 20.00 15.00 '101-1 15.056 0 2 ' 15.056 0 0 path=no 12275 dwt=#.00 ttf=4 7604 9691 dwt=.50 12512 dwt=#.00 9692 12511 6374 6373 7602 dwt=.50 7905 dwt=#.00 7906 9681 需要达到的效果是:凡事以字母开头的比如t lines init a'101-1 '都原封不动的print出来 然后,凡是本行内含有dwt=#0.00的,该行的下一行中的数字+100000 1 [root@master opt]# awk '/dwt=#.00/{print;getline;printf "%9s\n" , $NF+100000;next}1' infile 2 awk '/dwt=#.00/{print;getline;$1+=100000}1' infile ==================================================================================================== [root@L ~]# cat testfile lehih_0006->lehih_0004 teyou_0013->teyou_0001 teyou_0014->teyou_0001 teyou_0016->teyou_0001 teyou_0017->teyou_0002 teyou_0019->teyou_0002 teyou_0018->teyou_0002 以 "->"为FS 如何把第一个字段的内容放入一个awk数组中? 或者说 如何往awk数组中添加元素? [root@master opt]# awk -F"->" '{a[NR]=$1}END{for(i in a)print a[i]}' tfile teyou_0016 teyou_0017 teyou_0019 teyou_0018 lehih_0006 teyou_0013 teyou_0014 ----------------------------------------------------------- 统计文件中个字符或字符串个数 [root@L ~]# cat file A B C S C D E A B S D A D B S A E S C D S A B D 1 awk '{for(i=1;i<=NF;i++)array[$i]++} END{for(k in array)printf("%s\t%d\n",k,array[k])}' file A 5 B 4 C 3 D 5 E 2 S 5 2 [root@master opt]# awk '{for(i=1;i<=NF;i++)array[$i]++} END{for(k in array)print(k,array[k])}' file1 A 5 B 4 C 3 D 5 E 2 S 5 --------------------------------------------------------------------------------------------- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 1.txt 1 4 2 39 3 58 2.txt 1 34 2 76 3 60 现在我想要的结果是: 1 4 34 2 39 76 3 58 60 [root@master opt]# awk 'FNR==NR{a[$1]=$2}NR>FNR{print $0,a[$1]}' 2.txt 1.txt 1 4 34 2 39 76 3 58 60 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 文件内容如下: # 1 2 3 # 4 5 # 6 7 8 9 # 想要的结果如下: 123 45 6789 [root@master opt]# awk -vRS=# -vOFS= 'NF+=0' awk 123 45 6789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 打印文件的第二列到最后一列 [root@master opt]# cat grade.txt M.Tans 5/99 48311 Green 8 40 44 J.Lulu 06/99 48317 green 9 24 26 P.Bunny 02/99 48 Yellow 12 35 28 J.Troll 07/99 4842 Brown-3 12 26 26 L.Tansl 05/99 4712 Brown-2 12 30 28 [root@master opt]# awk '{for(i=2;i<=7;i++)printf ($i"\t");printf("\n")}' grade.txt 5/99 48311 Green 8 40 44 06/99 48317 green 9 24 26 02/99 48 Yellow 12 35 28 07/99 4842 Brown-3 12 26 26 05/99 4712 Brown-2 12 30 28 [root@master opt]# awk '{for(i=2;i<=NF;i++)printf ($i"\t");printf("\n")}' grade.txt 5/99 48311 Green 8 40 44 06/99 48317 green 9 24 26 02/99 48 Yellow 12 35 28 07/99 4842 Brown-3 12 26 26 05/99 4712 Brown-2 12 30 28 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 我想用shell 提取字符串中的数据,原始数据如下 <li><a href="#" key="201112/27/C.QhlYzQ44tTHDccKeQ" tag="0" > </a></li> <li><a href="#" key="201112/27/C.QhlYzQ44tTHDccKeC" tag="1" > </a></li> <li><a href="#" key="201112/27/C.QhlYzQ44tTHDccKeW" tag="2" > </a></li> <li><a href="#" key="201112/27/C.QhlYzQ44tTHDccKeU" tag="3" > </a></li> 提取后呈现 key 201112/27/C.QhlYzQ44tTHDccKeQ tag 0 key 201112/27/C.QhlYzQ44tTHDccKeC tag 1 key 201112/27/C.QhlYzQ44tTHDccKeW tag 2 key 201112/27/C.QhlYzQ44tTHDccKeU tag 3 [root@master opt]# awk '{s=$3" "$4;gsub(/=|\"/," ",s);print s}' data key 201112/27/C.QhlYzQ44tTHDccKeQ tag 0 key 201112/27/C.QhlYzQ44tTHDccKeC tag 1 key 201112/27/C.QhlYzQ44tTHDccKeW tag 2 key 201112/27/C.QhlYzQ44tTHDccKeU tag 3 [root@master opt]# awk -F "[=\" ]+" '{print $4,$5,$6,$7}' data key 201112/27/C.QhlYzQ44tTHDccKeQ tag 0 key 201112/27/C.QhlYzQ44tTHDccKeC tag 1 key 201112/27/C.QhlYzQ44tTHDccKeW tag 2 key 201112/27/C.QhlYzQ44tTHDccKeU tag 3 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( temp.dat 130 2008 2223.08 0.2814 200805092200 79200 20070815032657 133 2008 2184.69 0.5009 200805121400 50400 20071226232905 133 2008 1359.88 0.2354 200805122000 72000 20081105042440 122 2008 2480.08 0.2035 200805010100 3600 20090102084023 133 2008 1828.50 0.2576 200805122000 72000 20090105025641 只输出奇数行 [root@master opt]# awk '{print $0;getline}' temp.dat 130 2008 133 2008 133 2008 122 2008 133 2008 [root@client1 ~]# seq 10 | awk 'i=!(i++)' 1 3 5 7 9 [root@client1 ~]# seq 10 | awk 'i++%2' 2 4 6 8 10 [root@client ~]# seq 10| awk '{getline;print $0}' 2 4 6 8 10 seq 100 | awk 'i=!i' 这条命令能显示1到100之间的奇数,看了不理解,求解释 只输出偶数行 [root@master opt]# awk '{getline;print $0}' temp.dat 2223.08 0.2814 200805092200 79200 20070815032657 2184.69 0.5009 200805121400 50400 20071226232905 1359.88 0.2354 200805122000 72000 20081105042440 2480.08 0.2035 200805010100 3600 20090102084023 1828.50 0.2576 200805122000 72000 20090105025641 [root@master opt]# awk 'i++%2' temp.dat 2223.08 0.2814 200805092200 79200 20070815032657 2184.69 0.5009 200805121400 50400 20071226232905 1359.88 0.2354 200805122000 72000 20081105042440 2480.08 0.2035 200805010100 3600 20090102084023 1828.50 0.2576 200805122000 72000 20090105025641 是我说的不太清楚,我想利用奇数行的第一列,和偶数行,最终一奇一偶两行输出一行 [root@master opt]# awk '{printf NR%2?$1 FS :$0 RS}' temp.dat 130 2223.08 0.2814 200805092200 79200 20070815032657 133 2184.69 0.5009 200805121400 50400 20071226232905 133 1359.88 0.2354 200805122000 72000 20081105042440 122 2480.08 0.2035 200805010100 3600 20090102084023 133 1828.50 0.2576 200805122000 72000 20090105025641 ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( =================================================================== 有文件,比如 a b c d e f g 复制代码 现在要生成如下 a,b,c,d,e,f,g 复制代码 我现在 [root@nagios ~]# awk 'BEGIN{ORS=","}{print $0}' test a,b,c,d,e,f,g, [root@master opt]# awk 'BEGIN{RS="";OFS=","}NF+=0' ta a,b,c,d,e,f,g [root@master opt]# awk -vOFS="," -vRS="" 'NF+=0' ta a,b,c,d,e,f,g ==================================================================== .................................................................... 文本: hash:0af1403ac9021973529b723cf9479633 date:7.6.0.2001 hash:0af1403ac9021973529b723cf9479633 date:7.6.0.2001 hash:f80cf7f9629c91aaa899e2256a6b4dac date:7.6.0.2001 hash:f80cf7f9629c91aaa899e2256a6b4dac date:7.6.0.2001 hash:f80cf7f9629c91aaa899e2256a6b4dac date:7.6.0.2001 hash:f80cf7f9629c91aaa899e2256a6b4dac date:7.6.0.2001 hash:f80cf7f9629c91aaa899e2256a6b4dac date:7.6.0.2001 hash:f80cf7f9629c91aaa899e2256a6b4dac date:7.6.0.2001 hash:f80cf7f9629c91aaa899e2256a6b4dac date:7.6.0.2001 hash:f80cf7f9629c91aaa899e2256a6b4dac date:7.6.0.2009 根据date统计hash的个数,重复的只算一个,得到结果: 7.6.0.2001 2 7.6.0.2009 1 awk -F"[ :]" '!a[$2,$NF]++{b[$NF]++}END{for(i in b)print i,b[i]}' file .................................................................... ************************************************ 记录平时需要用到的awk命令1 ) 删除空行 1 awk NF 2 awk '!/^$/' 去掉重复行 awk '!a[$1]++' 去除第一列重复行 ?????????????????????????????????? str=" 1 2 3 4 5 6 7 8 9 " 打印成如下格式: 1 2 3 4 5 6 7 8 9 [root@master opt]# echo "11_22_33_44_55_66_77_88"_99|awk -vRS="_" 'ORS=NR%3?" ":"\n"' 11 22 33 44 55 66 77 88 99 [root@master opt]# echo "1 2 3 4 5 6 7 8 9"|awk -vRS=" " 'ORS=NR%3?" ":"\n"' 1 2 3 4 5 6 7 8 9 ?????????????????????????????????? >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> [root@master opt]# cat atest a,b,c,d e,f,g,h i,j,k,l [root@master opt]# awk -vRS="[,\n]" 'NF+=0' atest a b c d e f g h i j k l [root@master opt]# cat a1 a,b,c,d,e,f,g,h,i,j,k [root@master opt]# awk -vRS="," 'NF+=0' a1 a b c d e f g h i j k >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< etstat 监控端口查出的状态进行IP 和状态的统计,并排序 tcp 0 0 127.0.0.1:31001 10.156.177.88:3391 TIME_WAIT tcp 0 0 127.0.0.1:31001 10.165.176.249:1702 TIME_WAIT tcp 0 0 127.0.0.1:31001 10.113.179.124:2805 TIME_WAIT tcp 0 0 127.0.0.1:31001 10.111.178.157:1292 ESTABLISHED tcp 0 0 127.0.0.1:31001 10.38.178.189:1380 TIME_WAIT 期望得到的结果为: ip(第五列) 状态 出现次数 10.156.177.88 TIME_WAIT 1 [root@master opt]# cat file3 tcp 0 0 127.0.0.1:31001 10.156.177.88:3391 TIME_WAIT tcp 0 0 127.0.0.1:31001 10.165.176.249:1702 TIME_WAIT tcp 0 0 127.0.0.1:31001 10.113.179.124:2805 TIME_WAIT tcp 0 0 127.0.0.1:31001 10.111.178.157:1292 ESTABLISHED tcp 0 0 127.0.0.1:31001 10.38.178.189:1380 TIME_WAIT [root@master opt]# awk '{split($5,a,":");b[a[1]" "$6]++}END{for(i in b)print i,b[i]}' file3 10.38.178.189 TIME_WAIT 1 10.113.179.124 TIME_WAIT 1 10.165.176.249 TIME_WAIT 1 10.156.177.88 TIME_WAIT 1 10.111.178.157 ESTABLISHED 1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM 统计没歌词出现的次数 [root@master opt]# cat word data we me and you data we me and you data we me and you data we me and you data we me and you data we me and you [root@master opt]# awk '{for(i=1;i<=NF;i++)a[$i]++}END{for(i in a)print i,a[i]}' word me 6 data 6 and 6 you 6 we 6 MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM 实现如下格式 [root@client1 html]# echo "1,2,3,4,5,6"|awk -F "," '{for(i=2;i<=NF;i++) print $1","$i}' 1,2 1,3 1,4 1,5 1,6 ?: 运算符解释 条件成立取?后面的值 即条件为真 为假取:后面的值 0为假,则!0即为真 [gaoming@localhost ~]$ awk 'BEGIN{a="b";print a=="b"?"ok":"err";}' ok 文件内容如下 [root@client1 tmp]# cat b.txt 20121001 testing 20121001 test.192.168.0.1 20121001 time:20 实现格式如下 20121001 192.168.0.1,20 [root@client1 tmp]# cat b.txt | awk 'NR>1{a[$1]=a[$1]? a[$1]"," $2:$2}END{for (i in a)print i,a[i]}' | sed 's/[a-z]//g' 20121001 .192.168.0.1,:20 awk '{sub(/[^0-9]+/,"",$2);if($2~/[0-9]/)a[$1]=a[$1]?a[$1]", "$2:$2}END{for(i in a)print i,a[i]}' file 解释贴经典啊 http://bbs.chinaunix.net/thread-3769891-2-12.html [root@test ~]# cat a.log 1 a 1 b 2 c 2 d 2 e 3 f [root@test ~]# cat a.log |awk '{a[$1]=a[$1] ?a[$1]","$2:$2}END{for(i in a)print i,a}' 1 a,b 2 c,d,e 3 f 判断是对 a[$1]的判断,而不是 a[$1]=a[$1]的判断。 01.第一行时, a[$1]为空为假,所以, 执行a[$1]=a[$1]?a[$1] "," $2:$2 后, a[$1]为$2, 即a[1]=a. 02. 03.第二行时,a[$1]不为空为真,所以,执行a[$1]=a[$1]?a[$1] "," $2:$2 后, a[$1]为原来的a[$1] “," $2,即a[1]为 a,b 如题http://bbs.chinaunix.net/thread-3763442-1-22.html A:name B:number C:123456 D:654321 E: 0721 00:00:00 [ :]匹配空格或: + 匹配前面的子表达式 1 次或多次 [root@client1 tmp]# awk -F '[ :]+' '{print $2}' eg name number 123456 654321 如题http://bbs.chinaunix.net/thread-3760162-1-26.html -rw-a-- 2.0 fat 3156 b- defN 11-Jul-12 11:11 WEB-INF/classes/com/channelsoft/helpdesk/business/action/CallUpSgAction.class -rw-a-- 2.0 fat 8632 b- defN 11-Jul-12 10:59 WEB-INF/classes/com/channelsoft/helpdesk/business/util/SGClient.class -rw-a-- 2.0 fat 16720 t- defN 11-Jul-12 10:26 WEB-INF/classes/struts-business.xml -rw-a-- 2.0 fat 4 t- stor 11-Jul-12 11:54 svn_version.txt 如上信息 可以将上面信息当成file awk 怎么能获取这样的结果: CallUpSgAction.class SGClient.class struts-business.xml svn_version.txt 也就是说,获取每一行最后的那个文件名? [root@client1 tmp]# awk -F"[ /]" '{print $NF}' file1 CallUpSgAction.class SGClient.class struts-business.xml svn_version.txt http://bbs.chinaunix.net/thread-1679416-1-30.html [root@localhost test]# cat t1 a 1 b 45 c 90089809 d dsd [root@localhost test]# awk '{printf "%s",$1}' t1 abcd 如题 得到如下结果 [root@client1 tmp]# cat port port,0 mac,0017.0850.1530,ip-address,192.168.0.199 port,1 mac,0011.2557.972F,ip-address,192.168.0.166 port,2 mac,4016.9F0C.BB39,ip-address,192.168.0.25 mac,0013.02A3.F918,ip-address,192.168.0.27 mac,CC52.AF28.D6EC,ip-address,192.168.0.74 [root@client1 tmp]# awk '/^port,/{p=$0;next}{print $0","p}' port mac,0017.0850.1530,ip-address,192.168.0.199,port,0 mac,0011.2557.972F,ip-address,192.168.0.166,port,1 mac,4016.9F0C.BB39,ip-address,192.168.0.25,port,2 mac,0013.02A3.F918,ip-address,192.168.0.27,port,2 mac,CC52.AF28.D6EC,ip-address,192.168.0.74,port,2 http://bbs.chinaunix.net/thread-3705860-1-62.html
*/