Linux中Awk常用文本處理命令用法詳解

來源:互聯網
上載者:User

I. NR,FNR記錄當前行

[root@LinuxEA awk]# cat nr1
1 a
2 b
3 c
4 d
5 e
[root@LinuxEA awk]# awk '{print FNR}' nr1
1
2
3
4
5
6
[root@LinuxEA awk]# awk '{print NR}' nr1
1
2
3
4
5
6
[root@LinuxEA awk]# awk '{print "行. = " NR " 資料 = " RNR,$0}' nr1
行. = 1 資料 =  1 a
行. = 2 資料 =  2 b
行. = 3 資料 =  3 c
行. = 4 資料 =  4 d
行. = 5 資料 =  5 e
行. = 6 資料 = 
[root@LinuxEA awk]#
II. AWK引入變數:

[root@LinuxEA awk]# VAR=linuxea
[root@LinuxEA awk]# awk -v var=$VAR 'BEGIN{print var}'
linuxea
[root@LinuxEA awk]# awk 'BEGIN{print "'$VAR'"}'
linuxea
[root@LinuxEA awk]#
III. OFS輸出欄位分隔符號

[root@LinuxEA awk]# echo 'linuxea.com linuxea.com linuxea.com' |awk -v OFS="---" '{$1=$1;print $0}'
linuxea.com---linuxea.com---linuxea.com
[root@LinuxEA awk]#

[root@LinuxEA awk]# echo 'linuxea.com linuxea.com linuxea.com' |awk -v OFS="---" '{NF++0;print $0}'
linuxea.com---linuxea.com---linuxea.com---
[root@LinuxEA awk]# echo 'linuxea.com linuxea.com linuxea.com' |awk -v OFS="---" '{NF+=0;print $0}'
linuxea.com---linuxea.com---linuxea.com
[root@LinuxEA awk]#
IV. awk實現點分十進位的ip地址轉換數字格式

[root@LinuxEA awk]# echo 8.8.8.8 |awk -F. '{n=0;for(i=1;i<=NF;i++){n*=256;n+=$i}print n}'
134744072
[root@LinuxEA awk]#
[root@LinuxEA awk]# echo 8.8.8.8|awk -F. '{printf "%d\n", ($1*(2^24))+($2*(2^16))+($3*(2^8))+$4}'
134744072
[root@LinuxEA awk]#
[root@LinuxEA awk]# ping 134744072
PING 134744072 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=44 time=66.3 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=44 time=76.9 ms
[root@LinuxEA awk]# echo 00001000.00001000.00001000.00001000|awk -F. 'function bin2dec(a,b,i,c){b=length(a);c=0;for(i=1;i<=b;i++){c+=c;if(substr(a,i,1)=="1")c++}return c}{for(j=1;j<=NF;j++)printf("%d%s",bin2dec($j),j!=NF?".":"\n")}'
8.8.8.8
[root@LinuxEA awk]#
V. 選擇性列印

VI. 取其前一行

源檔案:

[root@LinuxEA awk]# cat nr1
1 a
2 b
3 c
4 d
5 e
當pattern等於5時,列印前一行

[root@LinuxEA awk]# awk '!/5/{a=$0}/5/{print a}' nr1
4 d
[root@LinuxEA awk]# awk '/5/{print i};i=$0{}'
4 d
如果取前2行則藉助一下屬組,以前兩行為例

[root@LinuxEA awk]# awk '{a[NR]=$0}/5/{p=NR}END{for(i=p-2;i<p;i++)print a[i] RS}' nr1
3 c

4 d
VII. 如果取其後兩行

已知從3行開始,從3到5行內容

[root@LinuxEA awk]# awk '/3/{p=1;a=NR}p&&NR-x<=5&&NR-x>0' nr1
3 c
4 d
5 e
[root@LinuxEA awk]#
VIII. 取出現匹配次數

來源資料:

[root@LinuxEA awk]# cat nr1
1 a
2 b
3 c
2 b
4 d
5 e
6 f
7 g

[root@LinuxEA awk]#
IX. 出現第一次匹配取後一行

[root@LinuxEA awk]# awk  '/2 b/{a++;b=NR}a==1&&NRb<=1 && NR-x>0' nr1
2 b
3 c
X. 出現第二次匹配,列印下面的行

[root@LinuxEA awk]# awk  '/2 b/{a++;b=NR}a==2&&NRb<=2 && NR-b>0' nr1
4 d
5 e
6 f
7 g

[root@LinuxEA awk]#
XI. 匹配中間資料

取出1 a 到4 d中間的資料

包括匹配行

