Ò». AWK ˵Ã÷
awkÊÇÒ»ÖÖ±à³ÌÓïÑÔ£¬ÓÃÓÚÔÚlinux/unix϶ÔÎı¾ºÍÊý¾Ý½øÐд¦Àí¡£Êý¾Ý¿ÉÒÔÀ´×Ô±ê×¼ÊäÈë¡¢Ò»¸ö»ò¶à¸öÎļþ£¬»òÆäËüÃüÁîµÄÊä³ö¡£ËüÖ§³ÖÓû§×Ô¶¨Ò庯ÊýºÍ¶¯Ì¬ÕýÔò±í´ïʽµÈÏȽø¹¦ÄÜ£¬ÊÇlinux/unixϵÄÒ»¸öÇ¿´ó±à³Ì¹¤¾ß¡£ËüÔÚÃüÁîÐÐÖÐʹÓ㬵«¸ü¶àÊÇ×÷Ϊ½Å±¾À´Ê¹Óá£
awkµÄ´¦ÀíÎı¾ºÍÊý¾ÝµÄ·½Ê½£ºËüÖðÐÐɨÃèÎļþ£¬´ÓµÚÒ»Ðе½×îºóÒ»ÐУ¬Ñ°ÕÒÆ¥ÅäµÄÌض¨Ä£Ê½µÄÐУ¬²¢ÔÚÕâЩÐÐÉϽøÐÐÄãÏëÒªµÄ²Ù×÷¡£Èç¹ûûÓÐÖ¸¶¨´¦Àí¶¯×÷£¬Ôò°ÑÆ¥ÅäµÄÐÐÏÔʾµ½±ê×¼Êä³ö(ÆÁÄ»)£¬Èç¹ûûÓÐÖ¸¶¨Ä£Ê½£¬ÔòËùÓб»²Ù×÷ËùÖ¸¶¨µÄÐж¼±»´¦Àí¡£
awk·Ö±ð´ú±íÆä×÷ÕßÐÕÊϵĵÚÒ»¸ö×Öĸ¡£ÒòΪËüµÄ×÷ÕßÊÇÈý¸öÈË£¬·Ö±ðÊÇAlfred Aho¡¢Brian Kernighan¡¢Peter Weinberger¡£
gawkÊÇawkµÄGNU°æ±¾£¬ËüÌṩÁËBellʵÑéÊÒºÍGNUµÄһЩÀ©Õ¹¡£ÏÂÃæ½éÉܵÄawkÊÇÒÔGUNµÄgawkΪÀýµÄ£¬ÔÚlinuxϵͳÖÐÒÑ°ÑawkÁ´½Óµ½gawk£¬ËùÒÔÏÂÃæÈ«²¿ÒÔawk½øÐнéÉÜ¡£
¶þ. awkÃüÁî¸ñʽºÍÑ¡Ïî
awk [options] 'script' var=value file(s)
awk [options] -f scriptfile var=value file(s)
£¨1£©-F fs or --field-separator fs £ºÖ¸¶¨ÊäÈëÎļþÕÛ·Ö¸ô·û£¬fsÊÇÒ»¸ö×Ö·û´®»òÕßÊÇÒ»¸öÕýÔò±í´ïʽ£¬Èç-F:¡£
£¨2£©-v var=value or --asign var=value £º¸³ÖµÒ»¸öÓû§¶¨Òå±äÁ¿¡£
£¨3£©-f scripfile or --file scriptfile £º´Ó½Å±¾ÎļþÖжÁÈ¡awkÃüÁî¡£
£¨4£©-mf nnn and -mr nnn £º¶ÔnnnÖµÉèÖÃÄÚÔÚÏÞÖÆ£¬-mfÑ¡ÏîÏÞÖÆ·ÖÅä¸ønnnµÄ×î´ó¿éÊýÄ¿£»-mrÑ¡ÏîÏÞÖƼǼµÄ×î´óÊýÄ¿¡£ÕâÁ½¸ö¹¦ÄÜÊÇBellʵÑéÊÒ°æawkµÄÀ©Õ¹¹¦ÄÜ£¬ÔÚ±ê×¼awkÖв»ÊÊÓá£
£¨5£©-W compact or --compat, -W traditional or --traditional £ºÔÚ¼æÈÝģʽÏÂÔËÐÐawk¡£ËùÒÔgawkµÄÐÐΪºÍ±ê×¼µÄawkÍêÈ«Ò»Ñù£¬ËùÓеÄawkÀ©Õ¹¶¼±»ºöÂÔ¡£
£¨6£©-W copyleft or --copyleft, -W copyright or --copyright £º´òÓ¡¼ò¶ÌµÄ°æȨÐÅÏ¢¡£
£¨7£©-W help or --help, -W usage or --usage £º´òÓ¡È«²¿awkÑ¡ÏîºÍÿ¸öÑ¡ÏîµÄ¼ò¶Ì˵Ã÷¡£
£¨8£©-W lint or --lint £º´òÓ¡²»ÄÜÏò´«Í³unixƽ̨ÒÆÖ²µÄ½á¹¹µÄ¾¯¸æ¡£
£¨9£©-W lint-old or --lint-old £º´òÓ¡¹ØÓÚ²»ÄÜÏò´«Í³unixƽ̨ÒÆÖ²µÄ½á¹¹µÄ¾¯¸æ¡£
£¨10£©-W posix £º´ò¿ª¼æÈÝģʽ¡£µ«ÓÐÒÔÏÂÏÞÖÆ£¬²»Ê¶±ð£º/x¡¢º¯Êý¹Ø¼ü×Ö¡¢func¡¢»»ÂëÐòÁÐÒÔ¼°µ±fsÊÇÒ»¸ö¿Õ¸ñʱ£¬½«ÐÂÐÐ×÷Ϊһ¸öÓò·Ö¸ô·û£»²Ù×÷·û**ºÍ**=²»ÄÜ´úÌæ^ºÍ^=£»fflushÎÞЧ¡£
£¨11£©-W re-interval or --re-inerval £ºÔÊÐí¼ä¸ôÕýÔò±í´ïʽµÄʹÓ㬲ο¼(grepÖеÄPosix×Ö·ûÀà)£¬ÈçÀ¨ºÅ±í´ïʽ[[:alpha:]]¡£
£¨12£©-W source program-text or --source program-text £ºÊ¹ÓÃprogram-text×÷ΪԴ´úÂ룬¿ÉÓë-fÃüÁî»ìÓá£
£¨13£©-W version or --version £º´òÓ¡bug±¨¸æÐÅÏ¢µÄ°æ±¾¡£
Èý. ģʽºÍ²Ù×÷
awk½Å±¾ÊÇÓÉģʽºÍ²Ù×÷×é³ÉµÄ£º
pattern {action} Èç$ awk '/root/' test£¬»ò$ awk '$3 < 100' test¡£
Á½ÕßÊÇ¿ÉÑ¡µÄ£¬Èç¹ûûÓÐģʽ£¬ÔòactionÓ¦Óõ½È«²¿¼Ç¼£¬Èç¹ûûÓÐaction£¬ÔòÊä³öÆ¥ÅäÈ«²¿¼Ç¼¡£Ä¬ÈÏÇé¿öÏ£¬Ã¿Ò»¸öÊäÈëÐж¼ÊÇÒ»Ìõ¼Ç¼£¬µ«Óû§¿Éͨ¹ýRS±äÁ¿Ö¸¶¨²»Í¬µÄ·Ö¸ô·û½øÐзָô¡£
ģʽ¿ÉÒÔÊÇÒÔÏÂÈÎÒâÒ»¸ö£º
£¨1£©ÕýÔò±í´ïʽ£ºÊ¹ÓÃͨÅä·ûµÄÀ©Õ¹¼¯¡£
£¨2£©¹Øϵ±í´ïʽ£º¿ÉÒÔÓÃÏÂÃæÔËËã·û±íÖеĹØϵÔËËã·û½øÐвÙ×÷£¬¿ÉÒÔÊÇ×Ö·û£¨3£©´®»òÊý×ֵıȽϣ¬Èç$2>%1Ñ¡ÔñµÚ¶þ¸ö×ֶαȵÚÒ»¸ö×ֶ㤵ÄÐС£
£¨4£©Ä£Ê½Æ¥Åä±í´ïʽ£ºÓÃÔËËã·û~(Æ¥Åä)ºÍ~!(²»Æ¥Åä)¡£
£¨5£©Ä£Ê½£¬Ä£Ê½£ºÖ¸¶¨Ò»¸öÐеķ¶Î§¡£¸ÃÓï·¨²»ÄÜ°üÀ¨BEGINºÍENDģʽ¡£
£¨6£©BEGIN£ºÈÃÓû§Ö¸¶¨ÔÚµÚÒ»ÌõÊäÈë¼Ç¼±»´¦Àí֮ǰËù·¢ÉúµÄ¶¯×÷£¬Í¨³£¿ÉÔÚÕâÀïÉèÖÃÈ«¾Ö±äÁ¿¡£
£¨7£©END£ºÈÃÓû§ÔÚ×îºóÒ»ÌõÊäÈë¼Ç¼±»¶ÁÈ¡Ö®ºó·¢ÉúµÄ¶¯×÷¡£
3.2. ²Ù×÷
²Ù×÷ÓÉÒ»ÈË»ò¶à¸öÃüÁî¡¢º¯Êý¡¢±í´ïʽ×é³É£¬Ö®¼äÓÉ»»Ðзû»ò·ÖºÅ¸ô¿ª£¬²¢Î»ÓÚ´óÀ¨ºÅÄÚ¡£Ö÷ÒªÓÐËIJ¿·Ý£º
£¨1£©±äÁ¿»òÊý×鸳ֵ
£¨2£©Êä³öÃüÁî
£¨3£©ÄÚÖú¯Êý
£¨4£©¿ØÖÆÁ÷ÃüÁî
ËÄ. awkµÄ»·¾³±äÁ¿
ÃèÊö |
|
$n |
µ±Ç°¼Ç¼µÄµÚn¸ö×ֶΣ¬×ֶμäÓÉFS·Ö¸ô¡£ |
$0 |
ÍêÕûµÄÊäÈë¼Ç¼¡£ |
ARGC |
ÃüÁîÐвÎÊýµÄÊýÄ¿¡£ |
ARGIND |
ÃüÁîÐÐÖе±Ç°ÎļþµÄλÖÃ(´Ó0¿ªÊ¼Ëã)¡£ |
ARGV |
°üº¬ÃüÁîÐвÎÊýµÄÊý×é¡£ |
CONVFMT |
Êý×Öת»»¸ñʽ(ĬÈÏֵΪ%.6g) |
ENVIRON |
»·¾³±äÁ¿¹ØÁªÊý×é¡£ |
ERRNO |
×îºóÒ»¸öϵͳ´íÎóµÄÃèÊö¡£ |
FIELDWIDTHS |
×ֶοí¶ÈÁбí(Óÿոñ¼ü·Ö¸ô)¡£ |
FILENAME |
µ±Ç°ÎļþÃû¡£ |
FNR |
ͬNR£¬µ«Ïà¶ÔÓÚµ±Ç°Îļþ¡£ |
FS |
×ֶηָô·û(ĬÈÏÊÇÈκοոñ)¡£ |
IGNORECASE |
Èç¹ûΪÕ棬Ôò½øÐкöÂÔ´óСдµÄÆ¥Åä¡£ |
NF |
µ±Ç°¼Ç¼ÖеÄ×Ö¶ÎÊý¡£ |
NR |
µ±Ç°¼Ç¼Êý¡£ |
OFMT |
Êý×ÖµÄÊä³ö¸ñʽ(ĬÈÏÖµÊÇ%.6g)¡£ |
OFS |
Êä³ö×ֶηָô·û(ĬÈÏÖµÊÇÒ»¸ö¿Õ¸ñ)¡£ |
ORS |
Êä³ö¼Ç¼·Ö¸ô·û(ĬÈÏÖµÊÇÒ»¸ö»»Ðзû)¡£ |
RLENGTH |
ÓÉmatchº¯ÊýËùÆ¥ÅäµÄ×Ö·û´®µÄ³¤¶È¡£ |
RS |
¼Ç¼·Ö¸ô·û(ĬÈÏÊÇÒ»¸ö»»Ðзû)¡£ |
RSTART |
ÓÉmatchº¯ÊýËùÆ¥ÅäµÄ×Ö·û´®µÄµÚÒ»¸öλÖᣠ|
SUBSEP |
Êý×éϱê·Ö¸ô·û(ĬÈÏÖµÊÇ/034)¡£ |
Îå. awkÔËËã·û
ÃèÊö |
|
= += -= *= /= %= ^= **= |
¸³Öµ |
?: |
CÌõ¼þ±í´ïʽ |
|| |
Âß¼»ò |
&& |
Âß¼Óë |
~ ~! |
Æ¥ÅäÕýÔò±í´ïʽºÍ²»Æ¥ÅäÕýÔò±í´ïʽ |
< <= > >= != == |
¹ØϵÔËËã·û |
¿Õ¸ñ |
Á¬½Ó |
+ - |
¼Ó£¬¼õ |
* / & |
³Ë£¬³ýÓëÇóÓà |
+ - ! |
Ò»Ôª¼Ó£¬¼õºÍÂß¼·Ç |
^ *** |
ÇóÃÝ |
++ -- |
Ôö¼Ó»ò¼õÉÙ£¬×÷Ϊǰ׺»òºó׺ |
$ |
×Ö¶ÎÒýÓà |
in |
Êý×é³ÉÔ± |
Áù. ¼Ç¼ºÍÓò
awk°Ñÿһ¸öÒÔ»»Ðзû½áÊøµÄÐгÆΪһ¸ö¼Ç¼¡£
¼Ç¼·Ö¸ô·û£ºÄ¬ÈϵÄÊäÈëºÍÊä³öµÄ·Ö¸ô·û¶¼Êǻسµ£¬±£´æÔÚÄÚ½¨±äÁ¿ORSºÍRSÖС£
$0±äÁ¿£ºËüÖ¸µÄÊÇÕûÌõ¼Ç¼¡£Èç$ awk '{print $0}' test½«Êä³ötestÎļþÖеÄËùÓмǼ¡£
±äÁ¿NR£ºÒ»¸ö¼ÆÊýÆ÷£¬Ã¿´¦ÀíÍêÒ»Ìõ¼Ç¼£¬NRµÄÖµ¾ÍÔö¼Ó1¡£
Èç$ awk '{print NR,$0}' test½«Êä³ötestÎļþÖÐËùÓмǼ£¬²¢ÔڼǼǰÏÔʾ¼Ç¼ºÅ¡£
¼Ç¼ÖÐÿ¸öµ¥´Ê³Æ×ö¡°Óò¡±£¬Ä¬ÈÏÇé¿öÏÂÒÔ¿Õ¸ñ»òtab·Ö¸ô¡£awk¿É¸ú×ÙÓòµÄ¸öÊý£¬²¢ÔÚÄÚ½¨±äÁ¿NFÖб£´æ¸ÃÖµ¡£Èç$ awk '{print $1,$3}' test½«´òÓ¡testÎļþÖеÚÒ»ºÍµÚÈý¸öÒÔ¿Õ¸ñ·Ö¿ªµÄÁÐ(Óò)¡£
6.3. Óò·Ö¸ô·û
ÄÚ½¨±äÁ¿FS±£´æÊäÈëÓò·Ö¸ô·ûµÄÖµ£¬Ä¬ÈÏÊÇ¿Õ¸ñ»òtab¡£ÎÒÃÇ¿ÉÒÔͨ¹ý-FÃüÁîÐÐÑ¡ÏîÐÞ¸ÄFSµÄÖµ¡£Èç$ awk -F: '{print $1,$5}' test½«´òÓ¡ÒÔðºÅΪ·Ö¸ô·ûµÄµÚÒ»£¬µÚÎåÁеÄÄÚÈÝ¡£
¿ÉÒÔͬʱʹÓöà¸öÓò·Ö¸ô·û£¬ÕâʱӦ¸Ã°Ñ·Ö¸ô·ûд³É·Åµ½·½À¨ºÅÖУ¬Èç$awk -F'[:/t]' '{print $1,$3}' test£¬±íʾÒÔ¿Õ¸ñ¡¢Ã°ºÅºÍtab×÷Ϊ·Ö¸ô·û¡£
Êä³öÓòµÄ·Ö¸ô·ûĬÈÏÊÇÒ»¸ö¿Õ¸ñ£¬±£´æÔÚOFSÖС£Èç$ awk -F: '{print $1,$5}' test£¬$1ºÍ$5¼äµÄ¶ººÅ¾ÍÊÇOFSµÄÖµ¡£
Æß. gawkרÓÃÕýÔò±í´ïʽԪ×Ö·û
ÒÔϼ¸¸öÊÇgawkרÓõģ¬²»ÊʺÏunix°æ±¾µÄawk¡£
£¨1£©/Y £ºÆ¥ÅäÒ»¸öµ¥´Ê¿ªÍ·»òÕßĩβµÄ¿Õ×Ö·û´®¡£
£¨2£©/B£ºÆ¥Åäµ¥´ÊÄڵĿÕ×Ö·û´®¡£
£¨3£©/<£ºÆ¥ÅäÒ»¸öµ¥´ÊµÄ¿ªÍ·µÄ¿Õ×Ö·û´®£¬Ãª¶¨¿ªÊ¼¡£
£¨4£©/> £ºÆ¥ÅäÒ»¸öµ¥´ÊµÄĩβµÄ¿Õ×Ö·û´®£¬Ãª¶¨Ä©Î²¡£
£¨5£©/w £ºÆ¥ÅäÒ»¸ö×ÖĸÊý×Ö×é³ÉµÄµ¥´Ê¡£
£¨6£©/W £ºÆ¥ÅäÒ»¸ö·Ç×ÖĸÊý×Ö×é³ÉµÄµ¥´Ê¡£
£¨7£©/¡®£ºÆ¥Åä×Ö·û´®¿ªÍ·µÄÒ»¸ö¿Õ×Ö·û´®¡£
£¨8£©/' £ºÆ¥Åä×Ö·û´®Ä©Î²µÄÒ»¸ö¿Õ×Ö·û´®¡£
ÓÃÀ´ÔڼǼ»òÕßÓòÄÚÆ¥ÅäÕýÔò±í´ïʽ¡£Èç$ awk '$1 ~/^root/' test½«ÏÔʾtestÎļþµÚÒ»ÁÐÖÐÒÔroot¿ªÍ·µÄÐС£
conditional expression1 ? expression2: expression3£¬
ÀýÈ磺$ awk '{max = {$1 > $3} ? $1: $3: print max}' test¡£Èç¹ûµÚÒ»¸öÓò´óÓÚµÚÈý¸öÓò£¬$1¾Í¸³Öµ¸ømax£¬·ñÔò$3¾Í¸³Öµ¸ømax¡£
$ awk '$1 + $2 < 100' test¡£Èç¹ûµÚÒ»ºÍµÚ¶þ¸öÓòÏà¼Ó´óÓÚ100£¬Ôò´òÓ¡ÕâЩÐС£
$ awk '$1 > 5 && $2 < 10' test,Èç¹ûµÚÒ»¸öÓò´óÓÚ5£¬²¢ÇÒµÚ¶þ¸öÓòСÓÚ10£¬Ôò´òÓ¡ÕâЩÐС£
·¶Î§Ä£°åÆ¥Åä´ÓµÚÒ»¸öÄ£°åµÄµÚÒ»´Î³öÏÖµ½µÚ¶þ¸öÄ£°åµÄµÚÒ»´Î³öÏÖÖ®¼äËùÓÐÐС£Èç¹ûÓÐÒ»¸öÄ£°åû³öÏÖ£¬ÔòÆ¥Åäµ½¿ªÍ·»òĩβ¡£Èç$ awk '/root/,/mysql/' test½«ÏÔʾrootµÚÒ»´Î³öÏÖµ½mysqlµÚÒ»´Î³öÏÖÖ®¼äµÄËùÓÐÐС£
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} </span></span></span></p>
<p class="MsoNormal" style="margin:0cm 0cm 0pt;"> BEGIN ±íʾÔÚ´¦ÀíÈÎÒâÐÐ֮ǰ½øÐеIJÙ×÷¡£
{max=($1 >max ?$1:max); print $1,"Now max is "max}' 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<NF) {print NF,$i;i++}}' file ͨ¹ýwhileÓï¾äʵÏÖÑ»·¡£
awk '{ for(i=1;i<NF;i++) {print NF,$i}}' file ͨ¹ýforÓï¾äʵÏÖÑ»·¡£
type file|awk -F "/" '
{ for(i=1;i<NF;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 "dd ",j,i}
}
}
}'
19¡¢ÔÚawkÖе÷ÓÃϵͳ±äÁ¿±ØÐëÓõ¥ÒýºÅ£¬Èç¹ûÊÇË«ÒýºÅ£¬Ôò±íʾ×Ö·û´®
Flag=abcd
awk '{print '$Flag'}' ½á¹ûΪabcd
awk '{print "$Flag"}' ½á¹ûΪ$Flag
20. ÆäËûСʾÀý
$ awk '/^(no|so)/' test-----´òÓ¡ËùÓÐÒÔģʽno»òso¿ªÍ·µÄÐС£
$ awk '/^[ns]/{print $1}' test-----Èç¹û¼Ç¼ÒÔn»òs¿ªÍ·£¬¾Í´òÓ¡Õâ¸ö¼Ç¼¡£
$ awk '$1 ~/[0-9][0-9]$/(print $1}' test-----Èç¹ûµÚÒ»¸öÓòÒÔÁ½¸öÊý×Ö½áÊø¾Í´òÓ¡Õâ¸ö¼Ç¼¡£
$ awk '$1 == 100 || $2 < 50' test-----Èç¹ûµÚÒ»¸ö»òµÈÓÚ100»òÕßµÚ¶þ¸öÓòСÓÚ50£¬Ôò´òÓ¡¸ÃÐС£
$ awk '$1 != 10' test-----Èç¹ûµÚÒ»¸öÓò²»µÈÓÚ10¾Í´òÓ¡¸ÃÐС£
$ awk '/test/{print $1 + 10}' test-----Èç¹û¼Ç¼°üº¬ÕýÔò±í´ïʽtest£¬ÔòµÚÒ»¸öÓò¼Ó10²¢´òÓ¡³öÀ´¡£
$ awk '{print ($1 > 5 ? "ok "$1: "error"$1)}' test-----Èç¹ûµÚÒ»¸öÓò´óÓÚ5Ôò´òÓ¡ÎʺźóÃæµÄ±í´ïʽֵ£¬·ñÔò´òӡðºÅºóÃæµÄ±í´ïʽֵ¡£
$ awk '/^root/,/^mysql/' test----´òÓ¡ÒÔÕýÔò±í´ïʽroot¿ªÍ·µÄ¼Ç¼µ½ÒÔÕýÔò±í´ïʽmysql¿ªÍ·µÄ¼Ç¼·¶Î§ÄÚµÄËùÓмǼ¡£Èç¹ûÕÒµ½Ò»¸öеÄÕýÔò±í´ïʽroot¿ªÍ·µÄ¼Ç¼£¬Ôò¼ÌÐø´òÓ¡Ö±µ½ÏÂÒ»¸öÒÔÕýÔò±í´ïʽmysql¿ªÍ·µÄ¼Ç¼Ϊֹ£¬»òµ½Îļþĩβ¡£
ÍƼöÐÅÏ¢
- linuxÃüÁîѧϰ±Ê¼Ç£¨11£©£ºnlÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨5£©£ºrmÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨4£©£ºmkdirÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨1£©£ºlsÃüÁî
- ½«CentosµÄyumÔ´¸ü»»Îª¹úÄڵİ¢ÀïÔÆÔ´
- ʹÓÃNginxÌí¼Óheader·ÀÖ¹ÍøÒ³±»frame
- linuxϼÓËÙscp´«Êä´óÎļþµÄËÙ¶È
- linuxϵͳÉ϶ÔnginxÈÕÖ¾·Ö¸î´¦Àí
- lnmp/nginxϵͳÕæÕýÓÐЧµÄͼƬ·ÀµÁÁ´ÍêÕûÉèÖÃÏê½â
- Í»ÆÆÊ®Íò²¢·¢µÄNginxµÄÅäÖü°ÓÅ»¯
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- ²éÕÒ²¢É¾³ý.svnĿ¼Îļþ
- redis ÆßÖÖÊý¾ÝÀàÐ͵ÄʹÓó¡¾°
- linux ÏÂÎļþ¸´ÖƵ½windowsÏÂÂÒÂëµÄ½â¾ö°ì·¨
- nginx³öÏÖ502 upstream sent too big he....
- linuxÏÂsudoÅäÖÃÏê½â
- linuxÃüÁîѧϰ±Ê¼Ç£¨15£©£ºtailÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨14£©£ºheadÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨13£©£ºlessÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨12£©£ºmoreÃüÁî
- ¼ÓÃÜËã·¨±È½Ï3DES AES RSA ECC MD5 SHA1µÈ
ÆÀÂÛ