9.6 awk命令(上)
awk是一種編程語言,用于在linux/unix下對文本和數據進行處理。數據可以來自標準輸入(stdin)、一個或多個文件,或其它命令的輸出。它支持用戶自定義函數和動態正則表達式等先進功能,是linux/unix下的一個強大編程工具。它在命令行中使用,但更多是作為腳本來使用。awk有很多內建的功能,比如數組、函數等,這是它和C語言的相同之處,靈活性是awk最大的優勢。
awk應用
awk打印指定內容
awk ‘{print $n}’ [filename] 在此n代表數字(當n=0時$0代表文件所有內容,當n=1,2,3...時$1,2,3...代表相應列)
awk -F 指定分隔符(默認以空格為分隔符)
[root@hch awk]# awk -F ':' '{print $1}' test.txt
root
bin
daemon
daemon
Ctrl-Alt-Deoooleoooote ios handled by /usr/lib/systemd/system/c
……
可以一次打印多列:
[root@hch awk]# awk -F ':' '{print $1,$2,$3}' test.txt |head -3
root x 0
bin x 1
daemon x 2
可以指定打印各列內容時的分隔符號:
[root@hch awk]#awk -F ':' '{print $1"#"$2"#"$3}' test.txt |head -3
root#x#0
bin#x#1
daemon#x#2
說明:?指定分隔符后該命令只識別指定的分隔符,如果某行無指定的分隔符則會打印其整行!
awk的匹配功能(匹配用“~”)
打印含有“oo”的所有行:
[root@adai003 awk]# awk '/oo/' test.txt
root:x:0:0:roprot:/root:/bin/bash
Ctrl-Alt-Deoooleoooote ios handled by /usr/lib/systemd/system/c
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
打印第一段含有“oo”的行:
[root@adai003 awk]# awk -F ':' '$1 ~ /oo/' test.txt
root:x:0:0:root:/root:/bin/bash
注:‘~’表示匹配!
支持正則表達式:
[root@hch awk]# awk -F ':' '$1 ~ /o+/' test.txt
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
operator:x:11:0:operator:/root:/sbin/nologin
[root@hch awk]# awk -F ':' '$1 ~ /oo+/' test.txt
root:x:0:0:root:/root:/bin/bash
支持多個表達式同時執行:
[root@hch awk]# awk -F ':' '/root/ {print $1,$3} /user/ {print $1,$3,$4}' test.txt
root 0
operator 11
tss 59 59
針對數學表達式的用法
數值比較
當'$3>=1000 {print $1}'時:
[root@hch awk]# awk -F ':' '$3>=1000 {print $1}' test.txt
mysql
[root@hch awk]# awk -F ':' '$3==0 {print $1}' test.txt
root
[root@hch awk]# awk -F ':' '$3==0 {print $0}' test.txt
root:x:0:0:root:/root:/bin/bash
[root@hch awk]# awk -F ':' '$3>=1000 {print $0}' test.txt
mysql:x:1000:1000::/home/mysql:/bin/bash
說明:?當“1000”加引號時會被當做是字符串,以ASC碼(二進制)的方式進行計算處理,不加引號的時候會被當做是數值處理。
[root@hch awk]# head -n3 test.txt |awk -F ':' '{OFS=":"} $1="root"'
root:x:0:0:root:/root:/bin/bash
root:x:1:1:bin:/bin:/sbin/nologin
root:x:2:2:daemon:/sbin:/sbin/nologin
說明:?當使用一個“=”等號時表示為等號前面字符賦值,使用兩個“==”表示邏輯關系(進行判斷)。
9.7 awk 命令(下)
字符比較大小
[root@hch awk]# awk -F ':' '$3<$4' test.txt
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
[root@hch awk]# awk -F ':' '$3==$4' test.txt |head -3
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@hch awk]# awk -F ':' '$3>"5" && $3<"7"' test.txt
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
說明:?“&&”表示并且。
[root@hch awk]# awk -F ':' '$3>1000 || $7!="/sbin/nologin"' test.txt
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mysql:x:1000:1000::/home/mysql:/bin/bash
[root@hch awk]# awk -F ':' '$3>1000 || $7~/bash/' test.txt
root:x:0:0:root:/root:/bin/bash
mysql:x:1000:1000::/home/mysql:/bin/bash
說明:?“||”表示或者。
OFS指定打印時的分隔符
[root@hch awk]# awk -F ':' '{OFS="#"} $3>1000 || $7~/bash/ {print $1,$3,$7}' test.txt
root#0#/bin/bash
mysql#1000#/bin/bash
[root@hch awk]# awk -F ':' '{OFS="#"} {if ($3>1000 || $7~/bash/) {print $1,$3,$7}}' test.txt
root#0#/bin/bash
mysql#1000#/bin/bash
NR (=number row)表示行號
[root@hch awk]# awk -F ':' '{OFS="#"} $3>1000 || $7~/bash/ {print $1,$3,$7}' test.txt
root#0#/bin/bash
mysql#1000#/bin/bash
[root@hch awk]# awk -F ':' '{OFS="#"} {if ($3>1000 || $7~/bash/) {print $1,$3,$7}}' test.txt
root#0#/bin/bash
mysql#1000#/bin/bash
[root@hch awk]# awk -F ':' '$3<5 && $7!="/sbin/nologin" {print NR":"$1}' test.txt
1:root
[root@hch awk]# awk -F ':' '$3<5 && $3>2 && $7=="/sbin/nologin" {print NR":"$1}' test.txt
4:adm
5:lp
[root@hch awk]# awk -F ':' 'NR<=3' test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@hch awk]# awk -F ':' 'NR<=3 && $1~/root/' test.txt
root:x:0:0:root:/root:/bin/bash
注:?類似于grep -n。
NF (=number fragment)表示段數(列)