ºìÁªLinuxÃÅ»§
Linux°ïÖú
µ±Ç°Î»ÖÃ: ºìÁªLinuxÃÅ»§ > Debian

Îı¾´¦ÀíÓïÑÔawkÔÚÏßʵÀýÈëÃŽ̳Ì

·¢²¼Ê±¼ä:2009-07-12 21:41:03À´Ô´:ºìÁª×÷Õß:Áõ³å
[i=s] ±¾Ìû×îºóÓÉ Áõ³å ÓÚ 2009-7-13 20:22 ±à¼­ [/i]

awkÔÚÏßʵÀýÈëÃÅ½Ì³Ì µÚÒ»²¿·Ö
------Ò»ÖÖÃû³ÆºÜÆæÌصÄÓÅÐãÓïÑÔ½éÉÜ

Awk ÊÇÒ»ÖÖÃû³ÆÆæ¹Öµ«¹¦ÄÜÇ¿´óµÄÓïÑÔ¡£±¾ÏµÁÐÎÄÕÂÊÇÒ»¸ö°üº¬Èý²¿·Ö(·Ö±ð·¢²¼ÖÁ±¾Ìû1#, 2#, 3#Â¥)¡£±¾ÎĽ«Ê¹ÄúѸËÙÕÆÎÕ awk ±à³Ì¼¼ÇÉ¡£Ëæ×ű¾ÏµÁеĽøÕ¹£¬½«ÌÖÂÛ¸ü¸ß¼¶µÄÖ÷Ì⣬×îºó½«ÑÝʾһ¸öÕæʵµÄ¸ß¼¶ awk Ó¦ÓóÌÐò¡£

º´ÎÀ awk

ÔÚÕâһϵÁеÄÎÄÕÂÖУ¬ÎÒ½«Ê¹Äú³ÉΪ¾«Í¨ awk µÄ±à³ÌÈËÔ±¡£ÎÒ³ÐÈÏ£¬awk ²¢Ã»ÓÐÒ»¸ö·Ç³£ºÃÌýÇÒÓַdz£ ¡°Ê±÷Ö¡± µÄÃû×Ö¡£awk µÄ GNU°æ±¾£¨½Ð×÷ gawk£©ÌýÆðÀ´·Ç³£¹ÖÒì¡£ÄÇЩ²»ÊìϤÕâÖÖÓïÑÔµÄÈË¿ÉÄÜÌý˵¹ý"awk"£¬¶øÇÒ¿ÉÄÜÈÏΪËüÊÇÒ»×éÂäÎéÇÒ¹ýʱµÄ»ìÂÒ´úÂë¡£ËüÉõÖÁ»áʹ×ѧµÄ UNIX ȨÍþÏÝÓÚ»ìÂҵıßÔµ£¨Ê¹Ëû²»¶ÏµØ·¢³ö "kill -9!"ÃüÁ¾ÍÏóʹÓÿ§·È»úÒ»Ñù£©¡£

µÄÈ·£¬awk ûÓÐÒ»¸ö¶¯ÌýµÄÃû×Ö¡£µ«ËüÊÇÒ»ÖֺܰôµÄÓïÑÔ¡£awkÊʺÏÓÚÎı¾´¦ÀíºÍ±¨±íÉú³É£¬Ëü»¹ÓÐÐí¶à¾«ÐÄÉè¼ÆµÄÌØÐÔ£¬ÔÊÐí½øÐжàÖÖ·½Ê½µÄ±à³Ì¡£ÓëijЩÓïÑÔ²»Í¬£¬awkµÄÓï·¨½ÏΪ³£¼û¡£Ëü½è¼øÁËijЩÓïÑÔµÄһЩ¾«»ª²¿·Ö£¬Èç C ÓïÑÔ¡¢python ºÍ bash£¨ËäÈ»ÔÚ¼¼ÊõÉÏ£¬awk ±È python ºÍ bashÔç´´½¨£©¡£awk ÊÇÒ»ÖÖÒ»µ©Ñ§»á¾Í»á³ÉΪÄúÕ½ÂÔ´úÂë¿âµÄÖØÒª²¿·ÖµÄÓïÑÔ¡£

µÚÒ»¸ö Awk

ÄúÓ¦¸Ã»á¿´µ½ /ect/passwd ÎļþÖеÄÄÚÈÝ£¬±¾ÎÄʹÓøÃÎļþÀ´½âÊÍ awk µÄ¹¤×÷Ô­Àí¡£µ±µ÷Óà awk ʱ£¬ÎÒÃÇÖ¸¶¨/etc/passwd ×÷ΪÊäÈëÎļþ¡£Awk ÔÚÖ´ÐÐÆÚ¼ä¶Ô /etc/passwd ÎļþÖеÄÿһÐÐÒÀ´ÎÖ´ÐÐ print ÃüÁî¡£ËùÓÐÊä³ö¶¼·¢Ë͵½stdout£¬¿ÉÒԵõ½ÀàËÆ cat ÃüÁîµÄ½á¹û¡£

ÏÖÔÚ½âÊÍ´úÂë¿é { print }¡£ÔÚ Awk ÖУ¬»¨À¨ºÅÓÃÓÚ½«´úÂë·Ö¿é£¬ÕâÓë C ÓïÑÔÀàËÆ¡£ÎÒÃǵĴúÂë¿éÖÐÖ»ÓÐÒ»Ìõ print ÃüÁî¡£ÔÚ Awk ÖУ¬µ± print ÃüÁîµ¥¶À³öÏÖʱ£¬½«´òÓ¡µ±Ç°ÐеÄÈ«²¿ÄÚÈÝ¡£¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:$ awk '{ print $0 }' /etc/passwd
ÔÚ Awk ÖУ¬±äÁ¿ $0 ±íʾÕû¸öµ±Ç°ÐУ¬Òò´Ë print ºÍ print $0 µÄ×÷ÓÃÍêÈ«Ïàͬ¡£¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:$ awk '{ print "" }' /etc/passwd
¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:$ awk '{ print "hiya" }' /etc/passwd
ÔËÐиýű¾£¬ÆÁÄ»ÉϽ²ÏÔʾ¶àÐС£

¶à¸ö×Ö¶Î

print $1¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:$ awk -F":" '{ print $1 $3 }' /etc/passwd
ÒýÓÃ:[indent]halt7
operator11
root0
shutdown6
sync5
bin1
....etc.[/indent]
print $1 $3¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:$ awk -F":" '{ print $1 " " $3 }' /etc/passwd
$1 $3¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:$ awk -F":" '{ print "username: " $1 "\t\tuid:" $3 }' /etc/passwd
ÒýÓÃ:[indent]username: halt uid:7
username: operator uid:11
username: root uid:0
username: shutdown uid:6
username: sync uid:5
username: bin uid:1
....etc.[/indent]
Íⲿ½Å±¾¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:BEGIN {
FS=":"
}
{ print $1 }
Á½ÖÖ·½·¨µÄÇø±ðÔÚÓÚÈçºÎÉèÖÃ×ֶηָô·û¡£Ôڸýű¾ÖУ¬£¨Í¨¹ýÉèÖà FS ±äÁ¿£©ÔÚ´úÂëÖÐÖ¸¶¨×ֶηָô·û£¬¶øǰһʾÀýͨ¹ýÔÚÃüÁîÐÐÏò awk ´«µÝ-F":" Ñ¡ÏîÀ´ÉèÖà FS¡£Ò»°ã¶øÑÔ£¬×îºÃÔڽű¾ÄÚ²¿ÉèÖÃ×ֶηָô·û£¬ÒòΪÕâÑù¿ÉÒÔÉÙÊäÈëÒ»¸öÃüÁîÐвÎÊý¡£±¾ÎÄÉÔºó½«ÉîÈë½²½â FS ±äÁ¿¡£

BEGIN Óë END ´úÂë¿é

ͨ³££¬awk »áÕë¶Ôÿ¸öÊäÈëÐÐÖ´ÐÐÒ»´Îÿ¸ö´úÂë¿é¡£µ«ÊÇ£¬ÔÚÐí¶à±à³ÌÇéÐÎÏ£¬¿ÉÄÜÐèÒªÔÚ awk ¿ªÊ¼´¦ÀíÊäÈëÎļþµÄÎı¾Ö®Ç°Ö´Ðгõʼ»¯´úÂë¡£¶ÔÕâÖÖÇé¿ö£¬awk Ö§³Ö¶¨Òå BEGIN ´úÂë¿é¡£Ç°Ò»Ê¾ÀýʹÓÃÁËÕâÖÖ´úÂë¿é¡£ÒòΪ BEGIN ´úÂë¿éÔÚ awk¿ªÊ¼´¦ÀíÊäÈëÎļþ֮ǰִÐУ¬Òò´ËËüÊdzõʼ»¯ FS£¨×ֶηָô·û£©±äÁ¿¡¢´òӡҳü»òÕß³õʼ»¯ÔÚºóÐø³ÌÐòÖн«ÒªÒýÓõÄÆäËûÈ«¾Ö±äÁ¿µÄ¾ø¼ÑλÖá£

ÁíÍ⣬awk »¹ÌṩÁËÁíÒ»ÖÖ³ÆΪ END µÄרÓôúÂë¿é¡£ÔÚÊäÈëÎļþµÄËùÓÐÐд¦ÀíÍê±ÏÖ®ºó£¬awk Ö´ÐÐÕâ¸ö´úÂë¿é¡£Í¨³££¬END ´úÂë¿éÓÃÓÚ½øÐÐ×îÖÕ¼ÆËã»òÕß´òÓ¡Ó¦¸ÃÔÚÊä³öÁ÷½áβ´¦³öÏֵĻã×ÜÐÅÏ¢¡£

ÕýÔò±í´ïʽÓë´úÂë¿é¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:/foo/ { print }
¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:/[0-9]+\.[0-9]*/ { print }
±í´ïʽÓë´úÂë¿é
fred print¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:$1 == "fred" { print $3 }
root¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:$5 ~ /root/ { print $3 }
Ìõ¼þÓï¾ä¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:if
{
if ( $5 ~ /root/ ) {
print $3
}
}
Á½¸ö½Å±¾µÄ×÷ÓÃÏàͬ¡£µÚÒ»¸öʾÀýµÄ²¼¶û±í´ïʽλÓÚ´úÂë¿éÍ⣬¶øµÚ¶þ¸öʾÀýµÄ´úÂë¿é»áÕë¶Ôÿ¸öÊäÈëÐÐÖ´ÐÐÒ»´Î£¬±¾ÎÄʹÓà if Óï¾äÓÐÑ¡ÔñµØÖ´ÐдòÓ¡ÃüÁî¡£Á½ÖÖ·½·¨¶¼¿ÉÒÔʹÓ㬿ÉÒÔÑ¡ÔñÓë½Å±¾µÄÆäËû²¿·Ö×îÆ¥ÅäµÄ·½·¨¡£
if if¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:{
if ( $1 == "foo" ) {
if ( $2 == "foo" ) {
print "uno"
} else {
print "one"
}
} else if ($1 == "bar" ) {
print "two"
} else {
print "three"
}
}
if¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:! /matchme/ { print $1 $3 $4 }
¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:{
if ( $0 !~ /matchme/ ) {
print $1 $3 $4
}
}
Á½¸ö½Å±¾¶¼»áÖ»Êä³ö²» °üº¬ matchme ×Ö·ûÐòÁеÄÐС£Ò²¿ÉÒÔÑ¡Ôñ×îÊʺÏÄúµÄ´úÂëµÄ·½·¨¡£ËüÃǵŦÄÜÍêÈ«Ïàͬ¡£¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:( $1 == "foo" ) && ( $2 == "bar" ) { print }
¸ÃʾÀýÖ»´òÓ¡µÚÒ»¸ö×ֶεÈÓÚ foo ÇÒ µÚ¶þ×ֶεÈÓÚ bar µÄÐС£

ÊýÖµ±äÁ¿£¡

ÔÚ BEGIN ´úÂë¿éÖУ¬ÎÒÃǽ«ÕûÐͱäÁ¿ x ³õʼ»¯ÎªÁã¡£ÕâÑù£¬awk ÿ´ÎÓöµ½¿Õ°×ÐÐʱ¶¼½«Ö´ÐÐ x=x+1 Óï¾ä£¬µÝÔö x Öµ¡£ÔÚËùÓÐÐж¼´¦ÀíÍê±ÏÖ®ºó£¬awk Ö´ÐÐ END ´úÂë¿é£¬²¢´òÓ¡×îÖյĻã×ÜÐÅÏ¢£¬ÒÔÏÔʾËüÕÒµ½µÄ¿Õ°×ÐÐÊý¡£

×Ö·û´®»¯±äÁ¿ÒýÓÃ:[indent]2.01[/indent]
1.01 x $( ) 1.01¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:{ print ($1^2)+1 }
ÉÔ×öÊÔÑé¾Í¿ÉÒÔ·¢ÏÖ£¬Èç¹ûÌض¨±äÁ¿²»°üº¬Ð§Êý×Ö£¬ÄÇô awk ÔÚ¼ÆËãÊýѧ±í´ïʽʱ½«¸Ã±äÁ¿µ±×÷ÊýÖµÁã´¦Àí¡£

ÖÚ¶àÔËËã·û

Awk µÄÁíÒ»¸öÓŵãÊÇËüÓµÓÐÈ«ÃæµÄÊýѧÔËËã·û¡£³ýÁ˱ê×¼µÄ¼Ó¼õ³Ë³ý£¬awk »¹Ö§³ÖÇ°ÃæÑÝʾµÄÖ¸ÊýÔËËã·û ¡°^¡±¡¢ÇóÄ££¨ÓàÊý£©ÔËËã·û ¡°%¡± ºÍ½è¼ø×Ô C ÓïÑԵĴóÁ¿·½±ãµÄ¸³ÖµÔËËã·û¡£

ÆäÖаüÀ¨Ç°ºó¼Ó/¼õ£¨i++¡¢--foo£©£¬¼Ó¼õ³Ë³ý¸³ÖµÔËËã·û£¨a+=3¡¢b*=2¡¢c/=2.2¡¢d-=6.2£©¡£¶øÇÒ£¬Õâ½ö½öÊÇÒ»²¿·Ö ---- ÎÒÃÇ»¹ÄÜʹÓ÷½±ãµÄÇóÄ£/Ö¸Êý¸³ÖµÔËËã·û£¨a^=2¡¢b%=4£©¡£

×ֶηָô·û

Awk ÓÐÆä×Ô¼ºµÄÌØÊâ±äÁ¿¼¯ºÏ¡£ÆäÖÐһЩ±äÁ¿Ö§³Öµ÷ÓÅ awk ÐÔÄÜ£¬¶øÇÒ¿ÉÒÔ¶ÁÈ¡ÁíһЩ±äÁ¿À´ÊÕ¼¯¹ØÓÚÊäÈëµÄÖØÒªÐÅÏ¢¡£Ç°ÃæÒѾ­½Ó´¥¹ýÌØÊâ±äÁ¿FS¡£ÈçÇ°ËùÊö£¬Õâ¸ö±äÁ¿Ö§³ÖÉèÖà awk ÆÚÍûÔÚ×Ö¶ÎÖÐÕÒµ½µÄ×Ö·ûÐòÁС£µ±ÎÒÃÇʹÓà /ect/passwd ×÷ΪÊäÈëʱ£¬FS ÉèΪ":"¡£¾¡¹ÜÕâÑù×ö¿ÉÒÔ½â¾öÎÊÌ⣬µ« FS »¹Ö§³Ö¸ü¸ßµÄÁé»îÐÔ¡£¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:FS="\t+"
ÉÏÃæʹÓÃÌØÊâµÄÕýÔò±í´ïʽ×Ö·û ¡°+¡±£¬±íʾ ¡°Ò»¸ö»ò¶à¸öÇ°Ò»×Ö·û¡±¡£¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:FS="[[:space:]+]"
¾¡¹Ü¸Ã¸³ÖµÔËËã·ûÄܹ»½â¾öÎÊÌ⣬µ«ÊDz¢·Ç±ØÒª¡£ÎªÊ²Ã´ÄØ£¿ÒòΪÔÚĬÈÏÇé¿öÏ£¬FS ±»ÉèΪµ¥¸ö¿Õ¸ñ×Ö·û£¬awk ½«Æä½âÊÍΪ ¡°Ò»¸ö»ò¶à¸ö¿Õ¸ñ»òÖƱí·û¡±¡£ÔÚÕâ¸öÌض¨µÄʾÀýÖУ¬Ä¬È쵀 FS ÉèÖÃÇ¡ºÃÊÇÄú×îÏëÒªµÄÉèÖ᣸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:FS="foo[0-9][0-9][0-9]"
×Ö¶ÎÊýÄ¿¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:{
if ( NF > 2 ) {
print $1 " " $2 ":" $3
}
}
¼Ç¼ÊýÄ¿¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:{
#skip header
if ( NR > 10 ) {
print "ok, now for the real information!"
}
}
Awk »¹ÌṩÁËһЩ¾ßÓжàÖÖÓÃ;µÄÆäËû±äÁ¿¡£ºóÐøÎÄÕÂÖн«ÉîÈë½²½âÕâЩ±äÁ¿¡£

ÎÒÃÇ¶Ô awk µÄ³õ´Î̽¾¿ÏÖÔھͽáÊøÁË¡£Ëæ×ű¾ÏµÁеÄÑÓÐø£¬ÎÒ½«ÑÝʾ¸ü¸ß¼¶µÄ awk ¹¦ÄÜ£¬ÎÒÃǽ«ÓÃÒ»¸öÕæʵµÄ awk Ó¦ÓóÌÐò×÷Ϊ±¾ÏµÁеĽáβ¡£Í¬Ê±£¬Èç¹û¼±ÓÚѧϰ¸ü¶à֪ʶ£¬Çë²Î¿¼ÏÂÃæÁгöµÄ²Î¿¼×ÊÁÏ¡£







awkÔÚÏßʵÀýÈëÃÅ½Ì³Ì µÚ¶þ²¿·Ö
----¼Ç¼¡¢Ñ­»·ºÍÊý×é

¶àÐмǼ

awk ÊÇÒ»ÖÖÓÃÓÚ¶ÁÈ¡ºÍ´¦Àí½á¹¹»¯Êý¾Ý£¨ÈçϵͳµÄ /etc/passwd Îļþ£©µÄ¼«¼Ñ¹¤¾ß¡£/etc/passwd ÊÇ UNIXÓû§Êý¾Ý¿â£¬²¢ÇÒÊÇÓÃðºÅ¶¨½çµÄÎı¾Îļþ£¬Ëü°üº¬Ðí¶àÖØÒªÐÅÏ¢£¬°üÀ¨ËùÓÐÏÖÓÐÓû§ÕÊ»§ºÍÓû§±êʶ£¬ÒÔ¼°ÆäËüÐÅÏ¢¡£ÔÚµÚÒ»²¿·ÖÖУ¬ÑÝʾÁË awkÈçºÎÇáËɵطÖÎöÕâ¸öÎļþ¡£ÎÒÃÇÖ»Ð뽫 FS£¨×ֶηָô·û£©±äÁ¿ÉèÖÃ³É ":"¡£

ÕýÈ·ÉèÖÃÁË FS ±äÁ¿Ö®ºó£¬¾Í¿ÉÒÔ½« awkÅäÖóɷÖÎö¼¸ºõÈκÎÀàÐ͵Ľṹ»¯Êý¾Ý£¬Ö»ÒªÕâЩÊý¾ÝÊÇÿÐÐÒ»¸ö¼Ç¼¡£È»¶ø£¬Èç¹ûÒª·ÖÎöÕ¼¾Ý¶àÐеļǼ£¬½ö½öÒÀ¿¿ÉèÖà FSÊDz»¹»µÄ¡£ÔÚÕâЩÇé¿öÏ£¬ÎÒÃÇ»¹ÐèÒªÐÞ¸Ä RS ¼Ç¼·Ö¸ô·û±äÁ¿¡£RS ±äÁ¿¸æËß awk µ±Ç°¼Ç¼ʲôʱºò½áÊø£¬Ð¼Ç¼ʲôʱºò¿ªÊ¼¡£

Æ©È磬ÈÃÎÒÃÇÌÖÂÛÒ»ÏÂÈçºÎÍê³É´¦Àí¡°Áª°îÖ¤È˱£»¤¼Æ»®¡±ËùÉæ¼°ÈËÔ±µÄµØÖ·ÁбíµÄÈÎÎñ£ºÒýÓÃ:[indent]Jimmy the Weasel
100 Pleasant Drive
San Francisco, CA 12345
Big Tony
200 Incognito Ave.
Suburbia, WA 67890[/indent]
ÀíÂÛÉÏ£¬ÎÒÃÇÏ£Íû awk ½«Ã¿ 3 Ðп´×÷ÊÇÒ»¸ö¶ÀÁ¢µÄ¼Ç¼£¬¶ø²»ÊÇÈý¸ö¶ÀÁ¢µÄ¼Ç¼¡£Èç¹û awk ½«µØÖ·µÄµÚÒ»Ðп´×÷ÊǵÚÒ»¸ö×Ö¶Î($1)£¬½ÖµÀµØÖ·¿´×÷Êǵڶþ¸ö×ֶΠ($2)£¬³ÇÊС¢ÖݺÍÓÊÕþ±àÂë¿´×÷ÊǵÚÈý¸ö×ֶΠ$3£¬ÄÇôÕâ¸ö´úÂë¾Í»á±äµÃºÜ¼òµ¥¡£ÒÔϾÍÊÇÎÒÃÇÏëÒªµÃµ½µÄ´úÂ룺¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:BEGIN {
FS="\n"
RS=""
}
ÔÚÉÏÃæÕâ¶Î´úÂëÖУ¬½« FS ÉèÖÃ³É "\n" ¸æËß awk ÿ¸ö×ֶζ¼Õ¼¾ÝÒ»ÐС£Í¨¹ý½« RS ÉèÖÃ³É ""£¬»¹»á¸æËß awkÿ¸öµØÖ·¼Ç¼¶¼ÓÉ¿Õ°×Ðзָô¡£Ò»µ© awkÖªµÀÊÇÈçºÎ¸ñʽ»¯ÊäÈëµÄ£¬Ëü¾Í¿ÉÒÔΪÎÒÃÇÖ´ÐÐËùÓзÖÎö¹¤×÷£¬½Å±¾µÄÆäÓಿ·ÖºÜ¼òµ¥¡£ÈÃÎÒÃÇÑо¿Ò»¸öÍêÕûµÄ½Å±¾£¬Ëü½«·ÖÎöÕâ¸öµØÖ·ÁÐ±í£¬²¢½«Ã¿¸ö¼Ç¼´òÓ¡ÔÚÒ»ÐÐÉÏ£¬ÓöººÅ·Ö¸ôÿ¸ö×ֶΡ£

address.awk¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:BEGIN {
FS="\n"
RS=""
}
{
print $1 ", " $2 ", " $3
}
Èç¹ûÕâ¸ö½Å±¾±£´æΪ address.awk£¬µØÖ·Êý¾Ý´æ´¢ÔÚÎļþ address.txt ÖУ¬¿ÉÒÔͨ¹ýÊäÈë "awk -f address.awk address.txt" À´Ö´ÐÐÕâ¸ö½Å±¾¡£´Ë´úÂ뽫²úÉúÒÔÏÂÊä³ö£ºÒýÓÃ:[indent]Jimmy the Weasel, 100 Pleasant Drive, San Francisco, CA 12345
Big Tony, 200 Incognito Ave., Suburbia, WA 67890[/indent]
OFS ºÍ ORS

ÔÚ address.awk µÄ print Óï¾äÖУ¬¿ÉÒÔ¿´µ½ awk»áÁ¬½Ó£¨ºÏ²¢£©Ò»ÐÐÖб˴ËÏàÁÚµÄ×Ö·û´®¡£ÎÒÃÇʹÓô˹¦ÄÜÔÚͬһÐÐÉϵÄÈý¸ö×Ö¶ÎÖ®¼ä²åÈëÒ»¸ö¶ººÅºÍ¿Õ¸ñ (",")¡£Õâ¸ö·½·¨ËäÈ»ÓÐÓ㬵«±È½ÏÄÑ¿´¡£ÓëÆäÔÚ×ֶμä²åÈë ", " ×Ö·û´®£¬µ¹²»ÈçÈÃͨ¹ýÉèÖÃÒ»¸öÌØÊâ awk ±äÁ¿ OFS£¬Èà awkÍê³ÉÕâ¼þÊ¡£Çë²Î¿¼ÏÂÃæÕâ¸ö´úÂëƬ¶Ï¡£¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:print "Hello", "there", "Jim!"
ÕâÐдúÂëÖеĶººÅ²¢²»ÊÇʵ¼ÊÎÄ×Ö×Ö·û´®µÄÒ»²¿·Ö¡£ÊÂʵÉÏ£¬ËüÃǸæËß awk "Hello"¡¢"there" ºÍ "Jim!" Êǵ¥¶ÀµÄ×ֶΣ¬²¢ÇÒÓ¦¸ÃÔÚÿ¸ö×Ö·û´®Ö®¼ä´òÓ¡ OFS ±äÁ¿¡£È±Ê¡Çé¿öÏ£¬awk ²úÉúÒÔÏÂÊä³ö£ºÒýÓÃ:[indent]Hello there Jim![/indent]
ÕâÊÇȱʡÇé¿öϵÄÊä³ö½á¹û£¬OFS ±»ÉèÖÃ³É " "£¬µ¥¸ö¿Õ¸ñ¡£²»¹ý£¬ÎÒÃÇ¿ÉÒÔ·½±ãµØÖØж¨Òå OFS£¬ÕâÑù awk ½«²åÈëÎÒÃÇÖÐÒâµÄ×ֶηָô·û¡£ÒÔÏÂÊÇԭʼ address.awk ³ÌÐòµÄÐÞ¶©°æ£¬ËüʹÓà OFS À´Êä³öÄÇЩÖмäµÄ ", " ×Ö·û´®£º

address.awk µÄÐÞ¶©°æ¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:BEGIN {
FS="\n"
RS=""
OFS=", "
}
{
print $1, $2, $3
}
awk»¹ÓÐÒ»¸öÌØÊâ±äÁ¿ ORS£¬È«³ÆÊÇ¡°Êä³ö¼Ç¼·Ö¸ô·û¡±¡£Í¨¹ýÉèÖÃȱʡΪ»»ÐÐ ("\n") µÄ OFS£¬ÎÒÃÇ¿ÉÒÔ¿ØÖÆÔÚ printÓï¾ä½áβ×Ô¶¯´òÓ¡µÄ×Ö·û¡£È±Ê¡ ORS Öµ»áʹ awk ÔÚÐÂÐÐÖÐÊä³öÿ¸öÐ嵀 print Óï¾ä¡£Èç¹ûÏëʹÊä³öµÄ¼ä¸ô·­±¶£¬¿ÉÒÔ½« ORS ÉèÖóÉ"\n\n"¡£»òÕߣ¬Èç¹ûÏëÒªÓõ¥¸ö¿Õ¸ñ·Ö¸ô¼Ç¼£¨¶ø²»»»ÐУ©£¬½« ORS ÉèÖÃ³É ""¡£

½«¶àÐÐת»»³ÉÓà tab ·Ö¸ôµÄ¸ñʽ

¼ÙÉèÎÒÃDZàдÁËÒ»¸ö½Å±¾£¬Ëü½«µØÖ·Áбíת»»³Éÿ¸ö¼Ç¼һÐУ¬ÇÒÓà tab ¶¨½çµÄ¸ñʽ£¬ÒԱ㵼Èëµç×Ó±í¸ñ¡£Ê¹ÓÃÉÔ¼ÓÐÞ¸ÄµÄ address.awk Ö®ºó£¬¾Í¿ÉÒÔÇå³þµØ¿´µ½Õâ¸ö³ÌÐòÖ»ÊʺÏÓÚÈýÐеĵØÖ·¡£Èç¹û awk Óöµ½ÒÔϵØÖ·£¬½«¶ªµôµÚËÄÐУ¬²¢ÇÒ²»´òÓ¡¸ÃÐУºÒýÓÃ:[indent]Cousin Vinnie
Vinnie's Auto Shop
300 City Alley
Sosueme, OR 76543[/indent]
Òª´¦ÀíÕâÖÖÇé¿ö£¬´úÂë×îºÃ¿¼ÂÇÿ¸ö×ֶεļǼÊýÁ¿£¬²¢ÒÀ´Î´òӡÿ¸ö¼Ç¼¡£ÏÖÔÚ£¬´úÂëÖ»´òÓ¡µØÖ·µÄÇ°Èý¸ö×ֶΡ£ÒÔϾÍÊÇÎÒÃÇÏëÒªµÄһЩ´úÂ룺

ÊʺϾßÓÐÈÎÒâ¶à×ֶεĵØÖ·µÄ address.awk °æ±¾¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:BEGIN {
FS="\n"
RS=""
ORS=""
}

{
x=1
while ( x print $x "\t"
x++
}
print $NF "\n"
}
Ê×ÏÈ£¬½«×ֶηָô·û FS ÉèÖÃ³É "\n"£¬½«¼Ç¼·Ö¸ô·û RS ÉèÖÃ³É ""£¬ÕâÑù awk¿ÉÒÔÏóÒÔÇ°Ò»ÑùÕýÈ··ÖÎö¶àÐеØÖ·¡£È»ºó£¬½«Êä³ö¼Ç¼·Ö¸ô·û ORS ÉèÖÃ³É ""£¬Ëü½«Ê¹ print Óï¾äÔÚÿ¸öµ÷Óýáβ ²»Êä³öÐÂÐС£ÕâÒâζ×ÅÈç¹ûÏ£ÍûÈκÎÎı¾´ÓеÄÒ»ÐпªÊ¼£¬ÄÇôÐèÒªÃ÷ȷдÈë print "\n" ¡£

ÔÚÖ÷´úÂë¿éÖУ¬´´½¨ÁËÒ»¸ö±äÁ¿ x À´´æ´¢ÕýÔÚ´¦ÀíµÄµ±Ç°×ֶεıàºÅ¡£Æð³õ£¬Ëü±»ÉèÖÃ³É 1¡£È»ºó£¬ÎÒÃÇʹÓà while Ñ­»·£¨Ò»ÖÖ awkÑ­»·½á¹¹£¬µÈͬÓÚ C ÓïÑÔÖÐµÄ while Ñ­»·£©£¬¶ÔÓÚËùÓмǼ£¨×îºóÒ»¸ö¼Ç¼³ýÍ⣩Öظ´´òÓ¡¼Ç¼ºÍ tab×Ö·û¡£×îºó£¬´òÓ¡×îºóÒ»¸ö¼Ç¼ºÍ»»ÐУ»´ËÍ⣬ÓÉÓÚ½« ORS ÉèÖÃ³É ""£¬print ½«²»Êä³ö»»ÐС£³ÌÐòÊä³öÈçÏ£¬ÕâÕýÊÇÎÒÃÇËùÆÚÍûµÄ£º

ÎÒÃÇÏëÒªµÄÊä³ö¡£²»ËãƯÁÁ£¬µ«Óà tab ¶¨½ç£¬ÒÔ±ãÓÚµ¼Èëµç×Ó±í¸ñÒýÓÃ:[indent]Jimmy the Weasel 100 Pleasant Drive San Francisco, CA 12345
Big Tony 200 Incognito Ave. Suburbia, WA 67890
Cousin Vinnie Vinnie's Auto Shop 300 City Alley Sosueme, OR 76543[/indent]
Ñ­»·½á¹¹

ÎÒÃÇÒѾ­¿´µ½ÁË awk µÄ while Ñ­»·½á¹¹£¬ËüµÈͬÓÚÏàÓ¦µÄ C ÓïÑÔ while Ñ­»·¡£awk »¹ÓÐ "do...while"Ñ­»·£¬ËüÔÚ´úÂë¿é½áβ´¦¶ÔÌõ¼þÇóÖµ£¬¶ø²»Ïó±ê×¼ while Ñ­»·ÄÇÑùÔÚ¿ªÊ¼´¦ÇóÖµ¡£ËüÀàËÆÓÚÆäËüÓïÑÔÖÐµÄ "repeat...until"Ñ­»·¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º

do...while ʾÀý

{
count=1
do {
print "I get printed at least once no matter what"
} while ( count != 1 )
}


ÓëÒ»°ãµÄ while Ñ­»·²»Í¬£¬ÓÉÓÚÔÚ´úÂë¿éÖ®ºó¶ÔÌõ¼þÇóÖµ£¬"do...while" Ñ­»·ÓÀÔ¶¶¼ÖÁÉÙÖ´ÐÐÒ»´Î¡£»»¾ä»°Ëµ£¬µ±µÚÒ»´ÎÓöµ½ÆÕͨ while Ñ­»·Ê±£¬Èç¹ûÌõ¼þΪ¼Ù£¬½«ÓÀÔ¶²»Ö´ÐиÃÑ­»·¡£

for Ñ­»·
awk ÔÊÐí´´½¨ for Ñ­»·£¬Ëü¾ÍÏó while Ñ­»·£¬Ò²µÈͬÓÚ C ÓïÑ﵀ for Ñ­»·£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:for ( initial assignment; comparison; increment ) {
code block
}
ÒÔÏÂÊÇÒ»¸ö¼ò¶ÌʾÀý£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:for ( x = 1; x <= 4; x++ ) {
print "iteration",x
}
´Ë¶Î´úÂ뽫´òÓ¡£ºÒýÓÃ:[indent]iteration 1
iteration 2
iteration 3
iteration 4[/indent]
break ºÍ continue

´ËÍ⣬Èçͬ C ÓïÑÔÒ»Ñù£¬awk ÌṩÁË break ºÍ continue Óï¾ä¡£Ê¹ÓÃÕâЩÓï¾ä¿ÉÒÔ¸üºÃµØ¿ØÖÆ awk µÄÑ­»·½á¹¹¡£ÒÔÏÂÊÇÆÈÇÐÐèÒª break Óï¾äµÄ´úÂëƬ¶Ï£º

while ËÀÑ­»·¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:while (1) {
print "forever and ever..."
}
while ËÀÑ­»· 1 ÓÀÔ¶´ú±íÊÇÕ棬Õâ¸ö while Ñ­»·½«ÓÀÔ¶ÔËÐÐÏÂÈ¥¡£ÒÔÏÂÊÇÒ»¸öÖ»Ö´ÐÐÊ®´ÎµÄÑ­»·£º

break Óï¾äʾÀý¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:x=1
while(1) {
print "iteration",x
if ( x == 10 ) {
break
}
x++
}
ÕâÀbreak Óï¾äÓÃÓÚ¡°ÌÓ³ö¡±×îÉî²ãµÄÑ­»·¡£"break" ʹѭ»·Á¢¼´ÖÕÖ¹£¬²¢¼ÌÐøÖ´ÐÐÑ­»·´úÂë¿éºóÃæµÄÓï¾ä¡£

continue Óï¾ä²¹³äÁË break£¬Æä×÷ÓÃÈçÏ£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:x=1
while (1) {
if ( x == 4 ) {
x++
continue
}
print "iteration",x
if ( x > 20 ) {
break
}
x++
}
Õâ¶Î´úÂë´òÓ¡ "iteration 1" µ½ "iteration 21"£¬"iteration 4" ³ýÍâ¡£Èç¹ûµü´úµÈÓÚ 4£¬ÔòÔö¼Ó x²¢µ÷Óà continue Óï¾ä£¬¸ÃÓï¾äÁ¢¼´Ê¹ awk ¿ªÊ¼Ö´ÐÐÏÂÒ»¸öÑ­»·µü´ú£¬¶ø²»Ö´ÐдúÂë¿éµÄÆäÓಿ·Ö¡£Èçͬ breakÒ»Ñù£¬continue Óï¾äÊʺϸ÷ÖÖ awk µü´úÑ­»·¡£ÔÚ for Ñ­»·Ö÷ÌåÖÐʹÓÃʱ£¬continue½«Ê¹Ñ­»·¿ØÖƱäÁ¿×Ô¶¯Ôö¼Ó¡£ÒÔÏÂÊÇÒ»¸öµÈ¼ÛÑ­»·£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:for ( x=1; x<=21; x++ ) {
if ( x == 4 ) {
continue
}
print "iteration",x
}
ÔÚ while Ñ­»·ÖÐʱ£¬ÔÚµ÷Óà continue ֮ǰûÓбØÒªÔö¼Ó x £¬ÒòΪ for Ñ­»·»á×Ô¶¯Ôö¼Ó x ¡£

Êý×é

Èç¹ûÄúÖªµÀ awk ¿ÉÒÔʹÓÃÊý×飬ÄúÒ»¶¨»á¸Ðµ½¸ßÐË¡£È»¶ø£¬ÔÚ awk ÖУ¬Êý×éϱêͨ³£´Ó 1 ¿ªÊ¼£¬¶ø²»ÊÇ 0£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:myarray[1]="jim"
myarray[2]=456
awk Óöµ½µÚÒ»¸ö¸³ÖµÓï¾äʱ£¬Ëü½«´´½¨ myarray £¬²¢½«ÔªËØ myarray[1] ÉèÖÃ³É "jim"¡£Ö´ÐÐÁ˵ڶþ¸ö¸³ÖµÓï¾äºó£¬Êý×é¾ÍÓÐÁ½¸öÔªËØÁË¡£

Êý×éµü´ú

¶¨ÒåÖ®ºó£¬awk ÓÐÒ»¸ö±ãÀûµÄ»úÖÆÀ´µü´úÊý×éÔªËØ£¬ÈçÏÂËùʾ£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:for ( x in myarray ) {
print myarray[x]
}
Õâ¶Î´úÂ뽫´òÓ¡Êý×é myarray ÖеÄÿһ¸öÔªËØ¡£µ±¶ÔÓÚ for ʹÓÃÕâÖÖÌØÊâµÄ "in" ÐÎʽʱ£¬awk ½« myarrayµÄÿ¸öÏÖÓÐϱêÒÀ´Î¸³Öµ¸ø x £¨Ñ­»·¿ØÖƱäÁ¿£©£¬Ã¿´Î¸³ÖµÒÔºó¶¼Ñ­»·Ò»´ÎÑ­»·´úÂë¡£ËäÈ»ÕâÊÇÒ»¸ö·Ç³£·½±ãµÄ awk ¹¦ÄÜ£¬µ«ËüÓÐÒ»¸öȱµã -- µ±awk ÔÚÊý×éϱêÖ®¼äÂÖתʱ£¬Ëü²»»áÒÀÕÕÈκÎÌض¨µÄ˳Ðò¡£ÄǾÍÒâζ×ÅÎÒÃDz»ÄÜÖªµÀÒÔÉÏ´úÂëµÄÊä³öÊÇ£ºÒýÓÃ:[indent]jim
456[/indent]
»¹ÊÇÒýÓÃ:[indent]456
jim[/indent]
Ì×Óà Forrest Gump µÄ»°À´Ëµ£¬µü´úÊý×éÄÚÈݾÍÏñÒ»ºÐÇÉ¿ËÁ¦ -- ÄúÓÀÔ¶²»ÖªµÀ½«»áµÃµ½Ê²Ã´¡£Òò´ËÓбØҪʹ awk Êý×é¡°×Ö·û´®»¯¡±£¬ÎÒÃÇÏÖÔÚ¾ÍÀ´Ñо¿Õâ¸öÎÊÌâ¡£

Êý×éϱê×Ö·û´®»¯

ÔÚÎÒµÄ Ç°Ò»ÆªÎÄÕ ÖУ¬ÎÒÑÝʾÁË awk ʵ¼ÊÉÏÒÔ×Ö·û´®¸ñʽÀ´´æ´¢Êý×ÖÖµ¡£ËäÈ» awk ÒªÖ´ÐбØÒªµÄת»»À´Íê³ÉÕâÏ×÷£¬µ«ËüÈ´¿ÉÒÔʹÓÃijЩ¿´ÆðÀ´ºÜÆæ¹ÖµÄ´úÂ룺¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:a="1"
b="2"
c=a+b+3
Ö´ÐÐÁËÕâ¶Î´úÂëºó£¬ c µÈÓÚ 6 ¡£ÓÉÓÚ awk ÊÇ¡°×Ö·û´®»¯¡±µÄ£¬Ìí¼Ó×Ö·û´® "1" ºÍ "2" ÔÚ¹¦ÄÜÉϲ¢²»±ÈÌí¼ÓÊý×Ö 1 ºÍ 2ÄÑ¡£ÕâÁ½ÖÖÇé¿öÏ£¬awk ¶¼¿ÉÒԳɹ¦Ö´ÐÐÔËËã¡£awk µÄ¡°×Ö·û´®»¯¡±ÐÔÖʷdz£¿É°® --Äú¿ÉÄÜÏëÒªÖªµÀÈç¹ûʹÓÃÊý×éµÄ×Ö·û´®Ï±ê»á·¢ÉúʲôÇé¿ö¡£ÀýÈ磬ʹÓÃÒÔÏ´úÂ룺¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:myarr["1"]="Mr. Whipple"
print myarr["1"]
¿ÉÒÔÔ¤ÁÏ£¬Õâ¶Î´úÂ뽫´òÓ¡ "Mr. Whipple"¡£µ«Èç¹ûÈ¥µôµÚ¶þ¸ö "1" ϱêÖеÄÒýºÅ£¬Çé¿öÓÖ»áÔõÑùÄØ£¿¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:myarr["1"]="Mr. Whipple"
print myarr[1]
²ÂÏëÕâ¸ö´úÂëƬ¶ÏµÄ½á¹û±È½ÏÄÑ¡£awk ½« myarr["1"] ºÍ myarr[1]¿´×÷Êý×éµÄÁ½¸ö¶ÀÁ¢ÔªËØ£¬»¹ÊÇËüÃÇÊÇָͬһ¸öÔªËØ£¿´ð°¸ÊÇËüÃÇÖ¸µÄÊÇͬһ¸öÔªËØ£¬awk ½«´òÓ¡ "Mr.Whipple"£¬ÈçͬµÚÒ»¸ö´úÂëƬ¶ÏÒ»Ñù¡£ËäÈ»¿´ÉÏÈ¥¿ÉÄÜÓеã¹Ö£¬µ« awk ÔÚÄ»ºóȴһֱʹÓÃÊý×éµÄ×Ö·û´®Ï±꣡

Á˽âÁËÕâ¸öÆæ¹ÖµÄÕæÏàÖ®ºó£¬ÎÒÃÇÖеÄһЩÈË¿ÉÄÜÏëÒªÖ´ÐÐÀàËÆÓÚÒÔÏµĹŹִúÂ룺¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:myarr["name"]="Mr. Whipple"
print myarr["name"]
Õâ¶Î´úÂë²»½ö²»»á²úÉú´íÎ󣬶øÇÒËüµÄ¹¦ÄÜÓëÇ°ÃæµÄʾÀýÍêÈ«Ïàͬ£¬Ò²½«´òÓ¡ "Mr. Whipple"£¡¿ÉÒÔ¿´µ½£¬awk²¢Ã»ÓÐÏÞÖÆÎÒÃÇʹÓô¿ÕûÊýϱꣻÈç¹ûÎÒÃÇÔ¸Ò⣬¿ÉÒÔʹÓÃ×Ö·û´®Ï±꣬¶øÇÒ²»»á²úÉúÈκÎÎÊÌâ¡£Ö»ÒªÎÒÃÇʹÓ÷ÇÕûÊýÊý×éϱ꣬Èçmyarr["name"] £¬ÄÇôÎÒÃǾÍÔÚʹÓà ¹ØÁªÊý×é ¡£´Ó¼¼ÊõÉϽ²£¬Èç¹ûÎÒÃÇʹÓÃ×Ö·û´®Ï±꣬awkµÄºǫ́²Ù×÷²¢Ã»ÓÐʲô²»Í¬£¨ÒòΪ¼´±ãʹÓá°ÕûÊý¡±Ï±꣬awk »¹ÊǻὫËü¿´×÷ÊÇ×Ö·û´®£©¡£µ«ÊÇ£¬Ó¦¸Ã½«ËüÃdzÆ×÷ ¹ØÁªÊý×é --ËüÌýÆðÀ´ºÜ¿á£¬¶øÇÒ»á¸øÄúµÄÉÏ˾ÁôÏÂÓ¡Ïó¡£×Ö·û´®»¯Ï±êÊÇÎÒÃǵÄСÃØÃÜ¡£;)

