awk工具

9.6 awk命令(上)

awk是一種編程語(yǔ)言,用于在linux/unix下對(duì)文本和數(shù)據(jù)進(jìn)行處理。數(shù)據(jù)可以來自標(biāo)準(zhǔn)輸入(stdin)、一個(gè)或多個(gè)文件,或其它命令的輸出。它支持用戶自定義函數(shù)和動(dòng)態(tài)正則表達(dá)式等先進(jìn)功能,是linux/unix下的一個(gè)強(qiáng)大編程工具。它在命令行中使用,但更多是作為腳本來使用。awk有很多內(nèi)建的功能,比如數(shù)組、函數(shù)等,這是它和C語(yǔ)言的相同之處,靈活性是awk最大的優(yōu)勢(shì)。

awk應(yīng)用

awk打印指定內(nèi)容

awk ‘{print $n}’ [filename] 在此n代表數(shù)字(當(dāng)n=0時(shí)$0代表文件所有內(nèi)容,當(dāng)n=1,2,3...時(shí)$1,2,3...代表相應(yīng)列)

awk -F 指定分隔符(默認(rèn)以空格為分隔符)

[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

可以指定打印各列內(nèi)容時(shí)的分隔符號(hào):

[root@hch awk]#awk -F ':' '{print $1"#"$2"#"$3}' test.txt |head -3

root#x#0

bin#x#1

daemon#x#2

說明:?指定分隔符后該命令只識(shí)別指定的分隔符,如果某行無指定的分隔符則會(huì)打印其整行!

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

注:‘~’表示匹配!

支持正則表達(dá)式:

[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

支持多個(gè)表達(dá)式同時(shí)執(zhí)行:

[root@hch awk]# awk -F ':' '/root/ {print $1,$3} /user/ {print $1,$3,$4}' test.txt

root 0

operator 11

tss 59 59

針對(duì)數(shù)學(xué)表達(dá)式的用法

數(shù)值比較

當(dāng)'$3>=1000 {print $1}'時(shí):

[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

說明:?當(dāng)“1000”加引號(hào)時(shí)會(huì)被當(dāng)做是字符串,以ASC碼(二進(jìn)制)的方式進(jìn)行計(jì)算處理,不加引號(hào)的時(shí)候會(huì)被當(dāng)做是數(shù)值處理。

[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

說明:?當(dāng)使用一個(gè)“=”等號(hào)時(shí)表示為等號(hào)前面字符賦值,使用兩個(gè)“==”表示邏輯關(guān)系(進(jìn)行判斷)。

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指定打印時(shí)的分隔符

[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)表示行號(hào)

[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)表示段數(shù)(列)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • awk命令的基本使用 [root@shellscript ~]# head -n 3 /etc/passwd | ...
    古寒飛閱讀 1,116評(píng)論 0 2
  • 一.基本介紹 1.awk: awk是一個(gè)強(qiáng)大的文本分析工具,在對(duì)文本文件的處理以及生成報(bào)表,awk是無可替代的。a...
    大福技術(shù)閱讀 1,744評(píng)論 1 4
  • awk: grep,sed,awk grep:文本過濾 sed:文本編輯 awk:文本格式化工具; 1 什么是aw...
    木林森閱讀 1,919評(píng)論 0 16
  • 系統(tǒng)巡檢腳本:Version 2016.08.09 ############################ 系統(tǒng)...
    NamasAmitabha閱讀 1,487評(píng)論 0 0
  • 1.創(chuàng)建文件夾 !/bin/sh mkdir -m 777 "%%1" 2.創(chuàng)建文件 !/bin/sh touch...
    BigJeffWang閱讀 10,509評(píng)論 3 53

友情鏈接更多精彩內(nèi)容