awk

AWK

[root@liuzan ~]# awk -F ':''OFS=":" {print $3,$1}' 1.txt awk常用用法

0:root

1:bin

2:daemon

3:adm

4:lp

 

Awk ‘/user|root/’ 1.txt 匹配userroot

Awk ‘/r*o/’ 1.txt 匹配0个或多个

Awk ‘/r?o/’ 1.txt  匹配0个或1

Awk ‘/r+o/’ 1.txt 匹配一个或多个

root@liuzan ~]# awk '/(oo)/' 1.txt 匹配00的行

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

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

sync:x:5:0:sync:/sbin:/bin/syooooooooooooooooonc

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

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

 

awk -F ':' '$1~/r*o/' 1.txt 匹配第一段中符合r*o的行

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

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

nobody:x:99:99:Nobody:/:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

 

[root@liuzan ~]# awk -F ':' '$1~/r*o/{print $1,$3}; $1~/user/ {print $1,$3}' 1.txt 匹配第一段中有r*o的行和有user的行,2次过滤

root 0

daemon 2

shutdown 6

operator 11

gopher 13

nobody 99

postfix 89

user3 506

 

awk -F ":" '$1~/r*o|nobody/{print $1,$3}' 1.txt 匹配r*onobody的行一次过滤

root 0

daemon 2

shutdown 6

operator 11

gopher 13

nobody 99

postfix 89


你可能感兴趣的:(awk)