Êý×鹤¾ß

̸µ½Êý×éʱ£¬awk ¸øÓèÎÒÃÇÐí¶àÁé»îÐÔ¡£¿ÉÒÔʹÓÃ×Ö·û´®Ï±꣬¶øÇÒ²»ÐèÒªÁ¬ÐøµÄÊý×ÖÐòÁÐϱ꣨ÀýÈ磬¿ÉÒÔ¶¨Òå myarr[1] ºÍmyarr[1000] £¬µ«²»¶¨ÒåÆäËüËùÓÐÔªËØ£©¡£ËäÈ»ÕâЩ¶¼ºÜÓÐÓ㬵«ÔÚijЩÇé¿öÏ£¬»á²úÉú»ìÏý¡£ÐҺã¬awkÌṩÁËһЩʵÓù¦ÄÜÓÐÖúÓÚʹÊý×é±äµÃ¸üÒ×ÓÚ¹ÜÀí¡£

Ê×ÏÈ£¬¿ÉÒÔɾ³ýÊý×éÔªËØ¡£Èç¹ûÏëҪɾ³ýÊý×é fooarray µÄÔªËØ 1 £¬ÊäÈ룺¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:delete fooarray[1]
¶øÇÒ£¬Èç¹ûÏëÒª²é¿´ÊÇ·ñ´æÔÚij¸öÌض¨Êý×éÔªËØ£¬¿ÉÒÔʹÓÃÌØÊâµÄ "in" ²¼¶ûÔËËã·û£¬ÈçÏÂËùʾ£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:if ( 1 in fooarray ) {
print "Ayep! It's there."
} else {
print "Nope! Can't find it."
}







