day19三劍客之a(chǎn)wk
sed反向引用補(bǔ)充
三劍客之a(chǎn)wk
格式:awk '找誰{干啥}'
執(zhí)行過程
取行與取列
取行:awk 'NR==1' lidao.txt
[root@cuihua~]# cat lidao.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
110,lidao,COCO
[root@cuihua ~]# awk 'NR==1' lidao.txt
101,oldboy,CEO
取列:awk '{print 1,2}' oldboy.txt
[root@cuihua~]# cat oldboy.txt
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our size is http://blog.oldboyedu.com
my qq is 49000448
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
ASAaSAsaZZZ
abcsadaabc
[root@cuihua ~]# awk '{print $1,$2}' oldboy.txt
I am
I teach
I like
my blog
our size
my qq
not 4900000448.
my god
ASAaSAsaZZZ
abcsadaabc
取列設(shè)置分隔符
單個(gè):awk -F "," '{print $3,$7}' /oldboy/fs.txt
[root@cuihua /oldboy]# cat fs.txt
I am lidao,my qq is 918391635
[root@cuihua /oldboy]# awk -F "," '{print $1,$7}' fs.txt
I am lidao
多個(gè):awk -F "[, ]" '{print $3,$7}' /oldboy/fs.txt
[root@cuihua /oldboy]# cat fs.txt
I am lidao,my qq is 918391635
[root@cuihua /oldboy]# awk -F "[, ]" '{print $3,$7}' /oldboy/fs.txt
lidao 918391635
取行取列并且設(shè)置分隔符并且行首有相同的字符
ip a s eth0 | awk -F "[/ ]+" 'NR==3 {print $3}'
[root@cuihua ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:85:96:73 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.201/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::3422:abf1:b8b1:db26/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@cuihua ~]# ip a s eth0 | awk -F "[/ ]+" 'NR==3 {print $3}'
10.0.0.201
過濾 $NF表示最后一列
比大小 簡(jiǎn)單的比大小不用顯示內(nèi)容直接$3就好了
awk -F ":" '$3>999' /etc/passwd
[root@cuihua ~]# awk -F ":" '$3>999' /etc/passwd
oldboy:x:1000:1000::/home/oldboy:/bin/bash
kangxu:x:1001:1001::/home/kangxu:/bin/bash
boy:x:1002:1002::/home/boy:/bin/bash
girl:x:1003:1003::/home/girl:/bin/bash
oldgirl:x:1004:1004::/home/oldgirl:/bin/bash
過濾范圍 awk -F ":" '$4>999&&$<0' /etc/passwd
[root@cuihua ~]# awk -F ":" '$4>0&&$4<1000' /etc/passwd
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin