ºìÁªLinuxÃÅ»§
Linux°ïÖú

awkÓ÷¨Ð¡½á

·¢²¼Ê±¼ä:2006-04-02 00:41:48À´Ô´:ºìÁª×÷Õß:reing
ÒýÓÃ:
awk Ó÷¨£ºawk ' pattern {action} '

±äÁ¿Ãû º¬Òå
ARGC ÃüÁîÐбäÔª¸öÊý
ARGV ÃüÁîÐбäÔªÊý×é
FILENAME µ±Ç°ÊäÈëÎļþÃû
FNR µ±Ç°ÎļþÖеļǼºÅ
FS ÊäÈëÓò·Ö¸ô·û£¬Ä¬ÈÏΪһ¸ö¿Õ¸ñ
RS ÊäÈë¼Ç¼·Ö¸ô·û
NF µ±Ç°¼Ç¼ÀïÓò¸öÊý
NR µ½Ä¿Ç°ÎªÖ¹¼Ç¼Êý
OFS Êä³öÓò·Ö¸ô·û
ORS Êä³ö¼Ç¼·Ö¸ô·û

1¡¢awk '/101/' file ÏÔʾÎļþfileÖаüº¬101µÄÆ¥ÅäÐС£
awk '/101/,/105/' file
awk '$1 == 5' file
awk '$1 == "CT"' file ×¢Òâ±ØÐë´øË«ÒýºÅ
awk '$1 * $2 >100 ' file
awk '$2 >5 && $2<=15' file
2¡¢awk '{print NR,NF,$1,$NF,}' file ÏÔʾÎļþfileµÄµ±Ç°¼Ç¼ºÅ¡¢ÓòÊýºÍÿһÐеĵÚÒ»¸öºÍ×îºóÒ»¸öÓò¡£
awk '/101/ {print $1,$2 + 10}' file ÏÔʾÎļþfileµÄÆ¥ÅäÐеĵÚÒ»¡¢¶þ¸öÓò¼Ó10¡£
awk '/101/ {print $1$2}' file
awk '/101/ {print $1 $2}' file ÏÔʾÎļþfileµÄÆ¥ÅäÐеĵÚÒ»¡¢¶þ¸öÓò£¬µ«ÏÔʾʱÓòÖмäûÓзָô·û¡£
3¡¢df | awk '$4>1000000 ' ͨ¹ý¹ÜµÀ·û»ñµÃÊäÈ룬È磺ÏÔʾµÚ4¸öÓòÂú×ãÌõ¼þµÄÐС£
4¡¢awk -F "|" '{print $1}' file °´ÕÕеķָô·û¡°|¡±½øÐвÙ×÷¡£
awk 'BEGIN { FS="[: \t|]" }
{print $1,$2,$3}' file ͨ¹ýÉèÖÃÊäÈë·Ö¸ô·û£¨FS="[: \t|]"£©ÐÞ¸ÄÊäÈë·Ö¸ô·û¡£

Sep="|"
awk -F $Sep '{print $1}' file °´ÕÕ»·¾³±äÁ¿SepµÄÖµ×öΪ·Ö¸ô·û¡£
awk -F '[ :\t|]' '{print $1}' file °´ÕÕÕýÔò±í´ïʽµÄÖµ×öΪ·Ö¸ô·û£¬ÕâÀï´ú±í¿Õ¸ñ¡¢:¡¢TAB¡¢|ͬʱ×öΪ·Ö¸ô·û¡£
awk -F '[][]' '{print $1}' file °´ÕÕÕýÔò±í´ïʽµÄÖµ×öΪ·Ö¸ô·û£¬ÕâÀï´ú±í[¡¢]
5¡¢awk -f awkfile file ͨ¹ýÎļþawkfileµÄÄÚÈÝÒÀ´Î½øÐпØÖÆ¡£
cat awkfile
/101/{print "\047 Hello! \047"} --Óöµ½Æ¥ÅäÐÐÒÔºó´òÓ¡ ' Hello! '.\047´ú±íµ¥ÒýºÅ¡£
{print $1,$2} --ÒòΪûÓÐģʽ¿ØÖÆ£¬´òӡÿһÐеÄÇ°Á½¸öÓò¡£
6¡¢awk '$1 ~ /101/ {print $1}' file ÏÔʾÎļþÖеÚÒ»¸öÓòÆ¥Åä101µÄÐУ¨¼Ç¼£©¡£
7¡¢awk 'BEGIN { OFS="%"}
{print $1,$2}' file ͨ¹ýÉèÖÃÊä³ö·Ö¸ô·û£¨OFS="%"£©ÐÞ¸ÄÊä³ö¸ñʽ¡£
8¡¢awk 'BEGIN { max=100 ;print "max=" max} BEGIN ±íʾÔÚ´¦ÀíÈÎÒâÐÐ֮ǰ½øÐеIJÙ×÷¡£
{max=($1 >max ?$1:max); print $1,"Now max is "max}' file È¡µÃÎļþµÚÒ»¸öÓòµÄ×î´óÖµ¡£
£¨±í´ïʽ1?±í´ïʽ2:±í´ïʽ3 Ï൱ÓÚ£º
if (±í´ïʽ1)
±í´ïʽ2
else
±í´ïʽ3
awk '{print ($1>4 ? "high "$1: "low "$1)}' file
9¡¢awk '$1 * $2 >100 {print $1}' file ÏÔʾÎļþÖеÚÒ»¸öÓòÆ¥Åä101µÄÐУ¨¼Ç¼£©¡£
10¡¢awk '{$1 == 'Chi' {$3 = 'China'; print}' file ÕÒµ½Æ¥ÅäÐкóÏȽ«µÚ3¸öÓòÌæ»»ºóÔÙÏÔʾ¸ÃÐУ¨¼Ç¼£©¡£
awk '{$7 %= 3; print $7}' file ½«µÚ7Óò±»3³ý£¬²¢½«ÓàÊý¸³¸øµÚ7ÓòÔÙ´òÓ¡¡£
11¡¢awk '/tom/ {wage=$2+$3; printf wage}' file ÕÒµ½Æ¥ÅäÐкóΪ±äÁ¿wage¸³Öµ²¢´òÓ¡¸Ã±äÁ¿¡£
12¡¢awk '/tom/ {count++;}
END {print "tom was found "count" times"}' file END±íʾÔÚËùÓÐÊäÈëÐд¦ÀíÍêºó½øÐд¦Àí¡£
13¡¢awk 'gsub(/\$/,"");gsub(/,/,""); cost+=$4;
END {print "The total is $" cost>"filename"}' file gsubº¯ÊýÓÿմ®Ìæ»»$ºÍ,ÔÙ½«½á¹ûÊä³öµ½filenameÖС£
1 2 3 $1,200.00
1 2 3 $2,300.00
1 2 3 $4,000.00

