课堂笔记Day19-59期

day19三剑客之awk

sed反向引用补充

课堂笔记Day19-59期_第1张图片
sed后向引用

三剑客之awk

格式:awk '找谁{干啥}'

执行过程

课堂笔记Day19-59期_第2张图片
awk执行过程

取行与取列

取行: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

取列设置分隔符
单个: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

多个: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

取行取列并且设置分隔符并且行首有相同的字符
ip a s eth0 | awk -F "[/ ]+" 'NR==3 {print $3}'

[root@cuihua ~]# ip a s eth0
2: eth0:  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表示最后一列
比大小 简单的比大小不用显示内容直接$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

你可能感兴趣的:(课堂笔记Day19-59期)