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

AWK:Linux¹ÜÀíÔ±µÄÖÇÄܹ¤¾ß°ü(¶þ)

·¢²¼Ê±¼ä:2005-09-10 23:35:44À´Ô´:ºìÁª×÷Õß:aini
Ô±¹¤ 46026 ºÍ 46027 ûÓÐסÔÚÑÇÀûÉ£ÄÇÖÝ£»µ«ÊÇËûÃǵÄÃû×ÖÖаüº¬ËùËÑË÷µÄ×Ö·ûÐòÁС£Çмǣ¬µ±ÔÚ AWK ÖнøÐÐģʽƥÅäʱ£¬ÀýÈç grep¡¢sed »òÕߴ󲿷ÖÆäËû Linux/Unix ÃüÁ½«ÔڼǼ£¨ÐУ©ÖеÄÈκÎλÖòéÕÒÆ¥Å䣬³ý·ÇÖ¸¶¨½øÐÐÆäËû²Ù×÷¡£Îª½â¾öÕâÒ»ÎÊÌ⣬±ØÐ뽫ËÑË÷ÓëÌض¨×Ö¶ÎÁªÏµÆðÀ´¡£Í¨¹ýÀûÓôú×ֺŠ(?) ÒÔ¼°¶ÔÌض¨×ֶεÄ˵Ã÷£¬¿ÉÒÔ´ïµ½ÕâһĿµÄ£¬ÈçÏÂÀýËùʾ£º



$ awk '$5 ? /AR/' emp_names
46019 BOGUE ROBERT PHOENIX AZ
46021 JUNE MICAH PHOENIX AZ
46022 KANE SHERYL UNKNOWN AZ
$

¡¡¡¡´ú×ֺţ¨±íʾƥÅ䣩µÄ¶ÔÓ¦·ûºÅÊÇÒ»¸öÇ°Ãæ´øÓиÐ̾ºÅµÄ´ú×ֺŠ(!?)¡£ÕâЩ×Ö·û֪ͨ³ÌÐò£¬Èç¹ûËÑË÷ÐòÁÐûÓгöÏÖÔÚÖ¸¶¨×Ö¶ÎÖУ¬ÔòÕÒ³öÓëËÑË÷ÐòÁÐÏàÆ¥ÅäµÄËùÓÐÐУº

$ awk '$5 !? /AR/' names
46012 DULANEY EVAN MOBILE AL
46013 DURHAM JEFF MOBILE AL
46015 STEEN BILL MOBILE AL
46017 FELDMAN EVAN MOBILE AL
46018 SWIM STEVE UNKNOWN AL
46024 WOOD WILLIAM MUNCIE IN
46026 FERGUS SARAH MUNCIE IN
46027 BUCK SARAH MUNCIE IN
46029 TUTTLE BOB MUNCIE IN
$

¡¡¡¡ÔÚÕâÖÖÇé¿öÏ£¬½«ÏÔʾµÚÎå¸ö×Ö¶ÎÖÐûÓÐ AR µÄËùÓÐÐÐ -- °üÀ¨Á½¸ö Sarah ÌõÄ¿£¬ÕâÁ½¸öÌõĿȷʵ°üº¬ AR£¬µ«È´ÊÇÔÚµÚÈý¸ö×ֶζø²»ÊǵÚÎå¸ö×Ö¶ÎÖС£

¡¡¡¡»¨À¨ºÅºÍ×ֶηָô·û

¡¡¡¡À¨ºÅ×Ö·ûÔÚ AWK ÃüÁîÖÐÆðןÜÖØÒªµÄ×÷ÓᣳöÏÖÔÚÀ¨ºÅÖ®¼äµÄ²Ù×÷Ö¸³ö½«Òª·¢ÉúʲôÒÔ¼°ºÎʱ·¢Éú¡£µ±Ö»Ê¹ÓÃÒ»¶ÔÀ¨ºÅʱ£º

{print $3,$2}

¡¡¡¡À¨ºÅ¼äµÄËùÓвÙ×÷ͬʱ·¢Éú¡£µ±Ê¹ÓöàÓÚÒ»¶ÔµÄÀ¨ºÅʱ£º

