※三剑客之awk
下图是awk的执行过程
awk执行过程.png1.取行------>NR==
取第一行
[?root@oldboy /tmp]# awk 'NR==1' lidao.txt
1 2 3 4 5 6 7 8 9 10
取带有oldboy的行
[✡root@oldboy /tmp]# awk '/oldboy/' lidao.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
our size is http://blog.oldboyedu.com
oldboy oldboy oldboy
2.取列------->1第一列2第二列
16952149-aebae7df10a10e9d.png取lidao.txt中的第一列和第三列内容
[✡root@oldboy /tmp]# awk '{print $1,$3}' lidao.txt
I oldboy
I linux.
I badminton
my is
our is
my is
not
my am
oldboy oldboy
给分隔的字符添加内容
用双引号 " "
[✡root@oldboy /tmp]# awk '{print $1"@@##"$3}' lidao.txt
I@@##oldboy
I@@##linux.
@@##
I@@##badminton
my@@##is{}
our@@##is
my@@##is
@@##
not@@##
my@@##am
oldboy@@##oldboy
显示oldboy.txt的第4行的第1列 第2列和第4列
[✡root@oldboy /tmp]# cat lidao.txt|awk 'NR==4'
I like badminton ball,billiard ball and chinese chess!
[✡root@oldboy /tmp]# cat lidao.txt|awk 'NR==4 {print $1,$2,$4}'
I like ball,billiard
找到这行的第一个字符 (,逗号就算一个分隔符)
[✡root@oldboy /tmp]# cat 2.txt
I am lidao,my qq is 918391635
[✡root@oldboy /tmp]# awk -F"," '{print $1}' 2.txt
I am lidao
查看10.0.0.201是第几列(有空格和/)
[✡root@oldboy /tmp]# cat 1.txt
//// 10.0.0.201 /24
[✡root@oldboy /tmp]# awk -F"[ /]+" '{print $2}' 1.txt
10.0.0.201
3.比较 > _ >= _ < _ <= _ == _ !=
找出/etc/passwd下第三列大于999的行
[✡root@oldboy /tmp]# awk -F":" '$3>999' /etc/passwd
oldboy:x:1000:1000::/home/oldboy:/bin/bash
gyj:x:1001:1010::/home/gyj:/bin/bash
显示/etc/passwd中第4列大于0 并且 第4列小于1000的行
[✡root@oldboy /tmp]# 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
....省略
显示使用内存情况内存
[?root@oldboy /tmp]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 2.3G 17G 12% /
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 197M 133M 64M 68% /boot
tmpfs 199M 0 199M 0% /run/user/0
使用率大于百分之1的
[?root@oldboy /tmp]# df -h |awk '$5>1'
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 2.3G 17G 12% /
tmpfs 991M 9.6M 981M 1% /run
/dev/sda1 197M 133M 64M 68% /boot
使用率大于百分之10的
[?root@oldboy /tmp]# df -h |awk '$5+0>10'
/dev/sda3 19G 2.3G 17G 12% /
/dev/sda1 197M 133M 64M 68% /boot
[?root@oldboy /tmp]#
网友评论