[root@LinuxEA awk]# awk '/1 a/{p=1}/4 d/{print;p=0}p' nr1
1 a
2 b
3 c
2 b
4 d
[root@LinuxEA awk]#
不包括1 a行

[root@LinuxEA awk]# awk '/1 a/{p=1;next}/4 d/{print;p=0}p' nr1
2 b
3 c
2 b
4 d
[root@LinuxEA awk]#
不包括4 d行

[root@LinuxEA awk]# awk '/1 a/{p=1}/4 d/{p=0}p' nr1
1 a
2 b
3 c
2 b
[root@LinuxEA awk]#
不列印匹配行

[root@LinuxEA awk]# awk '/1 a/{p=1;next}/4 d/{p=0}p' nr1
2 b
3 c
2 b
[root@LinuxEA awk]#
XII. 過濾關鍵字中間的行

保留123(linuxea)和xaca(linuxea)中含有關鍵字的資料

執行個體檔案

[root@LinuxEA awk]# cat file289
aaa
bbb
ccs
ddw
weq
123(linuxea)
szxc
sdaq
AZCA

SDQWQ
XXCA
ASDQ
XACA(linuxea)
BBB
[root@LinuxEA awk]#
保留123(linuxea)和xaca(linuxea)中含有關鍵字的資料

第一種方式:

將標籤P作為資料下表,將123到結尾資料存入數組中,最後列印匹配linuxea值

[root@LinuxEA awk]# awk '/123/{p++}/./{a[p]=a[p]"\n"$0}END{for(i=1;i<=p;i++) if(a[i]~/\(linuxea\)/) print a[i]}' file289

123(linuxea)
szxc
sdaq
AZCA
SDQWQ
XXCA
ASDQ
XACA(linuxea)
BBB
[root@LinuxEA awk]#
第二種方式:

利用awk內建函數RS

[root@LinuxEA awk]# awk -v RS="123" '/linuxea/{print RS $0}' file289
123(linuxea)
szxc
sdaq
AZCA

SDQWQ
XXCA
ASDQ
XACA(linuxea)
BBB

[root@LinuxEA awk]#
XIII. 統計關鍵字的行

[root@LinuxEA awk]# cat file299
head no
1235 linuxea mark 1 @09    linuxea.com
1235 mark
head no
1234 mark linuxea.com
1235 mark
1235 mark linuxea.com
head no
1235 mark linuxea.com
1235 mark
1235 mark linuxea.com
1235 mark linuxea.com
統計linuxea.com的行數

[root@LinuxEA awk]# awk '/no/{p++;b[p]=$0}/linuxea.com/{a[p]++}END{for(i=1;i<=p;i++) print b[i],a[i]}' file299
head no 1
head no 2
head no 3
[root@LinuxEA awk]#
XIV. 列印奇數偶數行數

[root@LinuxEA awk]# cat file2991
1
2
3
4
5
6
7
8
9
10
[root@LinuxEA awk]# awk p++%2 file2991
2
4
6
8
10
[root@LinuxEA awk]# awk ++p%2 file2991
1
3
5
7
9
## 列印匹配關鍵字的行號
linuxea.com

[root@LinuxEA awk]# cat 1006
123
abc
aaaa
vwd
vvvvvv
deafwq
linuxea.com
dddddddddd
aaaaaaa
列印關鍵字vwd後出現的第一個linuxea.com所處行的行號

[root@LinuxEA awk]# awk '/linuxea.com/{p=NR}/vwd/{x=NR}p>x&&x>0{print p;exit}' 1006
7
[root@LinuxEA awk]#
XV. 取其關鍵字後N行

[root@LinuxEA awk]# awk '/vwd/{i=3;next}--i>=0' 1006
vvvvvv
deafwq
linuxea.com
[root@LinuxEA awk]#
取其關鍵字前三行

[root@LinuxEA awk]# awk '/\<vwd\>/{for(i=NR-3;i<NR;i++)print a[i%3];exit}{a[NR%3]=$0}' 1006
123
abc
aaaa
[root@LinuxEA awk]# awk '{a[NR]=$0}/\<vwd\>/{for(i=NR-3;i<NR;i++)print a[i]}' 1006 
123
abc
aaaa
[root@LinuxEA awk]# awk '{a[NR]=$0}/vwd/{p=NR}END{for(i=p-3;i<p;i++) printf a[i] RS}' 1006
123
abc
aaaa
[root@LinuxEA awk]# awk '{a[NR]=$0}/vwd/{for(i=NR-3;i<NR;i++)print a[i]}' 1006
123
abc
aaaa
[root@LinuxEA awk]#

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.