{print $3}{print $2}

¡¡¡¡Ö´ÐеÚÒ»×éÃüÁÔÚ¸ÃÃüÁîÍê³ÉºóÖ´Ðеڶþ×éÃüÁî¡£×¢ÒâÒÔÏÂÁ½ÁÐÇåµ¥µÄÇø±ð£º

$ awk '{print $3,$2}' names
EVAN DULANEY
JEFF DURHAM
BILL STEEN
EVAN FELDMAN
STEVE SWIM
ROBERT BOGUE
MICAH JUNE
SHERYL KANE
WILLIAM WOOD
SARAH FERGUS
SARAH BUCK
BOB TUTTLE
$

$ awk '{print $3}{print $2}' names
EVAN
DULANEY
JEFF
DURHAM
BILL
STEEN
EVAN
FELDMAN
STEVE
SWIM
ROBERT
BOGUE
MICAH
JUNE
SHERYL
KANE
WILLIAM
WOOD
SARAH
FERGUS
SARAH
BUCK
BOB
TUTTLE
$
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 3 ÌõÆÀÂÛ

  1. aini ÓÚ 2005-09-10 23:36:57·¢±í:

    µÚÒ»ÏîÒµÎñ¶¨µ¥ÊÇͨ¹ý½«µÚ¶þ¸ö×ֶΣ¨ÊýÁ¿£©µÄÖµ³ËÒÔµÚÈý¸ö×ֶΣ¨¼Û¸ñ£©µÄÖµ£¬¼ÆËãÿÖÖÎïÆ·µÄ¿â´æ¼ÛÖµ£º

    $ awk '{print $1,"QTY: "$2,"PRICE: "$3,"TOTAL: "$2*$3}' inventory
    hammers QTY: 5 PRICE: 7.99 TOTAL: 39.95
    drills QTY: 2 PRICE: 29.99 TOTAL: 59.98
    punches QTY: 7 PRICE: 3.59 TOTAL: 25.13
    drifts QTY: 2 PRICE: 4.09 TOTAL: 8.18
    bits QTY: 55 PRICE: 1.19 TOTAL: 65.45
    saws QTY: 123 PRICE: 14.99 TOTAL: 1843.77
    nails QTY: 800 PRICE: .19 TOTAL: 152
    screws QTY: 80 PRICE: .29 TOTAL: 23.2
    brads QTY: 100 PRICE: .24 TOTAL: 24
    $

    ¡¡¡¡Èç¹ûÕâЩÐб¾Éí²¢²»ÖØÒª£¬ÄúÖ»ÊÇÏ£ÍûÈ·¶¨É̵êÖÐÓжàÉÙ¼þÎïÆ·£¬Ôò¿ÉÒÔ·ÖÅäÒ»¸öÆÕͨ±äÁ¿£¬°´ÕÕÿÌõ¼Ç¼ÖеÄÎïÆ·ÊýÁ¿Ôö¼Ó£º

    $ awk '{x=x+$2} {print x}' inventory
    5
    7
    14
    16
    71
    194
    994
    1074
    1174
    $

    ¡¡¡¡¸ù¾ÝÕâÒ»Êý¾Ý£¬É̵êÖÐÓÐ 1174 ¼þÎïÆ·¡£µÚÒ»´ÎÖ´ÐÐʱ£¬±äÁ¿ x ûÓÐÖµ£¬Òò´ËËü²ÉÓõÚÒ»Ðеڶþ¸ö×ֶεÄÖµ¡£µÚ¶þ´ÎÖ´ÐÐʱ£¬Ëü±£ÁôÁ˵ÚÒ»ÐеÄÖµ²¢¼ÓÉϵڶþÐеÄÖµ£¬ÒÔ´ËÀàÍÆ£¬Ö±µ½´ïµ½ÀۼƵÄ×ܺϡ£

    ¡¡¡¡¿ÉÒÔÓ¦ÓÃÏàͬµÄ¹ý³ÌÀ´È·¶¨ÏÖÓпâ´æµÄ×ܼÛÖµ£º

    $ awk '{x=x+($2*$3)} {print x}' inventory
    39.95
    99.93
    125.06
    133.24
    198.69
    2042.46
    2194.46
    2217.66
    2241.66
    $

  2. aini ÓÚ 2005-09-10 23:36:31·¢±í:

    ÕÒ³öÔ±¹¤ºÅÂë´¦ÓÚ 46012 ºÍ 46015 Ö®¼äµÄËùÓÐÐУº

    $ awk -F":" '/4601[2-5]/' emp_names
    46012 DULANEY EVAN MOBILE AL
    46013 DURHAM JEFF MOBILE AL
    46015 STEEN BILL MOBILE AL
    $

    ¡¡¡¡Ìí¼ÓÎı¾

    ¡¡¡¡¿ÉÒÔ°´ÕÕÌí¼Ó¿ØÖÆÐòÁлòÆäËû×Ö·ûµÄÏàͬ·½Ê½½«Îı¾Ìí¼Óµ½ÏÔʾÖС£ÀýÈ磬Ҫ½«·Ö¸ô·û´Ó¿Õ¸ñ¸ÄΪðºÅ£¬ÔòÃüÁîÊÇ

    awk '{print $1":"$2":"$3":"$4":"$5}' emp_names > new_emp_names

    ¡¡¡¡ÔÚÕâÖÖÇé¿öÏ£¬×Ö·û (:) °üº¬ÔÚÒýºÅ ("/") ÖУ¬Ëü±»Ìí¼Óµ½Ã¿¸ö×Ö¶ÎÖ®¼ä¡£ÔÚÒýºÅÖ®¼äµÄÖµ¿ÉÒÔÊÇÈκÎÄÚÈÝ¡£ÀýÈ磬´´½¨Ò»¸ö¹ØÓÚ¾ÓסÔÚ°¢À­°ÍÂíÖݵÄÔ±¹¤µÄÍâ¹ÛÀàËÆÊý¾Ý¿âµÄÏÔʾ£º

    $ awk '$5 ~ /AL/ {print "NAME: "$2", "$3"\nCITY-STATE:
    "$4", "$5"\n"}' emp_names

    NAME: DULANEY, EVAN
    CITY-STATE: MOBILE, AL

    NAME: DURHAM, JEFF
    CITY-STATE: MOBILE, AL

    NAME: STEEN, BILL
    CITY-STATE: MOBILE, AL

    NAME: FELDMAN, EVAN
    CITY-STATE: MOBILE, AL

    NAME: SWIM, STEVE
    CITY-STATE: UNKNOWN, AL
    $

    ¡¡¡¡Êýѧ²Ù×÷

    ¡¡¡¡AWK ³ýÁËÌṩÎı¾¹¦ÄÜ£¬»¹Ìṩȫ²¿·¶Î§µÄËãÊõ²Ù×÷·û£¬°üÀ¨ÒÔÏ·ûºÅ£º

    + ½«Êý×ÖÏà¼Ó
    - ¼õ
    * ³Ë
    / ³ý
    ^ Ö´ÐÐÖ¸ÊýÔËËã
    % Ìṩģ
    ++ ½«±äÁ¿Öµ¼ÓÒ»
    += ½«ÆäËû²Ù×÷µÄ½á¹û·ÖÅä¸ø±äÁ¿
    -- ½«±äÁ¿¼õÒ»
    -= ½«¼õ·¨²Ù×÷µÄ½á¹û·ÖÅä¸ø±äÁ¿
    *= ·ÖÅä³Ë·¨²Ù×÷µÄ½á¹û
    /= ·ÖÅä³ý·¨²Ù×÷µÄ½á¹û
    %= ·ÖÅäÇóÄ£²Ù×÷µÄ½á¹û

    ¡¡¡¡ÀýÈ磬¼Ù¶¨ÄúµÄ»úÆ÷ÉÏ´æÔÚÒÔϵÄÎļþ£¬ÏêϸµØÁгöÓ²¼þÉ̵êÖеÄÎïÆ·£º

    $ cat inventory
    hammers 5 7.99
    drills 2 29.99
    punches 7 3.59
    drifts 2 4.09
    bits 55 1.19
    saws 123 14.99
    nails 800 .19
    screws 80 .29
    brads 100 .24
    $

  3. aini ÓÚ 2005-09-10 23:36:12·¢±í:

    ÒªÀûÓöà×éÀ¨ºÅ½øÐÐÖظ´²éÕÒ£¬Ö´ÐеÚÒ»×éÖеÄÃüÁîÖ±µ½Íê³ÉΪֹ£»È»ºó´¦ÀíµÚ¶þ×éÃüÁî¡£Èç¹ûÓеÚÈý×éÃüÁÔòÔÚµÚ¶þ×éÃüÁîÍê³ÉºóÖ´ÐÐËü£¬ÒÔ´ËÀàÍÆ¡£ÔÚËùÉú³ÉµÄ´òÓ¡Êä³öÖУ¬ÓÐÁ½¸ö·Ö¸ôµÄ´òÓ¡ÃüÁÒò´ËÏÈÖ´ÐеÚÒ»¸öÃüÁËæºóÖ´Ðеڶþ¸öÃüÁÕâÑùµ¼ÖÂÿ¸öÌõÄ¿ÏÔʾÔÚÁ½Ðжø²»ÊÇÒ»ÐÐÖС£

    ¡¡¡¡Çø·ÖÁ½¸ö×ֶεÄ×ֶηָô·û²»Ò»¶¨Ê¼ÖÕÊÇ¿Õ¸ñ£»Ëü¿ÉÒÔÊÇÈκοÉʶ±ðµÄ×Ö·û¡£Îª½øÐÐÑÝʾ£¬¼Ù¶¨ emp_names ÎļþÀûÓÃðºÅ¶ø²»ÊÇÖƱí·ûÀ´·Ö¸ô×ֶΣº

    $ cat emp_names
    46012:DULANEY:EVAN:MOBILE:AL
    46013:DURHAM:JEFF:MOBILE:AL
    46015:STEEN:BILL:MOBILE:AL
    46017:FELDMAN:EVAN:MOBILE:AL
    46018:SWIM:STEVE:UNKNOWN:AL
    46019:BOGUE:ROBERT:PHOENIX:AZ
    46021:JUNE:MICAH:PHOENIX:AZ
    46022:KANE:SHERYL:UNKNOWN:AR
    46024:WOOD:WILLIAM:MUNCIE:IN
    46026:FERGUS:SARAH:MUNCIE:IN
    46027:BUCK:SARAH:MUNCIE:IN
    46029:TUTTLE:BOB:MUNCIE:IN
    $

    ¡¡¡¡Èç¹ûÊÔͼͨ¹ýÖ¸¶¨ËùÐèÒªµÄµÚ¶þ¸ö×Ö¶ÎÀ´´òÓ¡ÐÕÊÏ

    $ awk '{print $2}' emp_names

    ¡¡¡¡Äú×îºó»áµÃµ½Ê®¶þ¸ö¿ÕÐС£ÒòΪÎļþÖÐûÓпոñ£¬³ýÁ˵ÚÒ»¸ö×Ö¶ÎÖ®ÍâûÓпÉÈϱðµÄ×ֶΡ£Îª½â¾öÕâÒ»ÎÊÌ⣬±ØÐë֪ͨ AWK ÊÇ¿Õ¸ñÖ®ÍâµÄÁíÒ»¸ö×Ö·û×÷Ϊ·Ö¸ô·û£¬ÓÐÁ½ÖÖ·½·¨¿É֪ͨ AWK ʹÓÃеÄ×ֶηָô·û£ºÊ¹ÓÃÃüÁîÐвÎÊý -F£¬»òÔÚ³ÌÐòÖÐÖ¸¶¨±äÁ¿ FS¡£Á½ÖÖ·½·¨µÄЧ¹ûÏàͬ£¬Ö»ÓÐÒ»ÖÖÀýÍâÇé¿ö£¬ÈçÏÂÀýËùʾ£º

    $ awk '{FS=":"}{print $2}' emp_names

    DURHAM
    STEEN
    FELDMAN
    SWIM
    BOGUE
    JUNE
    KANE
    WOOD
    FERGUS
    BUCK
    TUTTLE
    $

    $ awk -F: '{print $2}' emp_names
    DULANEY
    DURHAM
    STEEN
    FELDMAN
    SWIM
    BOGUE
    JUNE
    KANE
    WOOD
    FERGUS
    BUCK
    TUTTLE
    $

    ¡¡¡¡ÔÚµÚÒ»¸öÃüÁîÖУ¬Í·Ò»Ìõ¼Ç¼·µ»Ø²»ÕýÈ·µÄ¿ÕÐУ¬¶øÆäËû½á¹ûÕýÈ·¡£Ö±µ½¶ÁÈ¡µÚ¶þÌõ¼Ç¼ʱ£¬²Åʶ±ð×ֶηָô·û²¢ÕýÈ·µØÖ´ÐС£Í¨¹ýʹÓà BEGIN Óï¾ä¿ÉÒÔ¾ÀÕýÕâһȱµã£¨ÔÚºóÎÄÏêÊö£©¡£-F µÄ¹¦Äܷdz£ÀàËÆÓÚ BEGIN£¬Äܹ»ÕýÈ·µØ¶ÁÈ¡µÚÒ»Ìõ¼Ç¼²¢°´ÒªÇóÖ´ÐС£

    ¡¡¡¡ÔÚ±¾ÎÄ¿ªÊ¼´¦ÎÒÔøÌáµ½£¬Ä¬ÈϵÄÏÔʾ/Êä³ö×ֶηָô·ûÊÇ¿Õ¸ñ¡£Í¨¹ýʹÓÃÊä³ö×ֶηָô·û (OFS) ±äÁ¿£¬¿ÉÒÔÔÚ³ÌÐòÖиü¸Ä´ËÌØÐÔ¡£ÀýÈ磬Ҫ¶ÁÈ¡Îļþ£¨ÓÉðºÅ·Ö¸ô£©²¢ÒԶ̻®ÏßÏÔʾ£¬ÔòÃüÁîÊÇ

    $ awk -F":" '{OFS="-"}{print $1,$2,$3,$4,$5}' emp_names
    46012-DULANEY-EVAN-MOBILE-AL
    46013-DURHAM-JEFF-MOBILE-AL
    46015-STEEN-BILL-MOBILE-AL
    46017-FELDMAN-EVAN-MOBILE-AL
    46018-SWIM-STEVE-UNKNOWN-AL
    46019-BOGUE-ROBERT-PHOENIX-AZ
    46021-JUNE-MICAH-PHOENIX-AZ
    46022-KANE-SHERYL-UNKNOWN-AR
    46024-WOOD-WILLIAM-MUNCIE-IN
    46026-FERGUS-SARAH-MUNCIE-IN
    46027-BUCK-SARAH-MUNCIE-IN
    46029-TUTTLE-BOB-MUNCIE-IN
    $

    ¡¡¡¡FS ºÍ OFS ÊÇ£¨ÊäÈ룩×ֶηָô·ûºÍÊä³ö×ֶηָô·û£¬ËüÃÇÖ»ÊÇÒ»¶Ô¿ÉÒÔÔÚ AWK ʵÓù¤¾ßÖÐʹÓõıäÁ¿¡£ÀýÈ磬ҪÔÚ´òӡʱΪÿÐбàºÅ£¬¿ÉÒÔ²ÉÓÃÒÔÏ·½Ê½Ê¹Óà NR ±äÁ¿£º

    $ awk -F":" '{print NR,$1,$2,$3}' emp_names
    1 46012 DULANEY EVAN
    2 46013 DURHAM JEFF
    3 46015 STEEN BILL
    4 46017 FELDMAN EVAN
    5 46018 SWIM STEVE
    6 46019 BOGUE ROBERT
    7 46021 JUNE MICAH
    8 46022 KANE SHERYL
    9 46024 WOOD WILLIAM
    10 46026 FERGUS SARAH
    11 46027 BUCK SARAH
    12 46029 TUTTLE BOB
    $