awkÔÚÏßʵÀýÈëÃÅ½Ì³Ì µÚÈý²¿·Ö
----×Ö·û´®º¯ÊýºÍ¡­¡­Ö§Æ±²¾£¿

¸ñʽ»¯Êä³ö

ËäÈ»´ó¶àÊýÇé¿öÏ awk µÄ print Óï¾ä¿ÉÒÔÍê³ÉÈÎÎñ£¬µ«ÓÐʱÎÒÃÇ»¹ÐèÒª¸ü¶à¡£ÔÚÄÇЩÇé¿öÏ£¬awk ÌṩÁËÁ½¸öÎÒÃÇÊìÖªµÄÀÏÅóÓÑprintf() ºÍ sprintf()¡£Êǵģ¬ÈçͬÆäËüÐí¶à awk ²¿¼þÒ»Ñù£¬ÕâЩº¯ÊýµÈͬÓÚÏàÓ¦µÄ C ÓïÑÔº¯Êý¡£printf()»á½«¸ñʽ»¯×Ö·û´®´òÓ¡µ½ stdout£¬¶ø sprintf() Ôò·µ»Ø¿ÉÒÔ¸³Öµ¸ø±äÁ¿µÄ¸ñʽ»¯×Ö·û´®¡£Èç¹û²»ÊìϤ printf() ºÍsprintf()£¬½éÉÜ C ÓïÑÔµÄÎÄÕ¿ÉÒÔÈÃÄúѸËÙÁ˽âÕâÁ½¸ö»ù±¾´òÓ¡º¯Êý¡£ÔÚ Linux ϵͳÉÏ£¬¿ÉÒÔÊäÈë "man 3 printf"À´²é¿´ printf() °ïÖúÒ³Ãæ¡£

ÒÔÏÂÊÇһЩ awk sprintf() ºÍ printf() µÄÑù±¾´úÂë¡£¿ÉÒÔ¿´µ½£¬ËüÃǼ¸ºõÓë C ÓïÑÔÍêÈ«Ïàͬ¡£¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:x=1
b="foo"
printf("%s got a %d on the last test\n","Jim",83)
myout=("%s-%d",b,x)
print myout
´Ë´úÂ뽫´òÓ¡£ºÒýÓÃ:[indent]Jim got a 83 on the last test
foo-1[/indent]
×Ö·û´®º¯Êý

awk ÓÐÐí¶à×Ö·û´®º¯Êý£¬ÕâÊǼþºÃÊ¡£ÔÚ awk ÖУ¬È·ÊµÐèÒª×Ö·û´®º¯Êý£¬ÒòΪ²»ÄÜÏóÔÚÆäËüÓïÑÔ£¨Èç C¡¢C++ ºÍ Python£©ÖÐÄÇÑù½«×Ö·û´®¿´×÷ÊÇ×Ö·ûÊý×é¡£ÀýÈ磬Èç¹ûÖ´ÐÐÒÔÏ´úÂ룺¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:mystring="How are you doing today?"
print mystring[3]
½«»á½ÓÊÕµ½Ò»¸ö´íÎó£¬ÈçÏÂËùʾ£ºÒýÓÃ:[indent]awk: string.gawk:59: fatal: attempt to use scalar as array[/indent]
àÞ£¬ºÃ°É¡£ËäÈ»²»Ïó Python µÄÐòÁÐÀàÐÍÄÇÑù·½±ã£¬µ« awk µÄ×Ö·û´®º¯Êý»¹ÊÇ¿ÉÒÔÍê³ÉÈÎÎñ¡£ÈÃÎÒÃÇÀ´¿´Ò»Ï¡£

