網(wǎng)站模板 psd免費(fèi)注冊(cè)個(gè)人網(wǎng)站不花錢(qián)
目錄
一、grep
1、參數(shù):
2、示例:
3、grep 正則匹配示例
二、sed
示例:
三、awk
一、語(yǔ)法:
二、常用參數(shù)
?三、示例
四、條件與循環(huán)語(yǔ)句
五、awk調(diào)用函數(shù)
1、grep:過(guò)濾文本,過(guò)濾出文本中符合要求的行
2、sed:修改文本,對(duì)文本中的行增,刪,改,查
3、awk:處理文本,文本中的行和列進(jìn)行處理
grep 經(jīng)常與sed、awk合用
grep、sed、awk、經(jīng)常與sort、uniq、wc等命令一起使用
文本三劍客支持正則表達(dá)式,因此學(xué)習(xí)之前建議先學(xué)一下正則表達(dá)式
一、grep
????????作用:文本搜索工具,根據(jù)用戶(hù)指定的“模式(過(guò)濾條件)”對(duì)目標(biāo)文本逐行進(jìn)行匹配檢查,打印匹配到的行。
1、參數(shù):
-i:忽略大小寫(xiě)
-n:顯示行號(hào)
-c:顯示行數(shù)
-e:支持基礎(chǔ)的正則表達(dá)式 多條件過(guò)濾 or and
-E:支持?jǐn)U展的
-v:反轉(zhuǎn)查找,輸出與模式不相符的行
-r:遞歸搜索所有文件
-d:過(guò)濾文件
-o:只顯示匹配的內(nèi)容
-A: 找到匹配行以及后幾行 比如-A2(后兩行)-A5(后5行)
-B:輸出匹配行以及前幾行 比如:-B2(前兩行) -B5(前5行)
-C:既要顯示前幾行 又要顯示后幾行 比如:-C5(當(dāng)前行的前后5行)
-a:不要忽略二進(jìn)制數(shù)據(jù)
-l:列出所包含的文件
-L:列出不包含的文件
2、示例:
[root@k8s-lb-backup tmp]# cat grepdemo.txt
hello word
Hi wuz\haobo
hello "hahaha" hehehe
L%ala'l'a
123/45/61、過(guò)濾 h 開(kāi)頭的行,忽略大小寫(xiě):-i、顯示行號(hào) -n[root@k8s-lb-backup tmp]# grep '^h' -i -n grepdemo.txt 1:hello word2:Hi wuzhaobo3:hello hahaha hehehe2、使用正則表達(dá)式的轉(zhuǎn)義字符,過(guò)濾有雙引號(hào)的行,使用單引號(hào) '' 或者轉(zhuǎn)義符 \ 或者使用 -F[root@k8s-lb-backup tmp]# grep '"' grepdemo.txt hello "hahaha" hehehe[root@k8s-lb-backup tmp]# grep \" grepdemo.txt hello "hahaha" hehehe[root@k8s-lb-backup tmp]# grep '/' grepdemo.txt 123/45/6[root@k8s-lb-backup tmp]# grep '\\' grepdemo.txt Hi wuz\haobo[root@k8s-lb-backup tmp]# grep -F 'z\h' grepdemo.txt Hi wuz\haobo[root@k8s-lb-backup tmp]# grep \'l\' grepdemo.txt L%ala'l'a3、過(guò)濾二進(jìn)制文件 使用 -a 參數(shù)[root@k8s-lb-backup tmp]# printf 'hello word' |grep hellohello word[root@k8s-lb-backup tmp]# [root@k8s-lb-backup tmp]# printf 'hello word\0' |grep hello匹配到二進(jìn)制文件 (標(biāo)準(zhǔn)輸入)[root@k8s-lb-backup tmp]# printf 'hello word\0' |grep -a hellohello word4、過(guò)濾文件的前兩行、后兩行、前后各兩行[root@k8s-lb-backup tmp]# grep -B2 'hah' grepdemo.txt hello wordHi wuz\hao\bohello "hahaha" hehehe[root@k8s-lb-backup tmp]# grep -A2 'hah' grepdemo.txt hello "hahaha" heheheL%ala'l'a123/45/6[root@k8s-lb-backup tmp]# grep -C2 'hah' grepdemo.txt hello wordHi wuz\hao\bohello "hahaha" heheheL%ala'l'a123/45/65、過(guò)條件過(guò)濾 使用-e 參數(shù) 既包含 hello 也包含 wuz[root@k8s-lb-backup tmp]# grep -i -e 'hello' -e 'wuz' grepdemo.txt hello wordHi wuz\hao\bohello "hahaha" hehehe[root@k8s-lb-backup tmp]# grep -i hello grepdemo.txt |grep hehhello "hahaha" hehehe6、反向查找,ps aux 過(guò)濾bash 使用 -v 參數(shù)[root@k8s-lb-backup tmp]# ps aux | grep bashroot 739 0.0 0.0 115404 964 ? S 09:35 0:00 /bin/bash /usr/sbin/ksmtunedroot 1994 0.0 0.0 116332 3064 pts/0 Ss 09:36 0:00 -bashroot 103695 0.0 0.0 112828 988 pts/0 S+ 14:09 0:00 grep --color=auto bash[root@k8s-lb-backup tmp]# ps aux | grep bash | grep -v grep | grep -v ksmtunedroot 1994 0.0 0.0 116332 3064 pts/0 Ss 09:36 0:00 -bash7、列出所包含的文件:使用-l參數(shù),列出不包含的文件 使用-L 參數(shù) 使用 -r 可以遞歸過(guò)濾目錄[root@k8s-lb-backup tmp]# grep -l hello *.txt1.txtgrepdemo.txt[root@k8s-lb-backup tmp]# grep -L hello *.txt8、grep 管道合用[root@k8s-lb-backup tmp]# ps aux | grep bash9、grep 與 wc (統(tǒng)計(jì)數(shù)量)和uniq(相鄰去重)和sort(排序)合用[root@k8s-lb-backup tmp]# echo 'hello world' |wc1(行) 2(個(gè)單詞) 12(字符)[root@k8s-lb-backup tmp]# echo 'hello wrold' |wc -c 12(直接統(tǒng)計(jì)字符)[root@k8s-lb-backup tmp]# echo 'hello wrold' |wc -l1(統(tǒng)計(jì)行數(shù))[root@k8s-lb-backup tmp]# echo 'hello wrold' |wc -w2(統(tǒng)計(jì)單詞)[root@k8s-lb-backup tmp]# grep -i -o hello grepdemo.txt | uniq (去重)hello [root@k8s-lb-backup tmp]# grep -i -o hello grepdemo.txt | sort -u 排序并去重使用 -u 參數(shù)hello10、xargs的使用:格式化輸出[root@k8s-lb-backup tmp]# cat 2.txt a b c d e f gh i g k l m no p qr s t u v wx y z[root@k8s-lb-backup tmp]# cat 2.txt | xargsa b c d e f g h i g k l m n o p q r s t u v w x y z[root@k8s-lb-backup tmp]# cat 2.txt | xargs | wc1 26 52[root@k8s-lb-backup tmp]# cat 2.txt | xargs -n 5 每行以5列輸出a b c d ef g h i gk l m n op q r s tu v w x yz11、grep 過(guò)濾進(jìn)程 并與awk合用 kill 殺死進(jìn)程[root@k8s-lb-backup tmp]# ps aux | grep bashroot 762 0.0 0.0 115404 956 ? S 09:25 0:00 /bin/bash /usr/sbin/ksmtunedroot 1897 0.0 0.0 116332 3032 pts/0 Ss 09:26 0:00 -bashroot 6444 0.0 0.0 112828 988 pts/0 S+ 09:38 0:00 grep --color=auto bash[root@k8s-lb-backup tmp]# ps aux | grep bash | grep -v grep | grep -v ksmtuned root 1897 0.0 0.0 116332 3032 pts/0 Ss 09:26 0:00 -bash[root@k8s-lb-backup tmp]# ps aux | grep bash | grep -v grep | grep -v ksmtuned | awk '{print $2}'1897[root@k8s-lb-backup tmp]# ps aux | grep bash | grep -v grep | grep -v ksmtuned | awk '{print "kill -9" $2} | sh[root@k8s-lb-backup tmp]# kill -9 `ps aux | grep bash | grep -v grep | grep -v ksmtuned | awk '{print $2}'`12、grep 與 sed 合用,查找本機(jī)IP地址先[root@k8s-lb-backup tmp]# ifconfig 在一層一層過(guò)濾 [root@k8s-lb-backup tmp]# ifconfig ens33[root@k8s-lb-backup tmp]# ifconfig ens33 | grep inetinet 192.168.134.141 netmask 255.255.255.0 broadcast 192.168.134.255inet6 fe80::e8e1:227e:c151:d3b6 prefixlen 64 scopeid 0x20<link>[root@k8s-lb-backup tmp]# ifconfig ens33 | grep inet | grep -v inet6inet 192.168.134.141 netmask 255.255.255.0 broadcast 192.168.134.255# 把inet之前的替換成空[root@k8s-lb-backup tmp]# ifconfig ens33 | grep inet | grep -v inet6 | sed 's/^.*inet//g'192.168.134.141 netmask 255.255.255.0 broadcast 192.168.134.255#再把netmask之后的替換成空[root@k8s-lb-backup tmp]# ifconfig ens33 | grep inet | grep -v inet6 | sed 's/^.*inet//g' | sed 's/netmask.*$//g'192.168.134.141
3、grep 正則匹配示例
1、匹配以 'he' 開(kāi)頭的行
[root@k8s-lb-backup tmp]# grep ^he grepdemo.txt
hello word
hello "hahaha" hehehe2、匹配以'6'結(jié)尾的行
[root@k8s-lb-backup tmp]# grep 6$ grepdemo.txt
123/45/63、匹配一個(gè)非換行字符 使用 . .表示任意字符
[root@k8s-lb-backup tmp]# grep w.rd grepdemo.txt
hello word4、匹配前一個(gè)字符0次或者1次以上
[root@k8s-lb-backup tmp]# grep hel* grepdemo.txt
hello word
hello "hahaha" hehehe5、匹配任意字符
[root@k8s-lb-backup tmp]# cat grepdemo.txt
hello word
Hi wuz\hao\bo
hello "hahaha" hehehe
L%ala'l'a
mysqla
mysqlb
mysqlA
mysqlB
mysql3
mysql5[root@k8s-lb-backup tmp]# grep mysql[a-zA-Z0-9] grepdemo.txt
mysqla
mysqlb
mysqlA
mysqlB
mysql3
mysql56、某一行以wo開(kāi)頭 和以rd結(jié)尾的字符
[root@k8s-lb-backup tmp]# grep '\<wo' grepdemo.txt
hello word
[root@k8s-lb-backup tmp]# grep 'rd\>' grepdemo.txt
hello word
二、sed
????????sed 是對(duì)文本文件中的某一行進(jìn)行增刪改查等操作,嘗嘗使用sed對(duì)文件的指定內(nèi)容進(jìn)行批量替換。
語(yǔ)法:
sed [options] 'asd [flags]' filename
命令參數(shù)
-f:后面跟一個(gè)文本文件,表示將sed的動(dòng)作卸載一個(gè)文件內(nèi)
-i:直接修改文件內(nèi)容,sed加上此參數(shù)后對(duì)文件的修改會(huì)生效
-r:支持?jǐn)U展正則表達(dá)式
-n:只打印匹配到的行,經(jīng)常與 -p 一起使用
-e:邏輯和 默認(rèn)選項(xiàng)
sed 常用的內(nèi)部命令:d:刪除匹配的行,并立即啟用下一輪循環(huán)p:打印當(dāng)前模式空間內(nèi)容,追加到默認(rèn)輸出之后a:在指定行后面追加文本,支持使用\n實(shí)現(xiàn)多行追加i:在行前面插入文本,支持使用\n實(shí)現(xiàn)多行追加c:替換行為單行或多行文本,支持使用\n實(shí)現(xiàn)多行追加w:保存模式匹配的行至指定文件r:讀取指定文件的文本至模式空間中匹配到的行后=:為模式空間中的行打印行號(hào)!:模式空間中匹配行取反處理s///:查找替換,支持使用其它分隔符,如:s@@@,s###;加g表示行內(nèi)全局替換;在替換時(shí),可以加一下命令,實(shí)現(xiàn)大小寫(xiě)轉(zhuǎn)換\l:把下個(gè)字符轉(zhuǎn)換成小寫(xiě)。\L:把replacement字母轉(zhuǎn)換成小寫(xiě),直到\U或\E出現(xiàn)。\u:把下個(gè)字符轉(zhuǎn)換成大寫(xiě)。\U:把replacement字母轉(zhuǎn)換成大寫(xiě),直到\L或\E出現(xiàn)。\E:停止以\L或\U開(kāi)始的大小寫(xiě)轉(zhuǎn)換
flags:數(shù)字:表示新文本替換的模式g:全局 全部p:表示打印原始的內(nèi)容w filename :表示將替換的結(jié)果寫(xiě)入文件
示例:
sed -e '1s/test/trial/g' test.txt
1:表示 test.txt 的第一行
s:表示 替換
test 是原內(nèi)容
trial 是替換成的內(nèi)容
g 整行 替換
示例:
一、替換
[root@k8s-lb-backup tmp]# cat seddemo.txt
This is a test of the trial script.
This is the second test of the trial script.1、將文本中第一行的 test 替換成 trial,并打印出來(lái),但是不會(huì)修改文件內(nèi)容,如果要修改文件內(nèi)容 加上 -i 參數(shù)[root@k8s-lb-backup tmp]# sed -e '1s/test/trial/g' seddemo.txt This is a trial of the trial script.This is the second test of the trial script.2、全局替換并寫(xiě)入文件 [root@k8s-lb-backup tmp]# sed -i 's/test/trial/g' seddemo.txt3、把第二行的第一個(gè)替換[root@k8s-lb-backup tmp]# sed '2s/test/asd/1' seddemo.txt This is a trial of the trial script.This is the second asd of the trial script.二、刪除(d)和添加(a,i)
1、刪除所有[root@k8s-lb-backup tmp]# sed 'd' seddemo.txt 2、刪除1行[root@k8s-lb-backup tmp]# sed '1d' seddemo.txt This is the second test of the trial script.3、刪除1-2行[root@k8s-lb-backup tmp]# sed '1,2d' seddemo.txt 4、刪除2-4行[root@k8s-lb-backup tmp]# sed '2,4d' seddemo.txt5、在第2行前面插入[root@k8s-lb-backup tmp]# sed '2i my name is wuzhaobo' seddemo.txt This is a trial of the trial script.my name is wuzhaoboThis is the second test of the trial script.6、在第2行后面插入[root@k8s-lb-backup tmp]# sed '2a my name is wuzhaobo' seddemo.txt This is a trial of the trial script.This is the second test of the trial script.my name is wuzhaobo7、在第一行前面插入2行[root@k8s-lb-backup tmp]# sed '1i my name is wuzhaobo.\nthis is two hang' seddemo.txt my name is wuzhaobo.this is two hangThis is a trial of the trial script.This is the second test of the trial script.三、sed 替換 c 參數(shù)
1、把第二行替換[root@k8s-lb-backup tmp]# sed '2c this is line number 2.' seddemo.txt This is a trial of the trial script.this is line number 2.四、sed 轉(zhuǎn)換 y 參數(shù)
1、把文件里的a轉(zhuǎn)換成大寫(xiě)A 參數(shù)是一一 對(duì)應(yīng)的。[root@k8s-lb-backup tmp]# sed 'y/a/A/' seddemo.txt This is A triAl of the triAl script.This is the second test of the triAl script.[root@k8s-lb-backup tmp]# echo 'this is 1' | sed 'y/123/456/'this is 4五、sed p 打印命令
[root@k8s-lb-master tmp]# vim demo
aaa
bbbb
AABBCCDD
1、只打印匹配到的行[root@k8s-lb-master tmp]# sed -n "/aaa/p" demo aaa
2、打印第2行[root@k8s-lb-master tmp]# sed -n "2p" demo Bbbb
3、先把包含3的這一行打印出來(lái),然后在把包含3的這一行中的bbbb替換成1111 ,在打印出來(lái)[root@k8s-lb-backup tmp]# sed -n '/3/{> p> s/bbbb/1111/p> }' seddemo.txtbbbb311113六、sed q 退出腳本
1、查找文件中 aaa 這一行 ,并把a(bǔ)aa替換成111,然后退出。[root@k8s-lb-backup tmp]# sed '/aaa/{s/aaa/111/;q}' seddemo.txt 111
把TXT中的h1、h2、h3加上尖括號(hào)
[root@k8s-lb-backup tmp]# cat test.txt
<html>
<title>First Web</title>
<body>
h1Hello1h1
h2Hello2h2
h3hello3h3
</body>
</html>
1、
[root@k8s-lb-backup tmp]# sed '{s/h[0-9]/\<&\>/1;s/h[0-9]/\<\/&\>/2}' test.txt
<html>
<title>First Web</title>
<body>
<h1>Hello1</h1>
<h2>Hello2</h2>
<h3>hello3</h3>
</body>
</html>
2、或者,把/h[0-9] 公共的 提到前邊
[root@k8s-lb-backup tmp]# sed '/h[0-9]/{s//\<&\>/1;s//\<\/&\>/2}' test.txt
<html>
<title>First Web</title>
<body>
<h1>Hello1</h1>
<h2>Hello2</h2>
<h3>hello3</h3>
</body>
</html>
講解:使用替換的放是 s/a/b/1:第一次把a(bǔ)替換成b,多條命令用;隔開(kāi),&表示:當(dāng)前匹配到的內(nèi)容。2:表示發(fā)生在第二個(gè)匹配位置
3、-或者使用調(diào)用腳本參數(shù) -f 來(lái)執(zhí)行
[root@k8s-lb-backup tmp]# cat a.sh
/h[0-9]/{s//\<&\>/1s//\<\/&\>/2
}
[root@k8s-lb-backup tmp]# sed -f a.sh test.txt
<html>
<title>First Web</title>
<body>
<h1>Hello1</h1>
<h2>Hello2</h2>
<h3>hello3</h3>
</body>
</html>
三、awk
AWK是一種編程語(yǔ)言,用于在linux下對(duì)文本和數(shù)據(jù)進(jìn)行處理。可以處理每一行的每一列
一、語(yǔ)法:
awk 'BEGIN {commands;……} pattern {commands;……} END {commands}' filename
- BEGIN:處理數(shù)據(jù)前執(zhí)行的命令
- END:處理數(shù)據(jù)后執(zhí)行的命令
- pattern:模式,每一行都執(zhí)行的命令
- BEGIN和END里的命令只是執(zhí)行一次
- pattern:里的命令會(huì)匹配每一行去處理
二、常用參數(shù)
-F fs:fs指定輸入分隔符,fs可以是字符串或正則表達(dá)式,如-F: 默認(rèn)使用 空格
-v var=value:賦值一個(gè)用戶(hù)定義變量,將外部變量傳遞給awk
-f scripfile:從腳本文件中讀取awk命令內(nèi)置變量:FS :輸入字段分隔符,默認(rèn)為空白字符OFS :輸出字段分隔符,默認(rèn)為空白字符RS :輸入記錄分隔符,指定輸入時(shí)的換行符,原換行符仍有效ORS :輸出記錄分隔符,輸出時(shí)用指定符號(hào)代替換行符NF :字段數(shù)量,共有多少字段, $NF引用最后一列,$(NF-1)引用倒數(shù)第2列NR :行號(hào),后可跟多個(gè)文件,第二個(gè)文件行號(hào)繼續(xù)從第一個(gè)文件最后行號(hào)開(kāi)始FNR :各文件分別計(jì)數(shù), 行號(hào),后跟一個(gè)文件和NR一樣,跟多個(gè)文件,第二個(gè)文件行號(hào)從1開(kāi)始FILENAME :當(dāng)前文件名ARGC :命令行參數(shù)的個(gè)數(shù)ARGV :數(shù)組,保存的是命令行所給定的各參數(shù),查看參數(shù)
操作符:算數(shù)操作符:x+y, x-y, x*y, x/y, x^y, x%y-x: 轉(zhuǎn)換為負(fù)數(shù)+x: 轉(zhuǎn)換為數(shù)值比較操作符:==, !=, >, >=, <, <=模式匹配符:~ :左邊是否和右邊匹配包含 !~ :不包含邏輯操作符:與&& ,或|| ,非!函數(shù)調(diào)用: function_name(argu1, argu2, ...)條件表達(dá)式(三目表達(dá)式):selector?if-true-expression:if-false-expression注釋:先判斷selector,如果符合執(zhí)行 ? 后的操作;否則執(zhí)行 : 后的操作
?三、示例
創(chuàng)建awkdemo目錄,準(zhǔn)備兩個(gè)測(cè)試文件
[root@k8s-lb-backup awkdemo]# cp /etc/passwd ./
[root@k8s-lb-backup awkdemo]# ps aux > test.txt 1、打印每一行 $:代表是行 $0:表示所有行 $1:表示第一個(gè)字段的值 $2:表示第二個(gè)字段的值,$NF:表示最后一個(gè)字段的值 $(NF-1):表示倒數(shù)第二個(gè)字段的值
[root@k8s-lb-backup awkdemo]# awk '{print $0}' test.txt2、打印第一列 $1
[root@k8s-lb-backup awkdemo]# awk '{print $1}' test.txt3、打印文件前5行 NR<5[root@k8s-lb-backup awkdemo]# awk 'NR<5' test.txt USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.1 125396 3912 ? Ss 09:14 0:01 /usr/lib/syroot 2 0.0 0.0 0 0 ? S 09:14 0:00 [kthreadd]root 3 0.0 0.0 0 0 ? S 09:14 0:00 [ksoftirqd/0]打印文件前5行,如果第一個(gè)文件行數(shù)不夠 就從第二個(gè)開(kāi)始[root@k8s-lb-backup awkdemo]# awk 'NR<5' test1.txt passwd USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 3 0.0 0.0 0 0 ? S 09:14 0:00 [ksoftirqd/0]root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin4、打印最后一列字段的值、和倒數(shù)第二行的值 $NF $(NF-1)[root@k8s-lb-backup awkdemo]# awk '{print $NF}' test.txt[root@k8s-lb-backup awkdemo]# awk '{print $(NF-1)}' test.txt5、指定分隔符,并打印第一個(gè)字段的值 -F ':'[root@k8s-lb-backup awkdemo]# awk -F ':' '{print $1}' passwd 6、指定分隔符,并打印包含job的行,或者某個(gè)值[root@k8s-lb-backup awkdemo]# awk -F : '/job/{print $0}' passwd job:x:1000:1000:job:/home/job:/bin/bash7、begin 和 end 的使用,以冒號(hào)分割,先打印 hello 在打印第一列的值,每一行+1,最后打印一共多少行,和輸出 goodbye[root@k8s-lb-backup awkdemo]# awk -F : 'BEGIN{ print "hello" }{ print $1;i+=1 } END { print i;print "goodbye"}' passwd 或者從腳本中執(zhí)行 -f[root@k8s-lb-backup awkdemo]# vim a.awkBEGIN{FS=":"; print "hello" }{ print $1;i+=1 }END { print i;print "goodbye"}[root@k8s-lb-backup awkdemo]# awk -f a.awk passwd 8、添加條件,比如查找 passwd 中 pid>uid的行,并打印[root@k8s-lb-backup awkdemo]# awk -F: '$4 > $3 {print $0}' passwd二、操作符示例
1、查詢(xún)以dev開(kāi)頭的磁盤(pán)信息 匹配使用 ~ 符號(hào)[root@k8s-lb-backup awkdemo]# df -h | awk '$0 ~ /^\/dev/'/dev/mapper/centos-root 36G 3.8G 32G 11% //dev/sda1 1014M 179M 836M 18% /boot2、只打印磁盤(pán)名和使用狀況 使用 $ 和 NF過(guò)濾[root@k8s-lb-backup awkdemo]# df -h | awk '$0 ~ /^\/dev/{print $1 "-----"$(NF-1)}'/dev/mapper/centos-root-----11%/dev/sda1-----18%3、查找磁盤(pán)大于15%的[root@k8s-lb-backup awkdemo]# df -h | awk '$0 ~ /^\/dev/{print $1 "="$(NF-1)}' | awk -F= '$2 > 15'/dev/sda1=18%三、邏輯操作符 并且&& 或|| 非!
1、查看passwd 中第三列的值 大于10 并且小于1000 的值[root@k8s-lb-backup awkdemo]# awk -F : '$3 > 10 && $3 < 1000 { print $3 }' passwd 2、查看passwd 中第三列的值 大于10 或者小于1000 的值[root@k8s-lb-backup awkdemo]# awk -F : '$3 > 10 || $3 < 1000 { print $3 }' passwd 3、查看passwd 中第三列的值 不等于1000 的值[root@k8s-lb-backup awkdemo]# awk -F : '$3 !=1000 { print $3 }' passwd 4、查看第四列之和[root@k8s-lb-backup awkdemo]# awk -F : '{ sum+=$4 } END {print sum}' passwd 76412
5、查看系統(tǒng) CPU和內(nèi)存和[root@k8s-lb-backup awkdemo]# ps -aux | grep -v USER | awk '{ cpu+=$3;mem+=$4 } END { print cpu;print mem }'08.3四、三目表達(dá)式
[root@k8s-lb-backup awkdemo]# awk -F : '{ $3 >= 1000 ? usertype="common user" : usertype="sysadmin user"; print usertype,$1,$3}' passwd
四、條件與循環(huán)語(yǔ)句
一、控制語(yǔ)句if-else語(yǔ)法
if(condition){statement;…}[elsestatement] 雙分支
if(condition1){statement1}else if(condition2){statement2}else{statement3} 多分支
示例:對(duì)passwd 判斷 當(dāng)$3 的UID 當(dāng)UID=0時(shí),就是超級(jí)用戶(hù),當(dāng)UID大于1 小于等于999,就是系統(tǒng)用戶(hù),其余的就是普通用戶(hù)[root@k8s-lb-backup awkdemo]# vim a.awkBEGIN{FS=":"}{if ($3==0){print $1 "is super user";} else if($3>1 && $3<=999 ){print $1 "is system user";}else{print $1 "is common user";}}[root@k8s-lb-backup awkdemo]# awk -f a.awk passwd
2、每隔5行打印分隔符,就是5的倍數(shù) 使用NR%5==0
[root@k8s-lb-backup awkdemo]# awk '{if (NR%5==0){ print "----------"}print $0 }' passwd
二、awk 循環(huán)語(yǔ)句 for
1、對(duì)每一行打印5次[root@k8s-lb-backup awkdemo]# awk -F : '{for (i=5;i>0;i--){print $0} }' passwd
2、使用for 循環(huán) 1至100的和[root@k8s-lb-backup awkdemo]# awk 'BEGIN {sum=0; for(i=1;i<=100;i++){sum+=i}print sum}'5050
3、使用while 循環(huán) 1至100的和[root@k8s-lb-backup awkdemo]# awk 'BEGIN {sum=0;i=1; while(i<=100){sum+=i;i++};print sum}'5050
4、使用 do while 循環(huán) 1至100的和[root@k8s-lb-backup awkdemo]# awk 'BEGIN {sum=0;i=1; do{sum+=i;i++}while(i<=100);print sum}'5050
5、使用for循環(huán)打印99乘法表
[root@k8s-lb-backup awkdemo]# awk 'BEGIN{for(i=1;i<=9;i++){for(j=1;j<=i;j++)printf i "*" j "=" i*j "\t";print("\n")}}'
1*1=12*1=2 2*2=43*1=3 3*2=6 3*3=94*1=4 4*2=8 4*3=12 4*4=165*1=5 5*2=10 5*3=15 5*4=20 5*5=256*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=367*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=498*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=649*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
6、while 循環(huán)打印99乘法表
[root@k8s-lb-backup awkdemo]# vim t.sh
BEGIN {i=1while (i<=9){j=1while(j<=i){printf i "*" j "=" i*jprintf "\t"j++}i++print "\n"}
}
[root@k8s-lb-backup awkdemo]# awk -f t.sh
1*1=12*1=2 2*2=43*1=3 3*2=6 3*3=94*1=4 4*2=8 4*3=12 4*4=165*1=5 5*2=10 5*3=15 5*4=20 5*5=256*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=367*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=498*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=649*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
五、awk調(diào)用函數(shù)
1、截取字符串 使用 substr函數(shù) substr(string,第幾位開(kāi)始,截取幾位)
[root@k8s-lb-backup awkdemo]# awk -f t.sh > substr.txt[root@k8s-lb-backup awkdemo]# awk '{if(substr($3,5,2)==12){print $0}}' substr.txt
4*1=4 4*2=8 4*3=12 4*4=162、使用length 計(jì)算字符串長(zhǎng)度
[root@k8s-lb-backup awkdemo]# awk '{if(substr($3,5,2)==12){print $0}{print length($3)}}' substr.txt [root@k8s-lb-backup awkdemo]# vim a.sh
function test(t1,t2){t1 > t2 ? var=t1 : var=t2return var
}
BEGIN{a=3;b=2;print test(a,b)
}[root@k8s-lb-backup awkdemo]# awk -f a.sh
3