awk '{gsub(/\$/,"");gsub(/,/,"");
if ($4>1000&&$4<2000) c1+=$4;
else if ($4>2000&&$4<3000) c2+=$4;
else if ($4>3000&&$4<4000) c3+=$4;
else c4+=$4; }
END {printf "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n",c1,c2,c3,c4}"' file
ͨ¹ýifºÍelse ifÍê³ÉÌõ¼þÓï¾ä

awk '{gsub(/\$/,"");gsub(/,/,"");
if ($4>3000&&$4<4000) exit;
else c4+=$4; }
END {printf "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n",c1,c2,c3,c4}"' file
ͨ¹ýexitÔÚijÌõ¼þʱÍ˳ö£¬µ«ÊÇÈÔÖ´ÐÐEND²Ù×÷¡£
awk '{gsub(/\$/,"");gsub(/,/,"");
if ($4>3000) next;
else c4+=$4; }
END {printf "c4=[%d]\n",c4}"' file
ͨ¹ýnextÔÚijÌõ¼þʱÌø¹ý¸ÃÐУ¬¶ÔÏÂÒ»ÐÐÖ´ÐвÙ×÷¡£


14¡¢awk '{ print FILENAME,$0 }' file1 file2 file3>fileall °Ñfile1¡¢file2¡¢file3µÄÎļþÄÚÈÝÈ«²¿Ð´µ½fileallÖУ¬¸ñʽΪ
´òÓ¡Îļþ²¢Ç°ÖÃÎļþÃû¡£
15¡¢awk ' $1!=previous { close(previous); previous=$1 }
{print substr($0,index($0," ") +1)>$1}' fileall °ÑºÏ²¢ºóµÄÎļþÖØзֲðΪ3¸öÎļþ¡£²¢ÓëÔ­ÎļþÒ»Ö¡£
16¡¢awk 'BEGIN {"date"|getline d; print d}' ͨ¹ý¹ÜµÀ°ÑdateµÄÖ´Ðнá¹ûË͸øgetline£¬²¢¸³¸ø±äÁ¿d£¬È»ºó´òÓ¡¡£
17¡¢awk 'BEGIN {system("echo \"Input your name:\\c\""); getline d;print "\nYour name is",d,"\b!\n"}'
ͨ¹ýgetlineÃüÁî½»»¥ÊäÈëname£¬²¢ÏÔʾ³öÀ´¡£
awk 'BEGIN {FS=":"; while(getline< "/etc/passwd" >0) { if($1~"050[0-9]_") print $1}}'
´òÓ¡/etc/passwdÎļþÖÐÓû§Ãû°üº¬050x_µÄÓû§Ãû¡£

18¡¢awk '{ i=1;while(i awk '{ for(i=1;i type file|awk -F "/" '
{ for(i=1;i { if(i==NF-1) { printf "%s",$i }
else { printf "%s/",$i } }}' ÏÔʾһ¸öÎļþµÄȫ·¾¶¡£
ÓÃforºÍifÏÔʾÈÕÆÚ
awk 'BEGIN {
for(j=1;j<=12;j++)
{ flag=0;
printf "\n%dÔ·Ý\n",j;
for(i=1;i<=31;i++)
{
if (j==2&&i>28) flag=1;
if ((j==4||j==6||j==9||j==11)&&i>30) flag=1;
if (flag==0) {printf "%02d%02d ",j,i}
}
}
}'
19¡¢ÔÚawkÖе÷ÓÃϵͳ±äÁ¿±ØÐëÓõ¥ÒýºÅ£¬Èç¹ûÊÇË«ÒýºÅ£¬Ôò±íʾ×Ö·û´®
Flag=abcd
awk '{print '$Flag'}' ½á¹ûΪabcd
awk '{print "$Flag"}' ½á¹ûΪ$Flag
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