Ê×ÏÈ£¬ÓÐÒ»¸ö»ù±¾ length() º¯Êý£¬Ëü·µ»Ø×Ö·û´®µÄ³¤¶È¡£ÒÔÏÂÊÇËüµÄʹÓ÷½·¨£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:print length(mystring)
´Ë´úÂ뽫´òÓ¡Öµ£ºÒýÓÃ:[indent]24[/indent]
ºÃ£¬¼ÌÐø¡£ÏÂÒ»¸ö×Ö·û´®º¯Êý½Ð×÷ index£¬Ëü½«·µ»Ø×Ó×Ö·û´®ÔÚÁíÒ»¸ö×Ö·û´®ÖгöÏÖµÄλÖã¬Èç¹ûûÓÐÕÒµ½¸Ã×Ö·û´®Ôò·µ»Ø 0¡£Ê¹Óà mystring£¬¿ÉÒÔ°´ÒÔÏ·½·¨µ÷ÓÃËü£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:print index(mystring,"you")
awk »á´òÓ¡£ºÒýÓÃ:[indent]9[/indent]
ÈÃÎÒÃǼÌÐøÌÖÂÛÁíÍâÁ½¸ö¼òµ¥µÄº¯Êý£¬tolower() ºÍ toupper()¡£ÓëÄú²ÂÏëµÄÒ»Ñù£¬ÕâÁ½¸öº¯Êý½«·µ»Ø×Ö·û´®²¢ÇÒ½«ËùÓÐ×Ö·û·Ö±ðת»»³ÉСд»ò´óд¡£Çë×¢Ò⣬tolower() ºÍ toupper() ·µ»ØеÄ×Ö·û´®£¬²»»áÐÞ¸ÄÔ­À´µÄ×Ö·û´®¡£Õâ¶Î´úÂ룺¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:print tolower(mystring)
print toupper(mystring)
print mystring
¡­¡­½«²úÉúÒÔÏÂÊä³ö£ºÒýÓÃ:[indent]how are you doing today?
HOW ARE YOU DOING TODAY?
How are you doing today?[/indent]
µ½ÏÖÔÚΪֹһÇв»´í£¬µ«ÎÒÃǾ¿¾¹ÈçºÎ´Ó×Ö·û´®ÖÐÑ¡Ôñ×Ó´®£¬ÉõÖÁµ¥¸ö×Ö·û£¿ÄǾÍÊÇʹÓà substr() µÄÔ­Òò¡£ÒÔÏÂÊÇ substr() µÄµ÷Ó÷½·¨£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:mysub=substr(mystring,startpos,maxlen)
mystringÓ¦¸ÃÊÇÒª´ÓÖгéÈ¡×Ó´®µÄ×Ö·û´®±äÁ¿»òÎÄ×Ö×Ö·û´®¡£startpos Ó¦¸ÃÉèÖóÉÆðʼ×Ö·ûλÖã¬maxlenÓ¦¸Ã°üº¬Òª³éÈ¡µÄ×Ö·û´®µÄ×î´ó³¤¶È¡£Çë×¢Ò⣬ÎÒ˵µÄÊÇ ×î´ó³¤¶È £»Èç¹û length(mystring) ±È startpos+maxlen¶Ì£¬ÄÇôµÃµ½µÄ½á¹û¾Í»á±»½Ø¶Ï¡£substr() ²»»áÐÞ¸Äԭʼ×Ö·û´®£¬¶øÊÇ·µ»Ø×Ó´®¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:print substr(mystring,9,3)
awk ½«´òÓ¡£ºÒýÓÃ:[indent]you[/indent]
Èç¹ûÄúͨ³£ÓÃÓÚ±à³ÌµÄÓïÑÔʹÓÃÊý×éϱê·ÃÎʲ¿·Ö×Ö·û´®£¨ÒÔ¼°²»Ê¹ÓÃÕâÖÖÓïÑÔµÄÈË£©£¬Çë¼Çס substr() ÊÇ awk ´úÌæ·½·¨¡£ÐèҪʹÓÃËüÀ´³éÈ¡µ¥¸ö×Ö·ûºÍ×Ó´®£»ÒòΪ awk ÊÇ»ùÓÚ×Ö·û´®µÄÓïÑÔ£¬ËùÒԻᾭ³£Óõ½Ëü¡£

