-e匹配多個模式,-v反轉輸出,-f匹配多個文件中相同的行 -i忽略大小寫 -o只輸出匹配內容 sed 流編輯器,過濾和替換文本。 工作原理:sed命令將當前處理的行讀入模式空間進行處理,處理完把結果輸出,並清空模式空間。然後再將下一行讀入模式空間進行處理輸出,以此類推,直至最後一行。 用法:sed ...
[root@VM_0_7_centos tmp]# cat 1.txt 1 2 3 4 5 6 [root@VM_0_7_centos tmp]# cat 2.txt 4 5 6 7 8 [root@VM_0_7_centos tmp]# grep -f 1.txt 2.txt 4 5 6 [root@VM_0_7_centos tmp]# grep -f -v 1.txt 2.txt grep: -v: No such file or directory [root@VM_0_7_centos tmp]# grep -v -f 1.txt 2.txt 7 8
-e匹配多個模式,-v反轉輸出,-f匹配多個文件中相同的行
[root@VM_0_7_centos tmp]# echo "11 22 33 44 55" |xargs -n1 | grep -e "11" 11 [root@VM_0_7_centos tmp]# echo "11 22 33 44 55" |xargs -n1 | grep -e "11" -e "22" 11 22
-i忽略大小寫
[root@VM_0_7_centos tmp]# echo "AA aa bb " | xargs -n1 | egrep -i "aa" AA aa
-o只輸出匹配內容
[root@VM_0_7_centos tmp]# ifconfig eth0 Link encap:Ethernet HWaddr 52:54:00:BE:3B:97 inet addr:172.17.0.7 Bcast:172.17.15.255 Mask:255.255.240.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:9272287 errors:0 dropped:0 overruns:0 frame:0 TX packets:9005522 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1614323123 (1.5 GiB) TX bytes:2170651985 (2.0 GiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:194402 errors:0 dropped:0 overruns:0 frame:0 TX packets:194402 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:213430544 (203.5 MiB) TX bytes:213430544 (203.5 MiB) [root@VM_0_7_centos tmp]# ifconfig | egrep "172.17.0.7" inet addr:172.17.0.7 Bcast:172.17.15.255 Mask:255.255.240.0 [root@VM_0_7_centos tmp]# ifconfig | egrep -o "172.17.0.7" 172.17.0.7
sed
流編輯器,過濾和替換文本。
工作原理:sed命令將當前處理的行讀入模式空間進行處理,處理完把結果輸出,並清空模式空間。然後再將下一行讀入模式空間進行處理輸出,以此類推,直至最後一行。
用法:sed [OPTION]... {script-only-if-no-other-script} [input-file]...
sed [選項] '地址 命令' file
選項 | 描述 |
-n | 不列印模式空間 |
-e | 執行腳本-表達式來處理 |
-f | 腳本文件的內容添加到命令被執行 |
-i | 修改源文件 |
-r | 使用擴展正則表達式 |
命令 | m描述 |
s/regexp/replacement/ | t替換字元串 |
p | d列印當前模式空間 |
P | d列印模式空間的第一行 |
d | s刪除模式空間,並且開始下一個迴圈 |
D | s刪除模式空間的第一行,開始下一個迴圈 |
= | d列印當前行號 |
a \text | d當前行追加文本 |
i \text | d當前行上面插入文本 |
c \text | s所選行替換文本 |
q | l立即退出sed腳本 |
r | z追加文本來自文件 |
地址 | m描述 |
first~step | b步長,每step行,從first開始 |
$ | p匹配最後一行 |
/regexp/ | z正則表達式匹配行 |
number | z只匹配製定行 |
addr1、addr2 | k開始匹配addr1開始,直接addr2行結束 |
addr1,+N | c從addr1行開始,向後的N行 |
addr1,~N | c從addr1行開始,到N行結束 |
[root@VM_0_7_centos tmp]# tail /etc/services nimgtw 48003/udp # Nimbus Gateway 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw iqobject 48619/tcp # iqobject iqobject 48619/udp # iqobject
[root@VM_0_7_centos tmp]# tail /etc/services | sed -n "/blp5/p" 匹配第一行 blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator
[root@VM_0_7_centos tmp]# tail /etc/services | sed -n "1p" 匹配第二行 nimgtw 48003/udp # Nimbus Gateway [root@VM_0_7_centos tmp]# tail /etc/services | sed -n "2p" 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
[root@VM_0_7_centos tmp]# tail /etc/services | sed -n "1,3p" 匹配1-3行 nimgtw 48003/udp # Nimbus Gateway 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services
[root@VM_0_7_centos tmp]# seq 10 | sed -n "1~2p" 從1開始,每次跳過一行 1 3 5 7 9
[root@VM_0_7_centos tmp]# tail /etc/services | sed -n "$p" [root@VM_0_7_centos tmp]# tail /etc/services | sed -n '$p' 列印最後一行 iqobject 48619/udp # iqobject
[root@VM_0_7_centos tmp]# tail /etc/services | sed -n '$!p' 不列印最後一行 nimgtw 48003/udp # Nimbus Gateway 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw iqobject 48619/tcp # iqobject
[root@VM_0_7_centos tmp]# tail /etc/services | sed -n '/^blp5/,/^com/p' 匹配從blp5到com開頭(第一個)的 blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw
[root@VM_0_7_centos tmp]# tail services | sed '/blp5/d' 刪除blp5行,不需要加-n nimgtw 48003/udp # Nimbus Gateway 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw iqobject 48619/tcp # iqobject iqobject 48619/udp # iqobject
[root@VM_0_7_centos tmp]# tail services | sed 's/blp5/test/' 將blp5替換為test,(沒有加全局就只修改第一個匹配到的字元串) nimgtw 48003/udp # Nimbus Gateway 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services test 48129/tcp # Bloomberg locator test 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw iqobject 48619/tcp # iqobject iqobject 48619/udp # iqobject [root@VM_0_7_centos tmp]# tail services | sed 's/blp5/test/g' 替換所有的blp5為test nimgtw 48003/udp # Nimbus Gateway 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services test 48129/tcp # Bloomberg locator test 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw iqobject 48619/tcp # iqobject iqobject 48619/udp # iqobject
[root@VM_0_7_centos tmp]# tail services |sed '1,5s/blp5/test/g' 替換1-5行的blp5為test
nimgtw 48003/udp # Nimbus Gateway 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services test 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw iqobject 48619/tcp # iqobject iqobject 48619/udp # iqobject
[root@VM_0_7_centos tmp]# tail services | sed 's/nimgtw/test/;s/udp/ll/' 對前面匹配到結果中的內容再進行匹配 test 48003/ll # Nimbus Gateway 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/ll # Image Systems Network Services blp5 48129/tcp # Bloomberg locator blp5 48129/ll # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/ll # com-bardac-dw iqobject 48619/tcp # iqobject iqobject 48619/ll # iqobject
[root@VM_0_7_centos tmp]# tail services | sed -r 's/(.*) ([0-9]+.*)(#.*)/\1/' nimgtw 3gpp-cbsp isnetserv isnetserv blp5 blp5 com-bardac-dw com-bardac-dw iqobject iqobject [root@VM_0_7_centos tmp]# tail services | sed -r 's/(.*) ([0-9]+.*)(#.*)/\1\2/' nimgtw 48003/udp 3gpp-cbsp 48049/tcp isnetserv 48128/tcp isnetserv 48128/udp blp5 48129/tcp blp5 48129/udp com-bardac-dw 48556/tcp com-bardac-dw 48556/udp iqobject 48619/tcp iqobject 48619/udp [root@VM_0_7_centos tmp]# tail services | sed -r 's/(.*) ([0-9]+.*)(#.*)/\1\2\3/' nimgtw 48003/udp # Nimbus Gateway 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw iqobject 48619/tcp # iqobject iqobject 48619/udp # iqobject
[root@VM_0_7_centos tmp]# seq 10 |sed '/5/,+3s/^/#/' 加註釋 1 2 3 4 #5 #6 #7 #8 9 10
[root@VM_0_7_centos tmp]# seq 10 | sed '5s/.*/&\ntest/' &的作用是取前面所匹配到的結果 1 2 3 4 5 test 6 7 8 9 10 [root@VM_0_7_centos tmp]# seq 10 | sed '5s/.*/test\n&/' 1 2 3 4 test 5 6 7 8 9 10
[root@VM_0_7_centos tmp]# seq 10 |sed 'n;n;s/^/ll/'
1
2
ll3
4
5
ll6
7
8
ll9
10
awk
awk是一個處理文本的編程語言工具,能用簡短的程式處理輸入文件、數據排序、計算以及生產報表等等。
在Linux系統下預設awk是gawk,它是awk的GUN版本。可以通過命令查看應用的版本:ll /bin/awk
基本的命令語法:awk option 'patten {action}' file
其中pattern表示AWK在數據中查找的內容,而action是在找到匹配內容時所執行的一系列命令。花括弧用於根據特定的模式對一系列指令進行分組。
pattern參數可以是egrep正則表達式的任意一個,使用/re/再加上一些樣式匹配,匹配方式與sed類似,也可以使用“,”分開兩樣式以選個某個範圍。
awk處理的工作方式與資料庫類似,支持對記錄和欄位處理,這也是grep和sed不能實現的。
在awk中,預設的情況下將文本文件中一行視為一個記錄,而將一行中的某一部分作為記錄中的一個欄位,用1,2,3....數字的方式順序的表示行(記錄)中的不同欄位。用$後跟數字,引用對應的欄位,以逗號分割,0表示整個行。
選項 | m描述 |
-f program-file | c從文件中讀取awk程式源文件 |
-F fs | z指定fs為輸入欄位分隔符 |
-v var=value | b變數賦值 |
--posix | z只支持POSIX正則表達式 |
Pattern | Description |
BEGIN{ } | g給程式賦予初始狀態,先執行的工作 |
END{ } | c程式結束之後執行的一些掃尾工作 |
/regular expression/ | w為每個輸入記錄匹配正則表達式 |
pattern && pattern | l邏輯and,滿足兩個模式 |
pattern || pattern |
邏輯或,滿足其中一個模式 |
! pattern | l邏輯非,不滿足模式 |
pattern1,pattern2 | f範圍模式,匹配所以1的記錄,直到匹配到模式2 |
[root@VM_0_7_centos tmp]# tail services nimgtw 48003/udp # Nimbus Gateway 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw iqobject 48619/tcp # iqobject iqobject 48619/udp # iqobject [root@VM_0_7_centos tmp]# tail services | awk -F '/' '{print $1}' 指定/為分割符,列印出第一部分 nimgtw 48003 3gpp-cbsp 48049 isnetserv 48128 isnetserv 48128 blp5 48129 blp5 48129 com-bardac-dw 48556 com-bardac-dw 48556 iqobject 48619 iqobject 48619
[root@VM_0_7_centos ~]# awk -va="123" 'BEGIN{print a}' 123 [root@VM_0_7_centos ~]# a=456 [root@VM_0_7_centos ~]# awk -va=$a 'BEGIN{print a}' 456
[root@Gin scripts]# awk '{print $0}' /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin ..................................................... [root@Gin scripts]# echo hhh|awk '{print "hello,world"}' hello,world [root@Gin scripts]# awk '{print "hiya"}' /etc/passwd hiya hiya hiya hiya ...............................................
調用awk時,指定/etc/passwd作為輸入文件,執行awk時,它依次對/etc/passwd中的每一行執行print命令。
所有輸出都發送到stdout,所得到的結果與執行cat /etc/passwd完全相同。
[root@VM_0_7_centos ~]# awk -F":" '{print $1}' /etc/passwd root bin daemon adm lp sync shutdown halt mail uucp operator games gopher ftp nobody vcsa abrt ntp saslauth postfix sshd dbus tcpdump cairui nginx mysql zabbix [root@VM_0_7_centos ~]# awk -F":" '{print $0}' /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin cairui:x:500:500::/home/cairui:/bin/bash nginx:x:501:501::/home/nginx:/sbin/nologin mysql:x:502:502::/home/mysql:/sbin/nologin zabbix:x:503:503::/home/zabbix:/sbin/nologin
列印1個100行文本的第20到30行
[root@VM_0_7_centos ~]# seq 100 | awk '{if(NR>=20 && NR<=30) print $1}' 20 21 22 23 24 25 26 27 28 29 30
[root@VM_0_7_centos tmp]# awk -F"[ ,]+" '{print $3" "$6}' test cai is [root@VM_0_7_centos tmp]# cat test I am cai,my qq is
[root@VM_0_7_centos tmp]# awk '{count++;print $0;} END{print "user count is ",count}' /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin cairui:x:500:500::/home/cairui:/bin/bash nginx:x:501:501::/home/nginx:/sbin/nologin mysql:x:502:502::/home/mysql:/sbin/nologin zabbix:x:503:503::/home/zabbix:/sbin/nologin user count is 27
[root@VM_0_7_centos tmp]# ll total 16 -rw-r--r-- 1 root root 12 Mar 21 11:08 1.txt -rw-r--r-- 1 root root 11 Mar 21 11:08 2.txt -rw-r--r-- 1 root root 616 Mar 21 13:44 services -rw-r--r-- 1 root root 18 Mar 22 14:30 test [root@VM_0_7_centos tmp]# ll |awk 'BEGIN{size=0;} {size=size+$5} END{print "[end] size is ",size}' 統計所占的位元組數 [end] size is 657
運算符
[root@VM_0_7_centos tmp]# awk 'BEGIN{a=5;a+=5;print a}' 10
[root@VM_0_7_centos tmp]# awk 'BEGIN{a=1;b=2;print (a>2&&b>1,a=1||b>1)}' 0 1
[root@VM_0_7_centos tmp]# awk 'BEGIN{a=11;if(a>=9){print "ok"}}' ok
[root@VM_0_7_centos tmp]# awk -F ":" 'NF==8{print $0}' ll 列印欄位數為8的行 zabbix:x:503:503::/home/zabbix:/sbin/nologin:ll [root@VM_0_7_centos tmp]# cat ll tcpdump:x:72:72::/:/sbin/nologin cairui:x:500:500::/home/cairui:/bin/bash nginx:x:501:501::/home/nginx:/sbin/nologin mysql:x:502:502::/home/mysql:/sbin/nologin zabbix:x:503:503::/home/zabbix:/sbin/nologin:ll
[root@VM_0_7_centos tmp]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 52:54:00:BE:3B:97 inet addr:172.17.0.7 Bcast:172.17.15.255 Mask:255.255.240.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:10059201 errors:0 dropped:0 overruns:0 frame:0 TX packets:9797173 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1673765442 (1.5 GiB) TX bytes:2246121551 (2.0 GiB) [root@VM_0_7_centos tmp]# ifconfig eth0 | awk -F [" ":]+ 'NR==2{print $4}' NR==2是取第二行 172.17.0.7
正則
[root@VM_0_7_centos tmp]# awk '/root/{print $0}' /etc/passwd 列印所有root行 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
awk中的if
{ if ($1=="foo"){ if($2=="foo"){ print "uno" }else{ print "one" } }elseif($1=="bar"){ print "two" }else{ print "three" } }
awk參考:https://www.cnblogs.com/ginvip/p/6352157.html