ãªãã¹ãæ¸ããªãawkã®ä½¿ãæ¹
awkã¨ãããå¤ãããã®ã¹ã¯ãªããè¨èªããããï¼1977å¹´çã¾ããèªã¿æ¹ã¯ããªã¼ã¯ãã§ãããã¨ã¤ã»ãããªã¥ã»ã±ã¤ã§ã¯ãªãï¼man awkãPDFã«å¤æãã¦ã¿ãã¨ããã£ãã®3ãã¼ã¸å¼·ãããªãã
$ man -t awk|pstopdf -i -o ~/Downloads/awk.pdf
ã¨ã¦ãã·ã³ãã«ãªè¨èªä»æ§ã§ã¯ããããawkã«ã¯å¿ è¦ååãªè¡¨ç¾åããããç¹ã«ããã¹ããå¦çããå ´é¢ã«ããã¦ã¯ãæå°éã®ã·ã³ãã«ãªè¨è¿°ã§ãæ°ã®å©ããå¦çãç´ æ©ãããªããç¡é§ã®ãªãawkã¯ã³ã©ã¤ãã¼ãè¦ãã¨ããã種ã®æåãè¦ãããawk以éã«çã¾ããã¹ã¯ãªããè¨èªã¯ãå°ãªãããawkã®å½±é¿ãåãã¦ããã¨æãããã
awkãç¥ããã¨ã§ãééããªã幸ç¦åº¦ã¯ä¸ããã¨æãããã¤ããã£ã¨ãç¥ã£ã¦ã¦è¯ãã£ããã¨æããæãæ¥ãã¯ãããã£ã¨ãã£ã¨ãawkãç¥ããããªã£ã¦ããã
åºæ¬åä½
awkã®åºæ¬åä½ã¯ãã¨ã£ã¦ãã·ã³ãã«ã§ããã
- ããã¹ãã1è¡èªã¿è¾¼ãã§ã
- èªã¿è¾¼ãã è¡ã空ç½åºåãã®ãã¼ã¿ã¨è§£éãã¦ã
- ä½ããã®å¦çãè¡ãã
- 以ä¸ã®åä½ããè¡æ«ã¾ã§ç¹°ãè¿ãã
- 1ã2ã®åä½ã¯ãawkãèªåçã«å¦çããã
- 3ã®é¨åããèªåãæ¸ããã¨ã«ãªãã
- ä¾ãã°ä»¥ä¸ã®ãããªããã¹ããã¡ã¤ã«ãä½ã£ã¦...
$ cat <abc.txt > a b c > d e f > g h i > EOS $ cat abc.txt a b c d e f g h i
- awkã¹ã¯ãªããã«ãã¤ãã§æ¸¡ãã¦ã¿ãã¨...
$ cat abc.txt | awk '{print $0}'
a b c
d e f
g h i
ã¤ã¾ã...
- a b cãèªã¿è¾¼ãã§ãprint $0ãã¦ã
- d e fãèªã¿è¾¼ãã§ãprint $0ãã¦ã
- g h iãèªã¿è¾¼ãã§ãprint $0ãã¦ããã®ã ã
- awkã¹ã¯ãªããå ¨ä½ã¯ã·ã³ã°ã«ã¯ã©ã¼ãã§å²ã£ã¦ãä½ããã®å¦çã¯ããã«{ã}ã§å²ã£ã¦ããå¿ è¦ãããã
- ã¹ãã¼ã¹åºåããå®æããããã«ãçãä¸ã®åï¼2åç®ï¼ã ãåºåãã¦ã¿ãã
$ cat abc.txt | awk '{print $2}'
b
e
h
ã¤ã¾ã...
- $0ã$2ã¯ãawkãç¨æããå¤æ°ã§ããã
- $1ã«ã¯ã1åç®ã®ãã¼ã¿ãå ¥ã£ã¦ããã
- $2ã«ã¯ã2åç®ã®ãã¼ã¿ãå ¥ã£ã¦ããã
- $nã«ã¯ãnåç®ã®ãã¼ã¿ãå ¥ã£ã¦ããã
- ããã¦ã$0ã«ã¯ãã¹ã¦ã®åãã¤ã¾ã1è¡å ¨ä½ã®ãã¼ã¿ãå ¥ã£ã¦ããã®ã ã
- ã¨ããã§ãprint $1 $2 $3ã¨ãprint $1,$2,$3ã¯ãéãã
$ cat abc.txt | awk '{print $1 $2 $3}' abc def ghi $ cat abc.txt | awk '{print $1,$2,$3}' a b c d e f g h i
æ¡ä»¶ã¨å¦ç
- æ¡ä»¶ãä»å ãããã¨ã«ãã£ã¦ããã®æ¡ä»¶ãæºããããæã ããä½ããã®å¦çãè¡ãããã«ãªãã
- æ¡ä»¶ã¯ãä½ããã®å¦çã®ç´åã«æ¸ãã
- "e"ãå«ãè¡ã®æã ããprintã
$ cat abc.txt | awk '/e/{print $0}'
d e f
- 1åç®ã"a"ã®æã ããprintã
$ cat abc.txt | awk '$1=="a"{print $0}'
a b c
- 3è¡ç®ã®æã ããprintã
$ cat abc.txt | awk 'NR==3{print $0}'
g h i
- NRã¯ãawkãç¨æããå¤æ°ã§ããã
- ç¾å¨å¦çãã¦ããè¡çªå·ãä»£å ¥ããã¦ããã
- awkã§ã¯ããã®æ¡ä»¶ã¨ä½ããã®å¦çã®çµã¿åããã«ãã£ã¦ããããããã¨ãå¦çãã¦ããã
- æ¡ä»¶ ï¼ ãã¿ã¼ã³ ã¨å¼ã°ãã¦ããã
- ä½ããã®å¦ç ï¼ ã¢ã¯ã·ã§ã³ ã¨å¼ã°ãã¦ããã
- è¨ãæããã°ãããã¿ã¼ã³ã¨ã¢ã¯ã·ã§ã³ã®çµã¿åããã«ãã£ã¦ããããããã¨ãå¦çãã¦ããã
- ããã¦ã'ãã¿ã¼ã³{ã¢ã¯ã·ã§ã³}'ã®å®ç¾©ã¯ãè¤æ°æ¸ããã¨ãã§ããã
- ã¤ã¾ããawkã¹ã¯ãªããã¨ã¯ 'ãã¿ã¼ã³{ã¢ã¯ã·ã§ã³}' ã®éåãªã®ã ã
- ä¾ãã°ãaã§å§ã¾ãè¡ã¨ãgã§å§ã¾ãè¡ãåãåºããªã...
$ cat abc.txt | awk '
> /^a/{print $0}
> /^g/{print $0}
> '
a b c
g h i
- è¤æ°è¡ã«åãã¦ãã1è¡ã«ã¾ã¨ãã¦ããã©ã¡ãã§ãOKã
$ cat abc.txt | awk '/^a/{print $0}/^g/{print $0}'
a b c
g h i
grepï¼cutã¨ã®éã
- ããã¾ã§ã®å¦çã¯ãå¥ã«awkã使ããªãã¦ããgrepã¨cutãçµã¿åãããã°å®ç¾ã§ããã
# 2åç®ãåãåºã $ cat abc.txt | cut -d' ' -f2 b e h # aãgã§å§ã¾ãè¡ãåãåºã $ cat abc.txt | grep -E '^a|^g' a b c g h i # aãgã§å§ã¾ãè¡ã®2åç®ãåãåºã $ cat abc.txt | grep -E '^a|^g' | cut -d' ' -f2 b h
- ã¨ãããããã®æ§è½ã¯å¾®å¦ã«éã£ã¦ããã
- ãã®éããã使ãåæã«å¤§ããå½±é¿ããã
- ä¾ãã°lsã®åºåãå å·¥ãããã¨æã£ãå ´å...
$ ls -l
total 16
drwx------+ 31 bebe staff 1054 12 4 16:21 Desktop
drwx------+ 450 bebe staff 15300 12 5 10:37 Documents
drwx------+ 332 bebe staff 11288 12 5 14:24 Downloads
drwx------@ 72 bebe staff 2448 10 30 17:44 Library
drwx------+ 9 bebe staff 306 10 30 06:42 Movies
drwx------+ 9 bebe staff 306 2 28 2013 Music
drwx------+ 14 bebe staff 476 11 5 15:51 Pictures
drwxr-xr-x+ 12 bebe staff 408 12 2 16:14 Public
drwxr-xr-x+ 5 bebe staff 170 12 2 16:14 Sites
...ä¸ç¥...
- cutã§5åç®ã®ãã¤ãæ°ã®åã ãåãåºãã®ã¯ãã¡ãã£ã¨è¦å´ããã
- åºåãè¨å·ãã¹ãã¼ã¹ã«å¤æ´ãã¦ã5åç®ãæå®ããã ãã§ã¯ã以ä¸ã®ãããªæå³ããªãçµæã«ãªã£ã¦ãã¾ã...ã
$ ls -l | cut -d' ' -f5
staff
staff
bebe
bebe
bebe
...ä¸ç¥...
- ãã®åå ã¯ãcutãæå®ãããåºåãè¨å·ã§ããã¹ãã¼ã¹ã使ã£ã¦ã1æåãã¤å³æ ¼ã«åºåããã¨ããçµæã§ããã
- ã¹ãã¼ã¹ãé£ç¶ããé¨åã§ããããããã®ã¹ãã¼ã¹ãåºåãè¨å·ã¨è§£éãã¦ãç¡é§ãªåºåããå¢ããã¦ããã®ã ã
- ãã£ã¦ãããcutã§ãã¤ãæ°ã®åã ãåãåºããªããé£ç¶ããã¹ãã¼ã¹ãï¼ã¤ã«ã¾ã¨ããå¦çãå¿
è¦ã«ãªãã
- sed 's/ \{1,\}/ /g' ã«ãã£ã¦ãé£ç¶ããã¹ãã¼ã¹ãï¼ã¤ã«ã¾ã¨ãã¦ããã
$ ls -l | sed 's/ \{1,\}/ /g' | cut -d' ' -f5
1054
15300
11288
2448
306
306
476
408
170
...ä¸ç¥...
- ä¸æ¹ãawkãªãä½ã®è¦å´ããªãããã¤ãæ°ã®åãåãåºããã
$ ls -l | awk '{print $5}'
1054
15300
11288
2448
306
306
476
408
170
...ä¸ç¥...
- awkã§ã¯ããã©ã«ãã®åºåãè¨å·ãã1æå以ä¸ã®é£ç¶ãããã¹ãã¼ã¹ãã¿ãã«ãªã£ã¦ãã模æ§ã
- ã¡ãªã¿ã«ãcutã®ããã©ã«ãã®åºåãè¨å·ã¯ã1æåã®ã¿ãã§ããã
- ããã¦æ®å¿µãªãã¨ã«ãcutã®åºåãæåã«ã¯ã1æå以ä¸ã¨ããæ£è¦è¡¨ç¾ãè¨å®ã§ããªãã®ã ...ã
awkã«ã¯ã人ã®æè¦ã«åã£ãåºåãæåãããã©ã«ãã§è¨å®ããã¦ãããã ãã使ããããï¼
- ã³ãã³ãåºåãããããã¹ããã¼ã¿ãå å·¥ãããªããæç¶awkã使ããããªãã
éè¨ã®æ
ããã¦ãawkã®å¦çã¯åãåãåºãã ãã«çã¾ããªãã
- åãåºããåã¯ãç´ æ©ãéè¨ã§ããã
- ENDã¯ããã¹ã¦ã®è¡ãèªã¿è¾¼ã¿å®äºå¾ã1åã ãå®è¡ãããç¹æ®ãªãã¿ã¼ã³ï¼ï¼æ¡ä»¶ï¼ã§ããã
- ã¡ãªã¿ã«ãæåã®è¡ãèªã¿è¾¼ãåã«ã1åã ãå®è¡ãããBEGINã¨ãããã¿ã¼ã³ï¼ï¼æ¡ä»¶ï¼ãããã
$ ls -l | awk '{sum+=$5; print $5} END{print "--------\n" sum}'
1054
15300
11288
2448
306
306
476
408
170
136
136
18
343
136
136
340
-
-
-
-
-
-
- -
-
-
-
-
-
- è¡åã®éè¨ã ã£ã¦ç°¡åã«ã§ããã
- -vãªãã·ã§ã³ã¯ãã¹ã¯ãªããå®è¡åã«å¤æ°ã«å¤ãè¨å®ããã
- OFSã¯awkãç¨æããçµè¾¼ã¿å¤æ°ã§ãawkãåºåããæã«ãåã¨åãç¹ãåºåãæåãä¿åããã
$ cat <123.txt > 1 2 3 > 4 5 6 > 7 8 9 > EOS $ cat 123.txt 1 2 3 4 5 6 7 8 9 $ cat 123.txt | awk -v OFS="\t" '{rt=$1+$2+$3; c1+=$1; c2+=$2; c3+=$3; c4+=rt; print $1,$2,$3,"",rt} END{print ""; print c1,c2,c3,"",c4}' 1 2 3 6 4 5 6 15 7 8 9 24 12 15 18 45
- Fizz Buzzåé¡ãã¯ã³ã©ã¤ãã¼ã§ã
- seqã³ãã³ãã¯ãæå®ããæ°å¤ã¾ã§ã®æ°åãåºåããã
$ seq 100 | awk '{n=$1} n%3==0{$1="";$3="Fizz"} n%5==0{$1="";$5="Buzz"} {print $1 $3 $5}'
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
...ä¸ç¥...
ããã¯ããã表è¨ç®ã¢ããªã¨åãæè¦ã§ããã
- ããã¹ãå ¨ä½ãåºåãæåã«ãã£ã¦åºåãããè¡åãã¼ã¿ã¨è§£éãããèªå¨ã«éè¨ã§ããã
- åãªãè¡åãã¼ã¿ã§ã¯ãªããæå®ããæ¡ä»¶ã«ããããããã¼ã¿ã®ã¿æ½åºãã¦ãéè¨ã§ããã
- ã¯ããæã®1977å¹´ã®é ãããã¾ã 表è¨ç®ã¢ããªãªã©åå¨ããªãæ代ãããawkã¯ãã¼ã¿ãã¼ã¹ã®æ¤ç´¢ã¨éè¨ãã·ã³ãã«ãªä»çµã¿ã§å®ç¾ãã¦ããã®ã ã
awkã¨ã¯ã表è¨ç®ã³ãã³ããªã®ãããããªãã
çç¥ã®æ
awkã®å¿å°ããã¯ãçç¥å¯è½ãªè¡¨ç¾åã ã¨æãã
- ä¾ãã°ãããã¾ã§print $0ã¨æ¸ãã¦ããããå¤ãã®å ´åã$0ã¯çç¥ã§ããã
- ãã£ã¦ãprint $0ã¯ãprintã®ã¿ã§OKã
$ #cat abc.txt | awk '{print $0}' $ cat abc.txt | awk '{print}' a b c d e f g h i
- ã¾ãããã¿ã¼ã³ã®ã¿ã§ã¢ã¯ã·ã§ã³ãåå¨ããªãå ´åããã¿ã¼ã³ããããããæãawkã¯{print}ãå¦çãã¦ãããã
- ãã£ã¦ãæ¤ç´¢ã¯ãã¿ã¼ã³ã®æå®ã®ã¿ã§OKã
$ #cat abc.txt | awk '/e/{print $0}' $ #cat abc.txt | awk '/e/{print}' $ cat abc.txt | awk '/e/' d e f
- ããã«ããã¿ã¼ã³ããããããã¨ã¯ãæ¡ä»¶å¼ã®æ»ãå¤ã0ã§ãªãç¶æ
ã§ããã
- æ¡ä»¶å¼ãæç«ããã¨ã1ãè¿ãã
- æ¡ä»¶å¼ãæç«ããªãã¨ã0ãè¿ãã
- ãã®ä»çµã¿ãç¥ãã¨ããã¿ã¼ã³ã¨ã¯ç¹ã«æ¡ä»¶å¼ã§ããå¿
è¦ã¯ãªããã¨ã«æ°ä»ãã
- ã¤ã¾ããä½ããã®å¼ã0ãè¿ãã°ãç¶ãã¢ã¯ã·ã§ã³ã¯å®è¡ãããã
- ä½ããã®å¼ã0以å¤ãè¿ãã°ãç¶ãã¢ã¯ã·ã§ã³ãå®è¡ãããã®ã ã
- å¼ã¨ã¯ãæ°å¤ã®ã¿ã§ãå¼ã§ããã
- ãã£ã¦ããã¹ã¦ã®è¡ãåºåãããã®ã§ããã°ã1ã¨æ¸ãã ãã§ãOKã
- 2ã§ã3ã§ãã0以å¤ãªãä½ã§ãOKãªã®ã ãã©ãä¸è¬çã«ãã1ã使ãããããã ã
- 1ã«ç¶ãã¢ã¯ã·ã§ã³ããªãã®ã§{print}ãå®è¡ãããã®ã ã
- ãã£ã¦ããã¹ã¦ã®è¡ãåºåãããã®ã§ããã°ã1ã¨æ¸ãã ãã§ãOKã
$ #cat abc.txt | awk '{print $0}' $ #cat abc.txt | awk '{print}' $ cat abc.txt | awk '1' a b c d e f g h i
- awk '1'ã ãã®ã¹ã¯ãªããã§ã¯ããã®æ©æµãã»ã¨ãã©æããªããã
- awkã§ã¯ååã®ãã¼ã¿ãå å·¥ãã¦ãæå¾ã«ãã¹ã¦ãprintããããªããã¨ãããããã
- ãã®å ´åã'{print}'ã®çç¥è¨æ³ã¨ãã¦ã'1'ã使ããããã¨ãå¤ãã
- ä¾ãã°ãFizz Buzzåé¡ã®æå¾ã®{print $1 $3 $5}ã¯ã1ã«ç½®æãã¦ãã許ããç¯å²ã®æ¸å¼ã§åºåãããã¨æãã
$ #seq 100 | awk '{n=$1} n%3==0{$1="";$3="Fizz"} n%5==0{$1="";$5="Buzz"} {print $1 $3 $5}' $ seq 100 | awk '{n=$1} n%3==0{$1="";$3="Fizz"} n%5==0{$1="";$5="Buzz"}1' 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fizz Buzz ...ä¸ç¥...
-
- OFS=""ãè¨å®ãã¦ããã°ãæ¸å¼ãä¹±ããªãã¯ãã
$ seq 100 | awk -v OFS="" '{n=$1} n%3==0{$1="";$3="Fizz"} n%5==0{$1="";$5="Buzz"}1'
- æååãç½®æããgsubã¨ããé¢æ°ãããã¹ã¦ã®è¡ã§ç½®ãæããçºçãããªããprintã¯ä¸è¦ã«ãªãã
- å°æåã®aã大æåã®Aã«ããã«ã¯ãç¹å®ã®è¡ãã対象ã«ãªããªãã®ã§ãprintãå¿ è¦ã§ããã
- gsubé¢æ°ã®æå¾ã®å¼æ°ã¯ãç½®ãæã対象ã®æååãæå®ããã®ã ããçç¥ããã¨è¡å ¨ä½ï¼$0ï¼ã«å¯¾ãã¦ç½®ãæãå¦çãè¡ãããã
$ #cat abc.txt | awk '{gsub(/a/,"A",$0);print}' $ cat abc.txt | awk '{gsub(/a/,"A");print}' A b c d e f g h i
-
- ããè¡é ã«#ãä»å ããå ´åãªãããã¹ã¦ã®è¡ã対象ã«ãªãã®ã§ãã¢ã¯ã·ã§ã³ã§ã¯ãªããã¿ã¼ã³ã«æ¸ããã¨ã§{print}ãçç¥ã§ããã
- ç½®ãæããçºçããå ´åãgsubé¢æ°ã®æ»ãå¤ã«ã¯ç½®ãæãããåæ°ãè¿ãã®ã§ãï¼å¿ ãç½®ãæããçºçããã°ã常ã«1以ä¸ãè¿ãï¼
$ #cat abc.txt | awk '{gsub(/^/,"#");print}' $ cat abc.txt | awk 'gsub(/^/,"#")' #a b c #d e f #g h i
çç¥ãã¦ããawkãè¯ãã«è¨ãã解éãã¦ãããæãå¬ããã
çç¥ã®è½ã¨ãç©´
ä½ããçç¥ã®ã«ã¼ã«ã«ãã£ã¦ãawkã«äºæ³å¤ã®è§£éãããããã¨ãããã
$ echo apple orange melon | awk '{if($0 ~ /apple/ || $0 ~ /orange/)print "Hit!"}'
Hit!
- ä¸è¨ã¨ä¸è¨ï¼$0 ~ ãçç¥ï¼ã¯ãæå³çã«ã¯ã¾ã£ããåãã§ããã
$ echo apple orange melon | awk '{if(/apple/ || /orange/)print "Hit!"}'
Hit!
- ãã®çç¥ã®ã«ã¼ã«ã«ãã£ã¦ãäºæ³å¤ã®çµæãå°ããã¦ãã¾ã...ã
$ echo apple orange melon | awk '{if(/apple/ ~ $1)print "Hit!";else print "Unmatched"}'
Unmatched
- $1ã¯"apple"ãªã¯ããªã®ã«ããªãUnmatchedã«ãªã£ã¦ãã¾ãã®ãï¼
- awkã¯ãæ¡ä»¶ã®å é ã«/æ£è¦è¡¨ç¾/ãè¦ã¤ããã¨ã($0 ~ /æ£è¦è¡¨ç¾/) ã¨è§£éãã¦ãã¾ãã®ã ã
- ãã£ã¦ãä¸è¨ã¹ã¯ãªããã¯ä»¥ä¸ã®ããã«è§£éããã¦ããã¨æãããã
$ echo apple orange melon | awk '{if(($0 ~ /apple/) ~ $1)print "Hit!";else print "Unmatched"}'
-
- ($0 ~ /apple/)ã®è¨ç®çµæã¯1ã¨ãªãã
- ãã®å¾(1 ~ $1)ãè©ä¾¡ããã¦ããã®ã ã
- ãªãã°ãappleã®åã«1ã追è¨ããã°ãHit!ããã¯ããæã£ãã¨ããï¼
$ echo 1 apple orange melon | awk '{if(/apple/ ~ $1)print "Hit!";else print "Unmatched"}'
Hit!
æ°ã®å©ããawkã®ãç¯ä»ãèªçºããå¯ä¸ã®ãã¤ãã¹é¢ã¨æããããæ°ãã¤ãããï¼
- æ¯è¼å¯¾è±¡ãæ示ããæã¯ãæ£è¦è¡¨ç¾ã¯å¾ã«æ¸ãå¿ è¦ãããã®ã ã
- ãã£ããå ã«æ¸ãã¦ãã¾ãã¨ãç¸å½æ©ã¿ç¶ãããã¨ã«ãªãããã
æåã¨æ°å¤ã¨å¤æ°
awkã¯å¿ è¦ã«å¿ãã¦ãæåãæ°å¤ãè¯ãã«è¨ããèªåå¤æãã¦ãããã
- æåã®è¶³ãç®ããã¡ããã¨è¨ç®ãã¦ãããã
$ echo | awk '{print 1 + 2}' 3 $ echo | awk '{print "1" + "2"}' 3
- æ°å¤ã«å¤æã§ããªãæåã¯0ã¨è§£éãããã
$ echo | awk '{print "a" + "b"}'
0
- æ°å¤å士ã§ããæååã¨ãã¦çµåã§ããã
$ echo | awk '{print 1 2}'
12
- ãã¿ã¼ã³ã®æ»ãå¤ã¯ã0ã¾ãã¯""ãfalseã
$ echo | awk '0{print "Trueã§ãã"}' $ echo | awk '""{print "Trueã§ãã"}'
- ä¸è¨ä»¥å¤ã¯ããã¹ã¦trueã"0"ãtrueã
$ echo | awk '"0"{print "Trueã§ãã"}'
Trueã§ãã
- å¤æ°ã«ã¯æåããã""ãä»£å ¥ããã¦ãããï¼ã¨èãããã¨ã«ãã¦ããï¼
- ãã£ã¦ãå¤æ°ã®åæåãªãã§ããããªãæ°å¤è¨ç®ã§ããã®ã ï¼
- 1ãã10ã¾ã§è¶³ãç®ãã¦ã¿ãã
$ seq 10 | awk '{sum+=$1; print sum}'
1
3
6
10
15
21
28
36
45
55
ããã°ã©ãã¼ãé¢åã ã¨æããã¨ã¯ããã¨ãã¨ãawkå´ã§ãã¾ãåãè¨ãã£ã¦ãããã®ã ï¼
- æåã¨æ°å¤ãåºå¥ãããã¨ãªãããããæ°åã§ã³ã¼ããæ¸ãã¦å¬ããã
- nilã»NULLã®å¤å®ãä¸è¦ã«ãªã£ã¦å¬ããã
è¨èªä»æ§
ããã¾ã§awkã®æ¦è¦ãåããã¨ã詳ããè¨èªä»æ§ãç¥ããããªãããã使ããããªé¨åãæç²ãã¦ã¿ãã
çµã¿è¾¼ã¿å¤æ°
ãã£ã7ã¤ã®å¤æ°ï¼$æ°å¤ãNFãNRãFSãRSãOFSãORSï¼ãè¦ãã¦ããã ãã§ãããªã便å©ã«ä½¿ããã
- ä»ã¾ã§è¡åãã¼ã¿ã¨æ¸ãã¦ããããawkã§ã¯ãè¡ãRecordï¼ã¬ã³ã¼ãï¼ã¨å¼ãã§ããã
- è¡ã®ä¸ã§åºåããã1åããFieldï¼ãã£ã¼ã«ãï¼ã¨å¼ãã§ããã
- Recordã¨Fieldã®æå³ãåããã¨ãawkã®çµã¿è¾¼ã¿å¤æ°ãè¦ãããããªãã®ã ã
$1 â |
FS â |
$2 â |
FS â |
$3 â |
FS â |
ï¼NF=ãã£ã¼ã«ãæ°ï¼ $NF â |
RS â |
|
---|---|---|---|---|---|---|---|---|
ãã£ã¼ã«ã1 | ãã£ã¼ã«ã2 | ãã£ã¼ã«ã3 | ãã£ã¼ã«ãæ«å°¾ | \n | âã¬ã³ã¼ã1ï¼å¦çä¸ãªãNR=1ï¼ | |||
ãã£ã¼ã«ã1 | ãã£ã¼ã«ã2 | ãã£ã¼ã«ã3 | ãã£ã¼ã«ãæ«å°¾ | \n | âã¬ã³ã¼ã2ï¼å¦çä¸ãªãNR=2ï¼ | |||
ãã£ã¼ã«ã1 | ãã£ã¼ã«ã2 | ãã£ã¼ã«ã3 | ãã£ã¼ã«ãæ«å°¾ | \n | âã¬ã³ã¼ã3ï¼å¦çä¸ãªãNR=3ï¼ |
- NFï¼å¦çãã¦ããã¬ã³ã¼ãã®ãã£ã¼ã«ãæ°ï¼Number of Fieldsï¼
- NRï¼å¦çãã¦ããã¬ã³ã¼ãã®å é ããã®çªå·ï¼Number of Recordï¼
- FSï¼å ¥åæã«ãã£ã¼ã«ããåºåãæåï¼Field Separatorï¼
- RSï¼å ¥åæã«ã¬ã³ã¼ããåºåãæåï¼Record Separatorï¼
- OFSï¼åºåæã«ãã£ã¼ã«ããç¹ããæåï¼Output Field Separatorï¼
- ORSï¼åºåæã«ã¬ã³ã¼ããç¹ããæåï¼Output Record Separatorï¼
-
- OFSãè¨å®ãããã¨ã§ãã¹ãã¼ã¹åºåããããã«ã³ãåºåãã«å¤æãã¦ã¿ãã
- $1=$1ã¯ãOFSã®å¤æ´ã$0ã«åæ ããããããä½ããã®ãã£ã¼ã«ãæä½ãå¿ è¦ãªã®ã ã
$ echo A B C | awk '{OFS=",";$1=$1;print}'
A,B,C
-
- OFSãå¤æ´ãã¦ãããã£ã¼ã«ãæä½ãä½ããªãã¨$0ãæ´æ°ãããã以åã®ã¾ã¾ã«ãªãã
$ echo A B C | awk '{OFS=",";print}'
A B C
$ã§å§ã¾ããã£ã¼ã«ãå¤æ°ã¯ãé
åã®ããã«æ¯ãèãï¼
- $æ°å¤ã¯ããã£ã¼ã«ãã®å
容ã代å
¥ãããå¤æ°ã§ããã
- $0ã«ã¯ãè¡å ¨ä½ã®å 容ãä»£å ¥ãããã
- $1ã«ã¯ã1çªç®ã®ãã£ã¼ã«ãã®å 容ãä»£å ¥ãããã
- $2ã«ã¯ã2çªç®ã®ãã£ã¼ã«ãã®å 容ãä»£å ¥ãããã
- $NFã«ã¯ãæå¾ã®ãã£ã¼ã«ãã®å 容ãä»£å ¥ãããã
- $NF以éã¯ãæªå®ç¾©ãªã®ã§""ãä»£å ¥ãããã
- $NFã®ä¾ãããåããããã«...
- $å¤æ°ã¯ãå¤æ°ã®å¤ã示ããã£ã¼ã«ãã®å 容ãä»£å ¥ãããã
$ echo A B C D E F G | awk '{num=5; print "$num =", "$" num, "=", $num}'
$num = $5 = E
-
- $(å¼)ã¯ãè¨ç®çµæã示ããã£ã¼ã«ãã®å 容ãä»£å ¥ãããã
$ echo A B C D E F G | awk '{print $(NF-1)}'
F
-
- $(å¼)ã¯ãåºæ¬çã«æ¬å¼§ã§å²ãå¿ è¦ãããã
- æ¬å¼§ãªãã§ã¯ã$NF=$7="G"ã¨è§£éããããã®å¾"G" - 1ãè¨ç®ãããã
$ echo A B C D E F G | awk '{print $NF-1}'
-1
-
- "G"ã¯æ°å¤ã«å¤æã§ããªãã®ã§0ã¨è©ä¾¡ããã0 - 1 = -1 ã¨ãªãã®ã ã
å¶å¾¡æ
if (æ¡ä»¶) çã®å¦ç else å½ã®å¦çããããããã | æ¡ä»¶ã«ãã£ã¦ãçå½ã©ã¡ããã®å¦çãå®è¡ãããï¼else以éã¯çç¥å¯è½ï¼ |
---|
$ echo A B C D E F G | awk '{if(length > 8)print $1,$2 "..." $NF; else print}' A B...G $ echo A B C D | awk '{if(length > 8)print $1,$2 "..." $NF; else print}' A B C D
- ifãelseã®å¾ãè¤æ°å¦çãå®è¡ããæã¯{ã}ã§å²ãã
$ echo A B C D E F G | awk '{if(length > 8){s=$1 FS $2 "..." $NF; print s;} else print}'
A B...G
while (ç¶ç¶æ¡ä»¶) ã«ã¼ãå¦çããããããããã | ç¶ç¶æ¡ä»¶ãçã®ç¶æ ãªããã«ã¼ãå¦çãç¶ããã |
---|---|
do ã«ã¼ãå¦ç while (ç¶ç¶æ¡ä»¶)ããããããã | ç¶ç¶æ¡ä»¶ãçã®ç¶æ ãªããã«ã¼ãå¦çãç¶ãããï¼ç¶ç¶æ¡ä»¶ãå½ã§ããæä½1åã¯ã«ã¼ãå¦çãå®è¡ããï¼ |
for (åæè¨å®; ç¶ç¶æ¡ä»¶; å¢æ¸è¨å®) ã«ã¼ãå¦çã | ç¶ç¶æ¡ä»¶ãçã®ç¶æ ãªããã«ã¼ãå¦çãç¶ãããï¼for ï¼ åæè¨å®ã¨å¢æ¸è¨å®ãä»å±ããwhileï¼ |
for (å¤æ° in é å) ã«ã¼ãå¦çãããããããã | é åã®ãã¼ãé ã«å¤æ°ã«ä»£å ¥ããªãããã«ã¼ãå¦çãç¹°ãè¿ãã |
breakããããããããããããããããããã | forãwhileãdoã®ã«ã¼ãå¦çãæãåºãã |
continueããããããããããããããããã | forãwhileãdoã®æ¬¡ã®ã«ã¼ãå¦çã¸ç§»è¡ããã |
$ echo A B C D E F G | awk '{for(i=2;i
B C D E F
- forãwhileãdoã®å¾ãè¤æ°å¦çãå®è¡ããæã¯{ã}ã§å²ãã
$ echo A B C D E F G | awk '{for(i=2;i
B
BC
BCD
BCDE
BCDEF
getlineããããããããããããããããããã | awkã¯1è¡ãã¤èªåçã«èªã¿è¾¼ãã§ãããããgetlineã¯1è¡èªã¿è¾¼ã¿ãèªåã§å¶å¾¡ããæã«ä½¿ãã |
---|---|
nextãããããããããããããããããããã | awkã®å¦çã次ã®è¡ã«é²ããã |
nextfileããããããããããããããããããã | awkã®å¦çã次ã®ãã¡ã¤ã«ã«é²ããã |
exit [ å¼ ]ãããããããããããããããããã | awkã®å¦çãä¸æãã¦ãå¼ã®çµæãã¹ãã¼ã¿ã¹ã³ã¼ãã¨ãã¦è¿ããï¼ENDãã¿ã¼ã³ã¯å®è¡ãããï¼ |
printãããããããããããããããããããã | æå®ãããå 容ããåºåãããï¼æ¹è¡ããï¼ |
---|---|
printfãããããããããããããããããããã | æå®ãããå 容ããæå®ããããã©ã¼ãããã§ãåºåãããï¼Cè¨èªã®printfãã©ã¼ãããã«æºããï¼ |
- 3æ¡åºåã
- \047ï¼ã·ã³ã°ã«ã¯ã©ã¼ãã®8é²æ°è¡¨è¨
- ã·ã³ã°ã«ã¯ã©ã¼ãå ã«"%'d\n"ã¨æ¸ããããã¨ã¹ã±ã¼ãããå¿ è¦ããã£ãã
$ echo 1234567| awk '{printf "%\047d\n", $0}'
1,234,567
- é常print
$ echo A B C D| awk '{printf "%s\n", $0}'
A B C D
- 10æ¡å¹ ã®å³å¯ã
$ echo A B C D| awk '{printf "|%10s|\n", $0}'
A B C D |
- 10æ¡å¹ ã®å·¦å¯ã
$ echo A B C D| awk '{printf "|%-10s|\n", $0}'
A B C D |
delete array[index]ããããããããããããã | é åã®æå®ãããindexãåé¤ãããï¼[index]æå®ãªãã ã¨ãé åå ¨ä½ãåé¤ããï¼ |
---|
- awkã®é åã¯ããã¹ã¦é£æ³é åã§ããã
$ echo | awk '{a[1]=100; print a["1"]}' 100 $ echo | awk '{a["dog"]="one!"; a["cat"]="nya-"; for(i in a)print a[i]}' nya- one!
- æªå®ç¾©ã®é åå 容ã¯ã""ãè¿ãããã
$ echo | awk '{a["dog"]="one!"; a["cat"]="nya-"; delete a["dog"]; print a["cat"], a["dog"]}' nya- $ echo | awk '{a["dog"]="one!"; a["cat"]="nya-"; delete a; print a["cat"], a["dog"]}'
æ¼ç®å
- åªå é ä½ãé«ãé ã®æ¼ç®åãªã¹ã
åªå é ä½ | æ¼ç®å | æå³ |
---|---|---|
é«ã | (ã) | ã°ã«ã¼ãã³ã° |
$ | ãã£ã¼ã«ãã®åç § | |
++ -- | ã¤ã³ã¯ãªã¡ã³ãããã¯ãªã¡ã³ã | |
^ ** | ã¹ãä¹ï¼ã©ã¡ããåãã¹ãä¹ï¼ | |
+ - ! | ãã©ã¹ããã¤ãã¹ãè«çå¦å® | |
∗ / % | ä¹ç®ãé¤ç®ãå°ä½ | |
+ - | å ç®ã¨æ¸ç® | |
åè§ã¹ãã¼ã¹ | æååé£æ¥ | |
< <= > >= != == | é¢ä¿æ¼ç®å | |
~ !~ | æ£è¦è¡¨ç¾ã®ããããéããã | |
in | é åã¸ã®åå¥ã¢ã¯ã»ã¹ | |
&& | è«ççãªAND | |
|| | è«ççãªOR | |
?ã:ã | 3é æ¼ç®åï¼æ¡ä»¶ ? çã®å¦ç : å½ã®å¦çï¼ | |
ä½ã | = += -= *= /= %= ^= | ä»£å ¥ï¼ä¾ï¼n+=2ã¯ãn=n+2ã¨åçï¼ |
é¢æ°
- awkãç¨æããé¢æ°ä¸è¦§
æ°å¤é¢æ° | atan2 cos exp int log rand sin sqrt srand |
---|---|
æåé¢æ° | gsub index length match split sprintf sub substr tolower toupper |
ãã®ä»ã®é¢æ° | close fflush system |
- ä¸è¨ã®ä¸ã§ããã使ããããªé¢æ°
é¢æ° | æ©è½ | å¼æ°ãçç¥ããã¨ãã®æå³ |
---|---|---|
sub(æ£è¦è¡¨ç¾, å¤æèªå¥, å¦ç対象ã®å¤æ°) | æ£è¦è¡¨ç¾ã«ãããããé¨åããå¤æèªå¥ã«ã1åã ãç½®ãæããã | sub(/mac/, "Mac") == sub(/mac/, "Mac", $0) |
gsub(æ£è¦è¡¨ç¾, å¤æèªå¥, å¦ç対象ã®å¤æ°) | æ£è¦è¡¨ç¾ã«ãããããé¨åããå¤æèªå¥ã«ããã¹ã¦ç½®ãæããã | gsub(/mac/, "macintosh") == gsub(/mac/, "macintosh", $0) |
index(æåå, æ¤ç´¢èª) | æååã«å«ã¾ããæ¤ç´¢èªã®ä½ç½®ãè¿ãã | |
length(æåå) | æååã®é·ããè¿ãã | length == length($0) |
split(æåå, é åå¤æ°, åºåãæå) | æååãåºåãæåã§å解ãã¦ãé åå¤æ°ã«ä»£å ¥ããã | split("A B C", a) == split("A B C", a, FS) |
sprintf("ãã©ã¼ããã", å¤, å¤...) | printfã®çµæãæååã¨ãã¦è¿ãã | |
tolower(æåå) | å°æåã«å¤æããã | tolower == tolower($0) |
toupper(æåå) | 大æåã«å¤æããã | toupper == toupper($0) |
- 大æåã»å°æåãåºå¥ããªãã§æ¤ç´¢ãããæãawkã¯tolowerã§å
¨ä½ãå°æåã«å¤æãã¦ããæ¤ç´¢ãããããªã...ã
- ãããã¯ãtoupperã§å ¨ä½ã大æåã«å¤æãã¦ããæ¤ç´¢ããã®ã ã
$ echo Apple | awk '/apple/' $ echo Apple | awk 'tolower ~ /apple/' Apple
- ä¸æ¹ãgrepãªã-iãªãã·ã§ã³ã§OKã
$ echo Apple | grep 'apple' $ echo Apple | grep -i 'apple' Apple
åèãã¼ã¸
ããã«è©³ããawkã®ä»æ§ã«ã¤ãã¦ã¯ã以ä¸ã®ãã¼ã¸ãããã¸ãåèã«ãªãã¾ãããï¼ç´ æ´ãããæ å ±ã«ãæè¬ã§ãï¼ï¼- awk sed å ¥é
- ãã¼ã¿ãã¡ã¤ã«å¦çã«ä¾¿å©ãªUNIXã³ãã³ã
- AWK ãªãã¡ã¬ã³ã¹ - UNIX & Linux ã³ãã³ãã»ã·ã§ã«ã¹ã¯ãªãã ãªãã¡ã¬ã³ã¹
- AWK Users JP :: awk åºç¤ææ³æéãã¹ã¿ã¼
- pSeries ããã³ AIX ã¤ã³ãã©ã¡ã¼ã·ã§ã³ã»ã»ã³ã¿ã¼
- awkã§æ¹è¡ã空ç½ã«å¤æ - ã±ã¼ãºã¡ã¢ï¼ã³ã¡ã³ãæ¬ãã¨ã¦ãåèã«ãªãï¼
- AWKã®ã¾ã¨ã - 大éªæè²å¤§å¦
ãawkã¯æ¸ãããããã£ãä¸è¡ãã£ã¦ä½ï¼
第ï¼ï¼è©±ãä¸æ¹ä¸ä¸¡æãï¼è½èªã®å°åºã®è½ã¡ï¼ããããããï¼å¤§å²¡ï¼é£ãããããã£ãä¸è³ï¼è¶åï¼ãã 第37話「三方一両損」
- ã¤ã¾ããå¤ãã¯é£ãããããã£ãä¸è³ãã
- 転ãã¦ãawkã¯æ¸ãããããã£ãä¸è¡ãã
- ã¤ã¾ãããªã¼ã¯ï¼å¤ãï¼ã¯æ¸ãããããã£ãä¸è¡ãã