ÏÖÔÚ£¬ÎÒÃÇÌÖÂÛһЩ¸üÄÍÈËѰζµÄº¯Êý£¬Ê×ÏÈÊÇ match()¡£match() Óë index() ·Ç³£ÏàËÆ£¬ËüÓë index()µÄÇø±ðÔÚÓÚËü²¢²»ËÑË÷×Ó´®£¬ËüËÑË÷µÄÊǹæÔò±í´ïʽ¡£match() º¯Êý½«·µ»ØÆ¥ÅäµÄÆðʼλÖã¬Èç¹ûûÓÐÕÒµ½Æ¥Å䣬Ôò·µ»Ø 0¡£´ËÍ⣬match()»¹½«ÉèÖÃÁ½¸ö±äÁ¿£¬½Ð×÷ RSTART ºÍ RLENGTH¡£RSTART °üº¬·µ»ØÖµ£¨µÚÒ»¸öÆ¥ÅäµÄλÖã©£¬RLENGTHÖ¸¶¨ËüÕ¼¾ÝµÄ×Ö·û¿ç¶È£¨Èç¹ûûÓÐÕÒµ½Æ¥Å䣬Ôò·µ»Ø -1£©¡£Í¨¹ýʹÓà RSTART¡¢RLENGTH¡¢substr()ºÍÒ»¸öСѭ»·£¬¿ÉÒÔÇáËɵصü´ú×Ö·û´®ÖеÄÿ¸öÆ¥Åä¡£ÒÔÏÂÊÇÒ»¸ö match() µ÷ÓÃʾÀý£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:print match(mystring,/you/), RSTART, RLENGTH
awk ½«´òÓ¡£ºÒýÓÃ:[indent]9 9 3[/indent]
×Ö·û´®Ìæ»»

ÏÖÔÚ£¬ÎÒÃǽ«Ñо¿Á½¸ö×Ö·û´®Ìæ»»º¯Êý£¬sub() ºÍ gsub()¡£ÕâЩº¯ÊýÓëÄ¿Ç°ÒѾ­ÌÖÂÛ¹ýµÄº¯ÊýÂÔÓв»Í¬£¬ÒòΪËüÃÇ È·ÊµÐÞ¸Äԭʼ×Ö·û´® ¡£ÒÔÏÂÊÇÒ»¸öÄ£°å£¬ÏÔʾÁËÈçºÎµ÷Óà sub()£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:sub(regexp,replstring,mystring)
µ÷Óà sub() ʱ£¬Ëü½«ÔÚ mystring ÖÐÆ¥Åä regexp µÄµÚÒ»¸ö×Ö·ûÐòÁУ¬²¢ÇÒÓà replstring Ìæ»»¸ÃÐòÁС£sub() ºÍgsub() ÓÃÏàͬµÄ×Ô±äÁ¿£»Î¨Ò»µÄÇø±ðÊÇ sub() ½«Ìæ»»µÚÒ»¸ö regexp Æ¥Å䣨Èç¹ûÓеĻ°£©£¬gsub()½«Ö´ÐÐÈ«¾ÖÌæ»»£¬»»³ö×Ö·û´®ÖеÄËùÓÐÆ¥Åä¡£ÒÔÏÂÊÇÒ»¸ö sub() ºÍ gsub() µ÷ÓÃʾÀý£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:sub(/o/,"O",mystring)
print mystring
mystring="How are you doing today?"
gsub(/o/,"O",mystring)
print mystring
±ØÐ뽫 mystring ¸´Î»³ÉÆä³õʼֵ£¬ÒòΪµÚÒ»¸ö sub() µ÷ÓÃÖ±½ÓÐÞ¸ÄÁË mystring¡£ÔÚÖ´ÐÐʱ£¬´Ë´úÂ뽫ʹ awk Êä³ö£ºÒýÓÃ:[indent]HOw are you doing today?
HOw are yOu dOing tOday?[/indent]
µ±È»£¬Ò²¿ÉÒÔÊǸü¸´ÔӵĹæÔò±í´ïʽ¡£ÎҰѲâÊÔһЩ¸´ÔÓ¹æÔò±í´ïʽµÄÈÎÎñÁô¸øÄúÀ´Íê³É¡£

ͨ¹ý½éÉܺ¯Êý split()£¬ÎÒÃÇÀ´»ã×ÜÒ»ÏÂÒÑÌÖÂÛ¹ýµÄº¯Êý¡£split() µÄÈÎÎñÊÇ¡°Çпª¡±×Ö·û´®£¬²¢½«¸÷²¿·Ö·Åµ½Ê¹ÓÃÕûÊýϱêµÄÊý×éÖС£ÒÔÏÂÊÇÒ»¸ö split() µ÷ÓÃʾÀý£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:numelements=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec",mymonths,",")
µ÷Óà split() ʱ£¬µÚÒ»¸ö×Ô±äÁ¿°üº¬ÒªÇпªÎÄ×Ö×Ö·û´®»ò×Ö·û´®±äÁ¿¡£ÔÚµÚ¶þ¸ö×Ô±äÁ¿ÖУ¬Ó¦¸ÃÖ¸¶¨ split()½«ÌîÈëƬ¶Î²¿·ÖµÄÊý×éÃû³Æ¡£ÔÚµÚÈý¸öÔªËØÖУ¬Ö¸¶¨ÓÃÓÚÇпª×Ö·û´®µÄ·Ö¸ô·û¡£split() ·µ»Øʱ£¬Ëü½«·µ»Ø·Ö¸îµÄ×Ö·û´®ÔªËصÄÊýÁ¿¡£split()½«Ã¿Ò»¸öƬ¶Î¸³Öµ¸øϱê´Ó 1 ¿ªÊ¼µÄÊý×飬Òò´ËÒÔÏ´úÂ룺¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:print mymonths[1],mymonths[numelements]
¡­¡­½«´òÓ¡£ºÒýÓÃ:[indent]Jan Dec[/indent]
ÌØÊâ×Ö·û´®ÐÎʽ

¼ò¶Ì×¢ÊÍ -- µ÷Óà length()¡¢sub() »ò gsub() ʱ£¬¿ÉÒÔÈ¥µô×îºóÒ»¸ö×Ô±äÁ¿£¬ÕâÑù awk ½«¶Ô $0£¨Õû¸öµ±Ç°ÐУ©Ó¦Óú¯Êýµ÷Óá£Òª´òÓ¡ÎļþÖÐÿһÐеij¤¶È£¬Ê¹ÓÃÒÔÏ awk ½Å±¾£º¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:{
print length()
}
²ÆÎñÉϵÄȤÊÂ

