Deleted articles cannot be recovered. Draft of this article would be also deleted. Are you sure you want to delete this article?
CodeZineç·¨éé¨ã§ã¯ãç¾å ´ã§æ´»èºãããããããã¼ãã¹ã¿ã¼ã«ããããã®ã«ã³ãã¡ã¬ã³ã¹ãDevelopers Summitãããã¨ã³ã¸ãã¢ã®çããã¾ããã¼ã¹ãããããã®ã¤ãã³ããDevelopers Boostããªã©ããã¾ãã¾ãªã«ã³ãã¡ã¬ã³ã¹ãä¼ç»ã»éå¶ãã¦ãã¾ãã
Section: Utility Commands (1) Updated: May 17 2000 Index JM Home Page roff page å称 gawk - ãã¿ã¼ã³æ¤ç´¢ã»å¦çè¨èª æ¸å¼ awk [ POSIX or GNU style options ] -f program-file [ -- ] file ... gawk [ POSIX or GNU style options ] [ -- ] program-text file ... 解説 gawk 㯠GNU ããã¸ã§ã¯ããå®è£ ãã ããã°ã©ãã³ã°è¨èª AWK ã®å¦çç³»ã§ãã æ¬å¦ç系㯠POSIX 1003.2 ã³ãã³ãè¨èªã¨ã¦ã¼ãã£ãªãã£è¦ç´ã«å®ãããã è¨èªã®å®ç¾©ã«é©åãã¦ãã¾ãã æ¬ãã¼ã¸ã§ã³ã¯ã¾ããAho, Kernighan, Weinberger ã®èæ¸ The AWK Programmi
02 æ 06 æ¥ï¼æï¼ ãã£ã«ã¿è¨èª AWK (3) AWK ã®é åããã¾ã使ãã°ï¼è¤éãªå¦çãç°¡åã«è¨è¿°ã§ããå ´åãããï¼ AWK ã®ç¬¬ä¸æ© ã gawk æ¥æ¬èªããã¥ã¢ã« çãåèã«ããªããï¼ ä½æ¥ãé²ãã¦è¡ããï¼ AWK ã®é å AWK ã®é£æ³é å æ¬æ¥ã®èª²é¡ AWK ã®é å AWK ã§ã¯ï¼å¤æ°ã¨åæ§ã«ï¼é åã«ã¤ãã¦ã宣è¨ããã«å©ç¨ã§ããï¼ ã¤ã¾ãï¼AWK ã®é åã¯ï¼ åçé åï¼è¦ç´ æ°ãå¯å¤ã®é åï¼ã¨ãããã¨ã ï¼ ï¼£ã®é åã¯ï¼åºæ¬çã«ã¯åºå®é åã ã£ãï¼ åçé åã使ããã¨ãã§ãããï¼ æºåã® malloc( ) 㨠çä»ãã® free() ã¨ãå¿ è¦ã§ï¼ é¢åã ã£ãï¼
awkã§éè¤è¡ãé«éåé¤ããã è¡ã®éè¤åé¤ã§èª¿ã¹ãã¨ããåºã¦ããã®ããsort & uniqã使ã£ãã³ãã³ãã ã½ã¼ãã¯ãã¡ã¤ã«ã大ãããªãã¨ãå®è¡ã«ããªãæéãããã£ã¦ãã¾ãã¾ããã ã½ã¼ãããã«é«éå®è¡ã§ããã®ãã次ã®awkã³ãã³ãã awk '!colname[$1]++{print $1}' ãããããã¡ã¤ã«ããã£ã¦ãä¸åç®ã®ååãåå¾ãããã¨ãã!colname[$1]ã¯ããã¼ã¿ããã¼ã«ããé£æ³é åã§ãã¤ã³ã¯ãªã¡ã³ããããã ãã ããã | colname["ãã"] â 0 {print $1} ãã¤ã±ã« ã¾ããã | colname["ãã¤ã±ã«"] â 0 {print $1} ãã¤ã±ã« ã¾ãã | colname["ãã¤ã±ã«"] â 1 ãµããã¼ PC ã | colname["ãµããã¼"] â 0 {print $1} ãµããã¼ ãã¼ãã¼ã | colname["ãµã
é£æ³é åã®ãã¼ã«ã°ã«ã¼ãã®ååã使ãã°ãkã éè¨ãããã¡ã¤ã«ââ hoge.txt ããã 230 ããã 300 ã°ãªãª 190 ã°ãªãª 190 ã¿ãã 110 ããã 310 ã°ãªãª 190 ã¿ãã 110 ã¿ãã 130 awkãã¡ã¤ã«ââ sum.awk { sum[$1]+=$2 }; END { for (key in sum) { print key, sum[key] } } ã¹ã¯ãªããã®å®è¡ã¨çµæã awk -f sum.awk hoge.txt ã°ãªãª 570 ã¿ãã 350 ããã 840
ç°¡åãªè§£æã¯ã¯ã³ã©ã¤ãã¼ã§ãã£ã¡ããããã¨ãããã¨ã§ã¾ãã¯ç°¡åã«æå°å¤ã¨æ大å¤ã¨å¹³åãæ±ããAwkã¯ã³ã©ã¤ãã¼ãæ¸ãã¾ãã ãã¨ãã°ä»¥ä¸ã®ãããªãã°ãã¡ã¤ã«ãããã¨ãããåã¯ãã¾ã3åç®ã®æ°å¤ã«å¯¾ãã¦çµ±è¨å¤ãåºãããã®ã ã 0 0 147.086 0 10 141.269 0 20 10000 0 30 2805.22 0 40 152.003 0 50 144.762 0 60 144.926 æå¤§å¤ cat test.log | awk '{if(m<$3) m=$3} END{print m}' { if(m<$3) m=$3 } END { print m } $3ã£ã¦ã®ã第3çªç®ã®åã¨ããæå³ãªã®ã§ããããå¤ããã°å¥ã®ã¨ãããåç §å¯è½ãBEGINã®ä¸ã§åæå¤ãè¨å®ããå¿ è¦ãããã®ããªã¨æã£ã¦ãããã©ããããªãif(m>$3)ãå®éã«ãã£ã¦ã¿ãã大ä¸å¤«ã ã£ãã æå°å¤ åºæ¬çã«ã¯å
jq is a lightweight and flexible command-line JSON processor. jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text. jq is written in portable C, and it has zero runtime dependencies. You can download a single binary, scp it to a far away machine of the same type, and expect
æè¿ã¡ããã¡ããPythonã触ãæ©ä¼ãå¤ããªã£ã¦ããã®ã ããå¦çã®ä¸ã§grepãawkã®ããã«è¡ã®æ½åºããããããã¨ãããã Subprocessã§grepã¨ãawkãå¼ã³åºãã®ã¯ãã£ãæªãããPythonå ã§å¦çãå®çµããããã¨ããã®ããã£ãã®ã§å°ã調ã¹ã¦ã¿ãã 1.æå®ããæååãå«ãè¡ãæ½åº 1-1.åºæ¬çãªæ½åºæ¹æ³ Pythonã§grepã®ãããªå¦çãè¡ãã«ã¯ãfind('æåå')ãç¨ãã¦ãã®æååãå«ãæ°ãæå®ãããã¨ã§æ½åºãå¯è½ã ã 以ä¸ãè¨è¿°ä¾ã # -*- coding: utf-8 -* import sys ld = open(sys.argv[1]) lines = ld.readlines() ld.close() for line in lines: if line.find("æåå") >= 0: print line[:-1] [root@BS-PUB-
ã¿ã¼ããã«ä¸ã§ç°¡åã«ã°ã©ããæãããã«termeterã¨ãããã¼ã«ãä½æãã¦ã¿ã¾ããã Getting Started termeterã¯é常ã®ã³ãã³ãã©ã¤ã³ãã¼ã«ã¨ãã¦ä½¿ç¨ãããã¨ãã§ãã¾ããã¿ãåºåããã«ã³ãåºåãã®ãã¼ã¿ãæ¨æºå ¥åããã¡ã¤ã«ã§ãããããã¨ã§ã°ã©ããæãäºãã§ãã¾ãã ä¾ã¨ãã¦ä»¥ä¸ã®ãããªä¸è§é¢æ°ã®ã¿ãåºåãã®ãã¼ã¿ãèãã¾ãã $ seq 100 | awk 'BEGIN{OFS="\t"; print "x","sin(x)","cos(x)"}{x=$1/10; print x,sin(x),cos(x)}' | head x sin(x) cos(x) 0.1 0.0998334 0.995004 0.2 0.198669 0.980067 0.3 0.29552 0.955336 0.4 0.389418 0.921061 0.5 0.479426 0.877
ã³ãã³ã $ ls | sed -e s/\.txt// | awk '{print $1 ".txt " $1 "_document.md"}' | xargs -n 2 mv æ¦ç¥ã«ã¤ã㦠ã¾ãåæã¨ãã¦ããã¡ã¤ã«ã®ãªãã¼ã ãè¡ãã«ã¯ã xargs -n 2 mv ã«ãã¤ã㧠å ãã¡ã¤ã«å ãªãã¼ã å¾ã®ãã¡ã¤ã«å ã®ãããªãã©ã¼ãããã®æååã渡ãã° OK ã§ãã $ ls before.txt $ echo "before.txt after.txt" before.txt after.txt $ echo "before.txt after.txt" | xargs -n 2 mv $ ls after.txt ãã®æååãè¤æ°è¡ããå ´åã xargs -n 2 mv ã¯1è¡ãã¤é 次å¦çãã¾ãã $ ls 1.txt 2.txt $ echo "1.txt 11.txt\n2.tx
Effective AWK Programming A User's Guide for GNU Awk Edition 1.0.4 April, 1999 Arnold D. Robbins ã¯ããã« awkã¨gawkã®æ´å² The GNU Project and This Book Acknowledgements Introduction Using This Book Dark Corners Typographical Conventions ä¾ã§ä½¿ç¨ãããã¼ã¿ãã¡ã¤ã« Getting Started with awk A Rose By Any Other Name awkããã°ã©ã ã®å®è¡ã®ä»æ¹ 使ãæ¨ã¦ã®ä¸çºawkããã°ã©ã å ¥åãã¡ã¤ã«ãªãã®awkã®å®è¡ é·ãããã°ã©ã ãå®è¡ãã å®è¡å¯è½ãªawkããã°ã©ã awkããã°ã©ã ä¸ã®ã³ã¡ã³ã é常ã«åç´ãªä¾ äºã¤ã®ã«ã¼ã«ãæã¤ä¾
ããã£ã¦ãªãã åã¯è¨æ¶åããããããªãã®ã§ãawkã®ã³ãã³ããããã«ã¡ã¢ãã¦ããã¾ãã å¤æ°ã®åãæ±ã $0 = è¡å ¨ä½ $1 = 1åç® ã¨ããé ã«ç¶ãã #æ½åº ååã ãæå® cat data.txt | awk '{print $1, $2, $4}' > hogehoge.txt ##æ¡ä»¶ä»ã 第nåãm以ä¸ã®ã¨ããæ½åº n = 3, m = 10ã¨ããã cat data.txt | awk '$3 > 10 {print}' > hogehoge.txt #æ大å¤ã»æå°å¤ 第3åã«ãããä¾ã¨ãã¦ã æå¤§å¤ cat data.txt | awk '{if(m<$3) m=$3} END{print m}' æå°å¤ cat data.txt | awk 'BEGIN{m=100000}{if(m>$3) m=$3} END{print m}' éè¤ éè¤è¡ãåé¤ cat data
2è¡ç®ã¨3è¡ç®ã表示ãã (ãã¤ãçµç±) % cat hoge.txt | awk 'NR==2,NR==3 { print }' 2è¡ç®ã¨3è¡ç®ã表示ãã (ãã¡ã¤ã«ããèªã¿è¾¼ã) % awk 'NR==2,NR==3 { print }' < hoge.txt æ¡ä»¶åå² (if else) % grep 'needle' hoge.txt \ | awk '{if($2==OK) ok++; else ng++}; END {print ok; print ng}' 第ï¼ãã£ã¼ã«ãã®ãOKããã«ã¦ã³ãããåæã«ãOK以å¤ããã«ã¦ã³ãããã æ¼ç®å && || % some_command | awk '{if($1==A && $2==B) print}' % some_command | awk '{if($1==A || $2==B) print}' 第ï¼ãã£ã¼ã«ããã1以ä¸ãã®è¡
2 åè¨ 9496 9 -rwxr-xr-x 1 root root 986672 9æ 27 2014 bash 9 -rwxr-xr-x 3 root root 30240 10æ 21 2013 bunzip2 9 -rwxr-xr-x 1 root root 1713424 11æ 15 2013 busybox 9 -rwxr-xr-x 3 root root 30240 10æ 21 2013 bzcat 11 lrwxrwxrwx 1 root root 6 10æ 21 2013 bzcmp -> bzdiff 9 -rwxr-xr-x 1 root root 2140 10æ 21 2013 bzdiff ..
<ITæè¡ã®å¦æ¹ç®ï¼awkã使ã£ã¦ããã£ã¬ã¯ããªã®ä¸è¦§ãåå¾ãã> awkã使ãã¨ãå¼ã渡ããããã¼ã¿ãé åã¨ãã¦èªã¿åãã¾ãã 以ä¸ã®ä¾ã ã¨ãls -la ã®3çªç®ã®è¦ç´ ãæ½åºãã¾ãã ls -l |awk '{print $3}' ãã®æ§è³ªã使ã£ã¦ããã£ã¬ã¯ããªä¸è¦§ãåå¾ãã¾ãããã å é ã'd'ã®ãã®ã¯ããã£ã¬ã¯ããªã§ããã $ls -l -rw-r--r-- 16:16 bbb -rw-r--r-- 16:16 cccc drwxr-xr-x 12:58 dir/ drwxr-xr-x 09:26 from/ awkãå¿ç¨ãã¦èããã¨ã»ã»ã»ã»ã» ãã£ã¬ã¯ããªã®ä¸è¦§ã®æååã ãåå¾ã§ãã¾ãã $ls -l | awk '$1 ~ /d/ {print $9 }' dir from â»è§£èª¬ $1ï¼æåã®æåï¼ãdã§å§ã¾ãã®ã¯ãdirã¨fromã ãã§ããã ä¸è¨ã®ãããªçµæã«ãªãã¾ãã
ãªãªã¼ã¹ãé害æ å ±ãªã©ã®ãµã¼ãã¹ã®ãç¥ãã
ææ°ã®äººæ°ã¨ã³ããªã¼ã®é ä¿¡
å¦çãå®è¡ä¸ã§ã
j次ã®ããã¯ãã¼ã¯
kåã®ããã¯ãã¼ã¯
lãã¨ã§èªã
eã³ã¡ã³ãä¸è¦§ãéã
oãã¼ã¸ãéã
{{#tags}}- {{label}}
{{/tags}}