# awk
---
(2022/11/08 整理awk的學習筆記。) ***學習awk的Youtube***
| [各種awk的課程](https://www.youtube.com/results?search_query=awk) | [Learning Awk Is Essential](https://www.youtube.com/watch?v=9YOZmI-zWok&t=202s) | [Awk Isn't Just A Command](https://www.youtube.com/watch?v=2eX0scAsYJk) | [An AWK love story](https://www.youtube.com/watch?v=IfhMUed9RSE&t=283s) |
---
- 列出與計算
```
htw@htw-B15:~/sh$ awk -F "/" ' {print $NF}' /etc/shells | uniq
htw@htw-B15:~/sh$ df -h | awk '/\/dev/ {print }'
htw@htw-B15:~/sh$ df -h | awk '/\/dev/ {print $1"\t"$2"\t"$3}'
htw@htw-B15:~/sh$ df -h | awk '/\/dev/ {print $1"\t"$2 + $3}'
htw@htw-B15:~/sh$ df -h | awk '/\/dev/ {print $1"\t"$2 - $3}'
htw@htw-B15:~/sh$ awk 'length($0) > 7' /etc/shells
htw@htw-B15:~/sh$ awk 'length($0) < 7' /etc/shells
htw@htw-B15:~/sh$ awk 'length($0) < 9' /etc/shells
htw@htw-B15:~/sh$ awk 'length($0) == 7' /etc/shells
```
- 如果某行內容等於某事,把它印出來
```
htw@htw-B15:~/sh$ ps -ef | awk '{ if($NF == "[cpuhp/2]") print $0 }'
root 24 2 0 16:09 ? 00:00:00 [cpuhp/2]
```
- loop迴路for
```
htw@htw-B15:~/sh$ awk 'BEGIN { for(i=1; i<=3; i++) print "The square root of ", i," is ", i*i;}'
The square root of 1 is 1
The square root of 2 is 4
The square root of 3 is 9
```
- 用 \/\/ 作搜尋
```
htw@htw-B15:~$ awk '$1 ~ /^[c,e]/ {print $0}' .bashrc #請列出 是c或e開頭的 行
htw@htw-B15:~$ awk '{print substr($0, 3) }' /etc/shells #從第3個字開始印出來
#找出其中含有u字的那句,並指出在第幾個字位置
htw@htw-B15:~$ awk '{print $0}' /etc/shells
htw@htw-B15:~$ awk 'match($0, /u/) {print $0}' /etc/shells
htw@htw-B15:~$ awk 'match($0, /u/) {print $0 "has \"u\" chacter at " RSTART}' /e:x:tc/shells
/usr/bin/bashhas "u" chacter at 2
/usr/bin/rbashhas "u" chacter at 2
#只要印出第3到第4行
htw@htw-B15:~$ df | awk '{print NR, $0}'
htw@htw-B15:~$ df | awk 'NR==4, NR==5 {print NR, $0}'
```
- END是移到最後行,BEGIN在開始第一行之前
```
htw@htw-B15:~$ awk 'END {print NR} ' /etc/passwd #移到最後一行的位置,說出現在是第幾行
54
htw@htw-B15:~$ awk 'END {print NR} ' /etc/shells #移到最後一行的位置,說出現在是第幾行
9
htw@htw-B15:~$ awk 'END {print NR} ' /etc/shells /etc/passwd #會算出共計行數
63
htw@htw-B15:~$ awk 'BEGIN { print "Hello world!"}'
htw@htw-B15:~$ awk 'BEGIN { print "Hello world!"}' xy.txt
htw@htw-B15:~$ awk 'BEGIN { print "Hello world!"} {print "Hi"}' xy.txt
htw@htw-B15:~$ awk 'BEGIN { print "Hello world!"} {print "Hi"} END { print "HELLO WORLD!"}' xy.txt
```
- 寫成一個awk程式更方便
```
#把從BEGIN到END的三句 寫入 xtest.awk, 用下列指令執行結果是一樣的
htw@htw-B15:~$ awk -f xtest.awk xy.txt
#用whereis awk發現是在/usr/bin/awk 所以在行首加上這句#!/usr/bin/awk -f 並改為可執行後
htw@htw-B15:~$ ./xtest.awk xy.txt #就可做出一樣的結果
htw@htw-B15:~$ awk -v name="htw" 'BEGIN { print name}' #在一句中 先直接設定變數
htw
htw@htw-B15:~$ awk -v name="htw" 'BEGIN { print "name"}' #注意" "的用法
name
htw@htw-B15:~$ awk -v name="htw" 'BEGIN { print ""name""}'
htw
htw@htw-B15:~$ awk '/dog/ {print}' xy.txt
htw@htw-B15:~$ awk '/dog/ {print} /114/ {print}' xy.txt
htw@htw-B15:~$ awk '{print}' xy.txt # =cat xy.txt
```
- 關於sed的二三事
```
例如我要把檔案〝MyFile.txt〞的 1~8 行中的〝The〞或〝the〞改為大寫的〝THE〞可如下:
$ sed -e '1,8 s/ [Tt]he/ THE/ g' MyFile.txt
基本用法為〝s/樣板(PATTERN)/取代(REPLACEMENT)/〞
$ echo 'this is a apple' | sed 's/a/AN/' ←將〝a〞改為〝AN〞(只取代搜尋到的第一個樣板就停止)
$ echo 'this is a apple' | sed 's/a/AN/g' ←將〝a〞改為〝AN〞(從頭到尾搜尋取代)
$ echo 'this is a apple' | sed 's/ is//g' ←將單字〝is〞刪掉(刪掉用空字串來取代)記得〝is〞前要加一空格寫成〝 is〞,不然〝this〞串中的〝is〞也會被刪除)
```