¼¸ÐÇÆÚÇ°£¬ÎÒ¾ö¶¨Óà awk ±àд×Ô¼ºµÄ֧Ʊ²¾½áËã³ÌÐò¡£ÎÒ¾ö¶¨Ê¹Óüòµ¥µÄ tab¶¨½çÎı¾Îļþ£¬ÒÔ±ãÓÚÊäÈë×î½üµÄ´æ¿îºÍÌá¿î¼Ç¼¡£Æä˼·Êǽ«Õâ¸öÊý¾Ý½»¸ø awk½Å±¾£¬¸Ã½Å±¾»á×Ô¶¯ºÏ¼ÆËùÓнð¶î£¬²¢¸æËßÎÒÓà¶î¡£ÒÔÏÂÊÇÎÒ¾ö¶¨ÈçºÎ½«ËùÓн»Ò׼Ǽµ½ "ASCII checkbook" ÖУºÒýÓÃ:[indent]23 Aug 2000 food - - Y Jimmy's Buffet 30.25[/indent]
´ËÎļþÖеÄÿ¸ö×ֶζ¼ÓÉÒ»¸ö»ò¶à¸ö tab ·Ö¸ô¡£ÔÚÈÕÆÚ£¨×Ö¶Î1£¬$1£©Ö®ºó£¬ÓÐÁ½¸ö×ֶνÐ×ö¡°·ÑÓ÷ÖÀàÕÊ¡±ºÍ¡°ÊÕÈë·ÖÀàÕÊ¡±¡£ÒÔÉÏÃæÕâÐÐΪÀý£¬ÊäÈë·ÑÓÃʱ£¬ÎÒÔÚ·ÑÓÃ×Ö¶ÎÖзÅÈëËĸö×ÖĸµÄ±ðÃû£¬ÔÚÊÕÈë×Ö¶ÎÖзÅÈë"-"£¨¿Õ°×Ï¡£Õâ±íʾÕâÒ»Ìض¨ÏîÊÇ¡°Ê³Æ··ÑÓᱡ£:) ÒÔÏÂÊÇ´æ¿îµÄʾÀý£ºÒýÓÃ:[indent]23 Aug 2000 - inco - Y Boss Man 2001.00[/indent]
ÔÚÕâ¸öʵÀýÖУ¬ÎÒÔÚ·ÑÓ÷ÖÀàÕÊÖзÅÈë "-"£¨¿Õ°×£©£¬ÔÚÊÕÈë·ÖÀàÕÊÖзÅÈë "inco"¡£"inco"ÊÇÒ»°ã£¨Ð½Ë®Ö®ÀࣩÊÕÈëµÄ±ðÃû¡£Ê¹Ó÷ÖÀàÕʱðÃûÈÃÎÒ¿ÉÒÔ°´Àà±ðÉú³ÉÊÕÈëºÍ·ÑÓõÄÃ÷ϸ·ÖÀàÕÊ¡£ÖÁÓڼǼµÄÆäÓಿ·Ö£¬ÆäËüËùÓÐ×ֶζ¼ÊDz»Ðè¼ÓÒÔ˵Ã÷µÄ¡£¡°ÊÇ·ñ¸¶Ç壿¡±×ֶΣ¨"Y" »ò "N"£©¼Ç¼Á˽»Ò×ÊÇ·ñÒѹýÕʵ½ÎÒµÄÕÊ»§£»³ý´ËÖ®Í⣬»¹ÓÐÒ»¸ö½»Ò×ÃèÊö£¬ºÍÒ»¸öÕýµÄÃÀÔª½ð¶î¡£

ÓÃÓÚ¼ÆË㵱ǰÓà¶îµÄËã·¨²»Ì«ÄÑ¡£awk Ö»ÐèÒªÒÀ´Î¶ÁȡÿһÐС£Èç¹ûÁгöÁË·ÑÓ÷ÖÀàÕÊ£¬µ«Ã»ÓÐÊÕÈë·ÖÀàÕÊ£¨Îª"-"£©£¬ÄÇôÕâÒ»Ïî¾ÍÊǽ跽¡£Èç¹ûÁгöÁËÊÕÈë·ÖÀàÕÊ£¬µ«Ã»ÓзÑÓ÷ÖÀàÕÊ£¨Îª"-"£©£¬ÄÇôÕâÒ»Ïî¾ÍÊÇ´û·½¡£¶øÇÒ£¬Èç¹ûͬʱÁгöÁË·ÑÓúÍÊÕÈë·ÖÀàÕÊ£¬ÄÇôÕâ¸ö½ð¶î¾ÍÊÇ¡°·ÖÀàÕÊתÕÊ¡±£»¼´£¬´Ó·ÑÓ÷ÖÀàÕʼõÈ¥ÃÀÔª½ð¶î£¬²¢½«´Ë½ð¶îÌí¼Óµ½ÊÕÈë·ÖÀàÕÊ¡£´ËÍ⣬ËùÓÐÕâЩ·ÖÀàÕʶ¼ÊÇÐéÄâµÄ£¬µ«¶ÔÓÚ¸ú×ÙÊÕÈëºÍÖ§³öÒÔ¼°Ô¤ËãÈ´·Ç³£ÓÐÓá£

´úÂë

ÏÖÔÚ¸ÃÑо¿´úÂëÁË¡£ÎÒÃǽ«´ÓµÚÒ»ÐУ¨BEGIN ¿éºÍº¯Êý¶¨Ò壩¿ªÊ¼£º

balance£¬µÚ 1 ²¿·Ö¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:#!/usr/bin/env awk -f
BEGIN {
FS="\t+"
months="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
}
function monthdigit(mymonth) {
return (index(months,mymonth)+3)/4
}
Ê×ÏÈÖ´ÐÐ "chmod +x myscript" ÃüÁÄÇô½«µÚÒ»ÐÐ "#!..." Ìí¼Óµ½ÈκΠawk ½Å±¾½«Ê¹Ëü¿ÉÒÔÖ±½Ó´Ó shellÖÐÖ´ÐС£ÆäÓàÐж¨ÒåÁË BEGIN ¿é£¬ÔÚ awk ¿ªÊ¼´¦Àí֧Ʊ²¾Îļþ֮ǰ½«Ö´ÐÐÕâ¸ö´úÂë¿é¡£ÎÒÃǽ« FS£¨×ֶηָô·û£©ÉèÖóÉ"\t+"£¬Ëü»á¸æËß awk ×Ö¶ÎÓÉÒ»¸ö»ò¶à¸ö tab ·Ö¸ô¡£ÁíÍ⣬ÎÒÃǶ¨ÒåÁË×Ö·û´® months£¬ÏÂÃ潫³öÏÖµÄ monthdigit()º¯Êý½«Ê¹ÓÃËü¡£

×îºóÈýÐÐÏÔʾÁËÈçºÎ¶¨Òå×Ô¼ºµÄ awk ¡£¸ñʽºÜ¼òµ¥ -- ÊäÈë"function"£¬ÔÙÊäÈëÃû³Æ£¬È»ºóÔÚÀ¨ºÅÖÐÊäÈëÓɶººÅ·Ö¸ôµÄ²ÎÊý¡£ÔÚ´ËÖ®ºó£¬"{ }"´úÂë¿é°üº¬ÁËÄúÏ£ÍûÕâ¸öº¯ÊýÖ´ÐеĴúÂë¡£ËùÓк¯Êý¶¼¿ÉÒÔ·ÃÎÊÈ«¾Ö±äÁ¿£¨Èç months ±äÁ¿£©¡£ÁíÍ⣬awk ÌṩÁË "return"Óï¾ä£¬ËüÔÊÐíº¯Êý·µ»ØÒ»¸öÖµ£¬²¢Ö´ÐÐÀàËÆÓÚ C ºÍÆäËüÓïÑÔÖÐ "return" µÄ²Ù×÷¡£Õâ¸öÌض¨º¯Êý½«ÒÔ 3¸ö×Öĸ×Ö·û´®¸ñʽ±íʾµÄÔ·ÝÃû³Æת»»³ÉµÈ¼ÛµÄÊýÖµ¡£ÀýÈ磬ÒÔÏ´úÂ룺¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:print monthdigit("Mar")
¡­¡­½«´òÓ¡£ºÒýÓÃ:[indent]3[/indent]
ÏÖÔÚ£¬ÈÃÎÒÃÇÌÖÂÛÆäËüһЩº¯Êý¡£

²ÆÎñº¯Êý

ÒÔÏÂÊÇÆäËüÈý¸öÖ´Ðв¾¼ÇµÄº¯Êý¡£ÎÒÃǼ´½«¼ûµ½µÄÖ÷´úÂë¿é½«µ÷ÓÃÕâЩº¯ÊýÖ®Ò»£¬°´Ë³Ðò´¦Àí֧Ʊ²¾ÎļþµÄÿһÐУ¬´Ó¶ø½«ÏàÓ¦½»Ò׼Ǽµ½ awkÊý×éÖС£ÓÐÈýÖÖ»ù±¾½»Ò×£¬´û·½ (doincome)¡¢½è·½ (doexpense) ºÍתÕÊ(dotransfer)¡£Äú»á·¢ÏÖÕâÈý¸öº¯ÊýÈ«¶¼½ÓÊÜÒ»¸ö×Ô±äÁ¿£¬½Ð×÷ mybalance¡£mybalanceÊǶþάÊý×éµÄÒ»¸öռλ·û£¬ÎÒÃǽ«Ëü×÷Ϊ×Ô±äÁ¿½øÐд«µÝ¡£Ä¿Ç°£¬ÎÒÃÇ»¹Ã»Óд¦Àí¹ý¶þάÊý×飻µ«ÊÇ£¬ÔÚÏÂÃæ¿ÉÒÔ¿´µ½£¬Óï·¨·Ç³£¼òµ¥¡£Ö»ÐëÓöººÅ·Ö¸ôÿһά¾ÍÐÐÁË¡£

ÎÒÃǽ«°´ÒÔÏ·½Ê½½«ÐÅÏ¢¼Ç¼µ½ "mybalance" ÖС£Êý×éµÄµÚһά´Ó 0 µ½ 12£¬ÓÃÓÚÖ¸¶¨Ô·ݣ¬0´ú±íÈ«Äê¡£µÚ¶þάÊÇËĸö×ÖĸµÄ·ÖÀàÕÊ£¬Èç "food" »ò "inco"£»ÕâÊÇÎÒÃÇ´¦ÀíµÄÕæʵ·ÖÀàÕÊ¡£Òò´Ë£¬Òª²éÕÒÈ«ÄêʳƷ·ÖÀàÕʵÄÓà¶î£¬Ó¦²é¿´mybalance[0,"food"]¡£Òª²éÕÒ 6 ÔµÄÊÕÈ룬Ӧ²é¿´ mybalance[6,"inco"]¡£

