awk

[root@abinlinux ~]# awk -F ':' '{print $3}' 1.txt   第三段    

0

1

2

[root@abinlinux ~]# awk -F ':' '{print $3,$4}' 1.txt     三段   四段    截取段  

0 0

1 1

2 2

[root@abinlinux ~]# awk -F ':' 'OFS=":" {print $3,$4}' 1.txt  

OFS=":"相當於 -F   指定分隔符   以:號分開  同時可以使用 #&! 也可以用做分開

0:0

1:1

2:2

3:4

4:7

[root@abinlinux ~]# awk -F ':' 'OFS="#" {print $3,$4,$1}' 1.txt    

$3 第三段  $4 第四段 $1第一段  

0#0#root

1#1#bin

2#2#daemon

3#4#adm

[root@abinlinux ~]# awk '/yun/' 1.txt    awk 匹配 用法

yun:x:500:500::/home/yun:/bin/bash

[root@abinlinux ~]# awk '/yun|root/' 1.txt     也可以匹配多個 用| 他表示  用法  awk可以直接用|

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

yun:x:500:500::/home/yun:/bin/bash

[root@abinlinux ~]# awk '/r*o/' 1.txt   可以匹配*號前面的任意一個  

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

[root@abinlinux ~]# awk '/r?o/' 1.txt   匹配問號前面 的一個或者多個  

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

[root@abinlinux ~]# awk '/r+o/' 1.txt   匹配一個或多個加好前面的字符  

root:x:0:0:root:/root:/bin/bash

mail:x:8:12:mail:/var/spoorrrrrrrrol/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

gopher:x:13:30:gopher:/var/goproher:/sbin/nologin

[root@abinlinux ~]# awk '/r.*o/' 1.txt     這叫貪婪匹配  

root:x:0:0:root:/root:/bin/bash

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologi

[root@abinlinux ~]# awk '/(oo)+/' 1.txt    一個是可以的多個也是可以的

rooooooooot:x:0:0:root:/root:/bin/bash

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[root@abinlinux ~]# awk '/(ooo)+/' 1.txt      三個O只有 這些         awk不支持{}

rooooooooot:x:0:0:root:/root:/bin/bash

halt:x:7:0:halt:/sbin:/rooooo*osbin/halt

[root@abinlinux ~]# awk -F ':' '$1~/r*o/' 1.txt    匹配第一段帶ro的 或者多個  

rooooooooot:x:0:0:root:/root:/bin/bash

daemon:x:2:2:daemon:/sbin:/sbin/nologin

shutdown:x:6:0:shutdowr.on:/sbin:/sbin/shutdown

operator:x:11:0:operator:/root:/sbin/nologin

[root@abinlinux ~]# awk -F ':' '$1~/r*o/ {print $4}' 1.txt     匹配過程中單獨的顯示第幾段

                                                                                               精準匹配精準打印  

0

2

0

0

30

99

89

26

[root@abinlinux ~]# awk -F ':' '$1~/r*o/ {print $1,$3}; $1~/nobody/ {print $1,$3}' 1.txt

兩個規則的匹配

rooooooooot 0

daemon 2

shutdown 6

operator 11

gopher 13

nobody 99

nobody 99

postfix 89

postgres 26

[root@abinlinux ~]# awk -F ':' '$1~/r*o|nobody/ {print $1,$3}' 1.txt

'$1~/r*o|nobody/  匹配第一段 r*o 或者nobody      ;{print $1,$3}'  匹配第一和第三段  打印

                        一個規則的匹配  

rooooooooot 0

daemon 2

shutdown 6

operator 11

gopher 13

nobody 99

postfix 89

postgres 26


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章