編注1ï¼æ¨æºå ¥åããã¡ã¤ã«ããè¡åä½ã§ããã¹ããã¼ã¿ãèªã¿è¾¼ã¿ãç½®æãªã©ã®å¦çãè¡ã£ã¦åºåããUnixã³ãã³ãã
æååã®æãåºã
ããã¼ã¿å¦çã®åºæ¬ã¯ããã¼ã¿ãå å·¥ã»éè¨ãã¦ã¾ã¨ããããã¨ãã¤ã¾ããå ã®ãã¼ã¿ããå¿ è¦ãªé¨åãæãåºãã¦å å·¥ããããã¨ã§ããååã§ã¯ããã£ã¼ã«ãåä½ã§æãåºãæ¹æ³ã説æãã¾ãããä»åã¯ãã£ã¼ã«ãåä½ã§ã¯ãªãããâæåç®ããâæåãæãåºããã¨ãã£ãå¦çãã説æãã¾ãã
ããã®ãâæåç®ããâæåãæãåºããå¦çãè¡ãã«ã¯ãsubstré¢æ°ã使ãã¾ããã§ã¯ã2æåç®ããå¾ã®æååãåå¾ãã¦ã¿ã¾ãããã
$ echo 'abcde' | awk '{print substr($0, 2)}' bcde
ããã®ããã«substré¢æ°ã¯ãæåã®å¼æ°ã«å¯¾è±¡æååã2çªç®ã®å¼æ°ã«ã2æåç®ããã§ããã°ã2ãä¸ãããã¨ã§å¿ è¦ãªæååãæãåºããã¨ãã§ãã¾ãã
ããã¡ããããã®ç縮形ã¯ä»¥ä¸ã®ããã«ãªããã·ã§ã«è¸ç·¨æ³¨2ã§ä½¿ãã¨ãã¼ã®ææ°ãå°ãªãå¹æçã§ãã
$ echo 'abcde' | awk '$0 = substr($0, 2)' bcde
編注2ï¼ãã·ã§ã«è¸ãã¨ã¯ãUNIXã·ã§ã«ï¼ä¸»ã«bashï¼ã®ã¯ã³ã©ã¤ãã¼ãé§ä½¿ãã¦æååå å·¥ãèªç±èªå¨ã«æããã¨ã§ããã¾ãããã®ãããªè½åããã¤äººãã·ã§ã«è¸äººã¨å¼ã³ã¾ãã
ãsubstré¢æ°ã«ã¯3çªç®ã®å¼æ°ãä¸ãããã¨ãã§ãã¾ãããã®3çªç®ã®å¼æ°ãæå®ããã¨ãæå®ããæåæ°åã ãã®æååãåãåºããã¨ãã§ãã¾ããã§ã¯ã2æåç®ãã3æåãåå¾ãã¦ã¿ã¾ãããã
$ echo 'abcde' | awk '{print substr($0, 2, 3)}' bcd
ãä»ã®è¨èªã«ãsubstré¢æ°ãããã«ä¼¼ãé¢æ°ãããã¾ãããã ãå¤ãã®è¨èªã¯0ï¼ã¼ãï¼ããæ°ãå§ãããã¼ããªãªã¸ã³ãã§ããããããAWKã¯1ããæ°ãå§ããããã人éã®æèã«è¿ãã¤ã¡ã¼ã¸ã§å¼æ°ãä¸ãããã¨ãã§ãã¾ããããã¯ã¬ã³ã¼ãããã£ã¼ã«ãã«ã¤ãã¦ãåãã§ãããAWKã«é¢ããå ¨ã¦ã®ã¤ã³ããã¯ã¹ã¯1ããéå§ããã¾ãããã£ã¨ããéã«åããã«ããã¨ããæ¹ãå¤ãããã§ãã
ããã使ãããæ¹æ³ã¨ãã¦ãã対象æååã®ä¸ã«ããç¹å®ã®æååããä»»æã®æååãåãåºããã¾ãã¯ãããæååã¾ã§ã®ä»»æã®æååãåãåºããã¨ãããã®ãããã¾ãããã®ãããªå ´åã«ã¯ãindexé¢æ°ãä½µãã¦ç¨ããã¨å¹æçã§ãã
$ echo 'abcde' | awk '{print substr($0, index($0, "b"))}' bcde
ããã®ä¾ã§ã¯æåå"b"ããæå¾ã¾ã§ãæãåºãã®ã«ãsubstré¢æ°ã¨indexé¢æ°ã®ä¸¡æ¹ãç¨ãã¦ãã¾ãã
ãindexé¢æ°ã¯æåã®å¼æ°ã«å¯¾è±¡ã¨ãªãæååã2çªç®ã®å¼æ°ã«æ¤ç´¢ãããæååãæå®ããã¨ãæ¤ç´¢ãããæååã®å é ä½ç½®ãè¿ãã¾ããæ¤ç´¢æååãåå¨ããªãå ´åã«ã¯0ï¼ã¼ãï¼ãè¿ãã¾ãã
ããã®substré¢æ°ã¨indexé¢æ°ã®çµã¿åããã¯ãæååã®æãåºãã®ä¸ã§ãç¹ã«å¤ãç¨ãããããã¯ããã¯ãªã®ã§ãè¦ãã¦ããã¨ä¾¿å©ã§ãã
æååã®æ¤ç´¢
ãæååãæ£è¦è¡¨ç¾ã®æ¤ç´¢ã¯ãã§ã«èª¬æãã¦ãã¾ãã
echo 'abcde' | awk '$0 ~ /b.*/' abcde
ããã®ä¸è¡éé編注3ãè¦ãã¨ãä¸ããããå ¥åè¡ãæ£è¦è¡¨ç¾ã«ããããã¦ããã¨ããã®ã¯åããã¾ãããå ·ä½çã«ã©ã®é¨åãããããã¦ããã®ãã¯ããã®ã¾ã¾ã§ã¯åããã¾ãããããã§AWKã«ã¯matchã¨ããå°ãç¹æ®ãªé¢æ°ãç¨æããã¦ãã¾ãã
$ echo 'abcde' | awk 'match($0, /b.*/)' abcde
編注3ï¼ã¯ã³ã©ã¤ãã¼ã®ãã¨ã1è¡ã§å¿ è¦ãªå¦çãæ¸ãåãã
ãã¤ã¾ããmatché¢æ°ã¯æåã®å¼æ°ã«å¯¾è±¡æååãæå®ãã¦ã2çªç®ã®å¼æ°ã«ã¯æ¤ç´¢ããæ£è¦è¡¨ç¾ãæå®ãã¾ãã
ãããã ããè¦ãã¨matché¢æ°ã¯ãããæ¼ç®å"~"ã¨åããããªæ°ããã¾ããã§ã¯ããªãmatché¢æ°ãç¹æ®ãªã®ã§ãããããããã¯matché¢æ°ãå¤ãè¿ãã ãã§ãªããçµè¾¼å¤æ°RSTARTã¨RLENGTHãã»ããããããã§ãããã®çµè¾¼å¤æ°RSTARTã«ã¯ãããããæåã®ä½ç½®ãçµè¾¼å¤æ°RLENGTHã«ã¯ãããããé·ããæ ¼ç´ããã¾ãã
echo 'abcde' | awk 'match($0, /b.*/) {print RSTART, RLENGTH}' 2 4
ãã¤ã¾ããæ£è¦è¡¨ç¾"b.*"ã¨ããã®ã¯ã$0ã®2æåç®ãã4æåã該å½ããé¨åã§ããã¨ãããã¨ã§ãã
ãããã«ãmatché¢æ°ãã»ããããçµè¾¼å¤æ°RSTARTã¨RLENGTHã使ã£ã¦ã以ä¸ã®ãããªè¨è¿°ããããã¨ãããããã¾ãã
$ echo 'abcde' | awk 'match($0, /b.*/) {print substr($0, RSTART, RLENGTH)}' bcde
ãã¤ã¾ããæ£è¦è¡¨ç¾ã«è©²å½ããç®æã ããæãåºãã®ã«matché¢æ°ã¨substré¢æ°ãç¨ãã¦ãã¾ãã
ãAWKã§æ±ãå¤ãã®å ´åã«ã¯ãããæ¼ç®å"~"ã ãã§ååãªãã¨ãå¤ããmatché¢æ°ãç¨ãããã¨ã¯å°ãªãæ°ããã¾ãããªããªããmatché¢æ°ã®æ大ã®ç¹å¾´ã§ããçµè¾¼å¤æ°RSTARTã¨RLENGTHã使ããã¨ããã¨ãå¿ ç¶çã«ä¸è¡ééã§è¨è¿°ããã«ã¯é·éããããã§ãã
ãã·ã§ã«è¸åå¼·ä¼ã§çã¾ããæ¬æ¥ã®ç®çã§ã¯ãªãæ大ã®ææã®ä¸ã¤ã«"grep -o"ã¨ãããã®ãããã¾ããgrepã³ãã³ãã®å¼æ°ã¨ãã¦"-o"ãä»ããã¨æ£è¦è¡¨ç¾ã«ãããããé¨åã ããæãåºããã¤ã¾ãå ã»ã©ã®AWKã¹ã¯ãªããã¨åããã¨ãgrepã³ãã³ãã§ã§ãã¦ãã¾ãã®ã§ãã
$ echo 'abcde' | grep -o 'b.*' bcde
ãããããããã ãã§ãªããã·ã§ã«è¸ã®ä¸ã§ããã使ããããæååã1æåãã¤åé¢ããããããã«æ¹è¡ãæ¿å ¥ããã横â縦å¤æããç¬æã«ããªãã¦ãããã®ã§ä¾¿å©ã§ãï¼éã«ã縦â横å¤æã¯å¼æ°ãªãã®xargsã³ãã³ãã§è¡ãã¾ãï¼ã
$ echo 'abcde' | grep -o '.' a b c d e
ãå°ãè±ç·ãã¾ãããããã®matché¢æ°ã¯ææ°ã®GNU AWKï¼gawkï¼ã§ã¯æ¡å¼µããã¦ç¬¬3ã®å¼æ°ãåããããã«ãªã£ã¦ãããããã«ä¾¿å©ã«ãªã£ã¦ããã®ã§ãããããããGNUæ¡å¼µã®ã話ã¯å¥ã®æ©ä¼ã«è²ãããã¨æãã¾ãã
æ¬é£è¼ãåè¡æ¬ã«ãªãã¾ããï¼
ãã·ã§ã«è¸ãã«å¹ãï¼AWKå¦æ¹ç®
èè
ï¼æè¤åæ
çºå£²æ¥ï¼2017å¹´1æ31æ¥ï¼ç«ï¼
ä¾¡æ ¼ï¼PODï¼ï¼2,160åï¼ç¨è¾¼ï¼
ä¾¡æ ¼ï¼é»åæ¸ç±ï¼ï¼1,728åï¼ç¨è¾¼ï¼
æ¬æ¸ã«ã¤ãã¦
ãã³ãã³ãã§ãã軽éè¨èªï¼LLï¼ã®å
ç¥ã§ããã£ã¦ãã·ã§ã«ã§ã®ããã¹ããã¼ã¿å¦çã«ã¯ä¾¿å©ã§ææ¾ããªããAWKãã®é
åã¨æ¸ãæ¹ãã·ã§ã«ã³ãã³ãã¨çµã¿åããããã¯ããã¯ï¼ã·ã§ã«è¸ï¼ã解説ã
ãããªã³ããªã³ããã³ãï¼PODï¼ã¨é»åæ¸ç±ã«ã¦ã絶è³çºå£²ä¸ã§ãï¼