balance£¬µÚ 2 ²¿·Ö¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:function doincome(mybalance) {
mybalance[curmonth,$3] += amount
mybalance[0,$3] += amount
}
function doexpense(mybalance) {
mybalance[curmonth,$2] -= amount
mybalance[0,$2] -= amount
}
function dotransfer(mybalance) {
mybalance[0,$2] -= amount
mybalance[curmonth,$2] -= amount
mybalance[0,$3] += amount
mybalance[curmonth,$3] += amount
}
µ÷Óà doincome() »òÈκÎÆäËüº¯Êýʱ£¬ÎÒÃǽ«½»Ò׼Ǽµ½Á½¸öλÖà -- mybalance[0,category] ºÍmybalance[curmonth,category]£¬ËüÃÇ·Ö±ð±íʾȫÄêµÄ·ÖÀàÕÊÓà¶îºÍµ±ÔµķÖÀàÕÊÓà¶î¡£ÕâÈÃÎÒÃÇÉÔºó¿ÉÒÔÇáËɵØÉú³ÉÄê¶È»òÔ¶ÈÊÕÈ룯֧³öÃ÷ϸ·ÖÀàÕÊ¡£

Èç¹ûÑо¿ÕâЩº¯Êý£¬½«·¢ÏÖÔÚÎÒµÄÒýÓÃÖд«µÝÁË mybalanceÒýÓõÄÊý×é¡£ÁíÍ⣬ÎÒÃÇ»¹ÒýÓÃÁ˼¸¸öÈ«¾Ö±äÁ¿£ºcurmonth£¬Ëü±£´æÁ˵±Ç°¼Ç¼ËùÊôµÄÔ·ݵÄÊýÖµ£¬$2£¨·ÑÓ÷ÖÀàÕÊ£©£¬$3£¨ÊÕÈë·ÖÀàÕÊ£©ºÍ½ð¶î£¨$7£¬ÃÀÔª½ð¶î£©¡£µ÷Óà doincome() ºÍÆäËüº¯Êýʱ£¬ÒѾ­ÎªÒª´¦ÀíµÄµ±Ç°¼Ç¼£¨ÐУ©ÕýÈ·ÉèÖÃÁËËùÓÐÕâЩ±äÁ¿¡£

Ö÷¿é

ÒÔÏÂÊÇÖ÷´úÂë¿é£¬Ëü°üº¬ÁË·ÖÎöÿһÐÐÊäÈëÊý¾ÝµÄ´úÂë¡£Çë¼Çס£¬ÓÉÓÚÕýÈ·ÉèÖÃÁË FS£¬¿ÉÒÔÓà $ 1 ÒýÓõÚÒ»¸ö×ֶΣ¬Óà $2ÒýÓõڶþ¸ö×ֶΣ¬ÒÀ´ÎÀàÍÆ¡£µ÷Óà doincome() ºÍÆäËüº¯Êýʱ£¬ÕâЩº¯Êý¿ÉÒÔ´Óº¯ÊýÄÚ²¿·ÃÎÊ curmonth¡¢$2¡¢$3ºÍ½ð¶îµÄµ±Ç°Öµ¡£ÇëÏÈÑо¿´úÂ룬ÔÚ´úÂëÖ®ºó¿ÉÒÔ¼ûµ½ÎÒµÄ˵Ã÷¡£

balance£¬µÚ 3 ²¿·Ö¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:{
curmonth=monthdigit(substr($1,4,3))
amount=$7

#record all the categories encountered
if ( $2 != "-" )
globcat[$2]="yes"
if ( $3 != "-" )
globcat[$3]="yes"
#tally up the transaction properly
if ( $2 == "-" ) {
if ( $3 == "-" ) {
print "Error: inc and exp fields are both blank!"
exit 1
} else {
#this is income
doincome(balance)
if ( $5 == "Y" )
doincome(balance2)
}
} else if ( $3 == "-" ) {
#this is an expense
doexpense(balance)
if ( $5 == "Y" )
doexpense(balance2)
} else {
#this is a transfer
dotransfer(balance)
if ( $5 == "Y" )
dotransfer(balance2)
}
}
ÔÚÖ÷¿éÖУ¬Ç°Á½Ðн« curmonth ÉèÖÃ³É 1 µ½ 12 Ö®¼äµÄÕûÊý£¬²¢½«½ð¶îÉèÖóÉ×Ö¶Î7£¨Ê¹´úÂëÒ×ÓÚÀí½â£©¡£È»ºó£¬ÊÇËÄÐÐÓÐȤµÄ´úÂ룬ËüÃǽ«ÖµÐ´µ½Êý×é globcatÖС£globcat£¬»ò³Æ×÷È«¾Ö·ÖÀàÕÊÊý×飬ÓÃÓڼǼÔÚÎļþÖÐÓöµ½µÄËùÓзÖÀàÕÊ -- "inco"¡¢"misc"¡¢"food"¡¢"util"µÈ¡£ÀýÈ磬Èç¹û $2 == "inco"£¬Ôò½« globcat["inco"] ÉèÖÃ³É "yes"¡£ÉÔºó£¬ÎÒÃÇ¿ÉÒÔʹÓüòµ¥µÄ "for (xin globcat)" Ñ­»·À´µü´ú·ÖÀàÕÊÁÐ±í¡£

ÔÚ½Ó×ŵĴóÔ¼¶þÊ®ÐÐÖУ¬ÎÒÃÇ·ÖÎö×ֶΠ$2 ºÍ $3£¬²¢Êʵ±¼Ç¼½»Òס£Èç¹û $2=="-" ÇÒ $3!="-"£¬±íʾÎÒÃÇÓÐÊÕÈ룬Òò´Ëµ÷ÓÃdoincome()¡£Èç¹ûÊÇÏà·´µÄÇé¿ö£¬Ôòµ÷Óà doexpense()£»Èç¹û $2 ºÍ $3 ¶¼°üº¬·ÖÀàÕÊ£¬Ôòµ÷ÓÃdotransfer()¡£Ã¿´ÎÎÒÃǶ¼½« "balance" Êý×é´«µÝ¸øÕâЩº¯Êý£¬´Ó¶øÔÚÕâЩº¯ÊýÖмǼÊʵ±µÄÊý¾Ý¡£

Äú»¹»á·¢ÏÖ¼¸ÐдúÂë˵¡°if ( $5 == "Y" )£¬ÄÇô½«Í¬Ò»¸ö½»Ò׼Ǽµ½ balance2 ÖС±¡£ÎÒÃÇÔÚÕâÀ﾿¾¹×öÁËЩʲô£¿Äú½«»ØÒäÆð$5 °üº¬ "Y" »ò "N"£¬²¢¼Ç¼½»Ò×ÊÇ·ñÒѾ­¹ýÕʵ½ÕÊ»§¡£ÓÉÓÚ½öµ±¹ýÕÊÁ˽»Ò×ʱÎÒÃDzŽ«½»Ò׼Ǽµ½ balance2£¬Òò´Ë balance2°üº¬ÁËÕæʵµÄÕÊ»§Óà¶î£¬¶ø "balance" °üº¬ÁËËùÓн»Ò×£¬²»¹ÜÊÇ·ñÒѾ­¹ýÕÊ¡£¿ÉÒÔʹÓà balance2À´ÑéÖ¤Êý¾ÝÏÒòΪËüÓ¦¸ÃÓ뵱ǰÒøÐÐÕÊ»§Óà¶îÆ¥Å䣩£¬¿ÉÒÔʹÓà "balance" À´È·±£Ã»ÓÐ͸֧ÕÊ»§£¨ÒòΪËü»á¿¼ÂÇÄú¿ª³öµÄÉÐδ¶ÒÏÖµÄËùÓÐ֧Ʊ£©¡£

Éú³É±¨±í

Ö÷¿éÖظ´´¦ÀíÁËÿһÐмǼ֮ºó£¬ÏÖÔÚÎÒÃÇÓÐÁ˹ØÓڱȽÏÈ«ÃæµÄ¡¢°´·ÖÀàÕʺͰ´Ô·ݻ®·ÖµÄ½è·½ºÍ´û·½¼Ç¼¡£ÏÖÔÚ£¬ÔÚÕâÖÖÇé¿öÏÂ×îºÏÊʵÄ×ö·¨ÊÇÖ»Ð붨ÒåÉú³É±¨±íµÄ END ¿é£º

balance£¬µÚ 4 ²¿·Ö¸´ÖÆÄÚÈݵ½¼ôÌù°å´úÂë:END {
bal=0
bal2=0
for (x in globcat) {
bal=bal+balance[0,x]
bal2=bal2+balance2[0,x]
}
printf("Your available funds: %10.2f\n", bal)
printf("Your account balance: %10.2f\n", bal2)
}
Õâ¸ö±¨±í½«´òÓ¡³ö»ã×Ü£¬ÈçÏÂËùʾ£ºÒýÓÃ:[indent]Your available funds:1174.22
Your account balance:2399.33[/indent]
ÔÚEND ¿éÖУ¬ÎÒÃÇʹÓà "for (x in globcat)"½á¹¹À´µü´úÿһ¸ö·ÖÀàÕÊ£¬¸ù¾Ý¼Ç¼ÔÚ°¸µÄ½»Ò×½áËãÖ÷ÒªÓà¶î¡£Êµ¼ÊÉÏ£¬ÎÒÃǽáËãÁ½¸öÓà¶î£¬Ò»¸öÊÇ¿ÉÓÃ×ʽð£¬ÁíÒ»¸öÊÇÕÊ»§Óà¶î¡£ÒªÖ´ÐгÌÐò²¢´¦ÀíÄúÔÚÎļþ"mycheckbook.txt" ÖÐÊäÈëµÄ²ÆÎñÊý¾Ý£¬½«ÒÔÉÏËùÓдúÂë·ÅÈëÎı¾Îļþ "balance"£¬Ö´ÐÐ "chmod +xbalance"£¬È»ºóÊäÈë "./balance mycheckbook.txt"¡£È»ºó balance½Å±¾½«ºÏ¼ÆËùÓн»Ò×£¬´òÓ¡³öÁ½ÐÐÓà¶î»ã×Ü¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2 ÌõÆÀÂÛ

  1. snnq ÓÚ 2009-10-31 14:16:23·¢±í:

    лл·ÖÏí

  2. »¨Ã¨ ÓÚ 2009-07-13 20:07:35·¢±í:

    ):o:s¶Ô¼ÆËã»úÓïÑÔÒ»µãÒ²²»¶®£¬´¿Ö§³ÖÒ»¸ö°É