标签:awk
一、awk的优点和缺点:
优点:1.awk用来取列,单独取列简单方便
2.awk支持变量
3.awk支持&&和||
4.awk支持流程控制语句
5.awk支持循环
6.awk支持函数
缺点:awk功能强大,相对来说较复杂
二、awk的命令格式:
awk ‘条件1 {动作1} 条件2 {动作2}...‘ file.txt
三、awk的用法:
打印匹配root的行
[root@localhost ~]# awk ‘/root/‘ /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
2.-F选项的作用
(a)以冒号分隔,打印第一列数据;不加-F选项,默认是空格,冒号可以自定义
[root@localhost ~]# awk -F: ‘{print $1,$3,$5,$7}‘ /etc/passwd
root 0 root /bin/bash
bin 1 bin /sbin/nologin
daemon 2 daemon /sbin/nologin
...
(b)-F自定义分隔符,分隔数据
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
57G 7.2G 47G 14% /
/dev/sda1 99M 12M 82M 13% /boot
tmpfs 252M 0 252M 0% /dev/shm
[root@localhost ~]# df -h |grep /$ |awk ‘{print $4}‘ |awk -F"%" ‘{print $1}‘
14
cut命令同样实现
[root@localhost ~]# df -h |grep /$ |awk ‘{print $4}‘ |cut -d"%" -f 1
14
(c)打印列的时候,逗号来分隔列
[root@localhost ~]# awk -F: ‘/root/{print $1 $3 $5 $7}‘ /etc/passwd
root0root/bin/bash
operator11operator/sbin/nologin
[root@localhost ~]# awk -F: ‘/root/{print $1,$3,$5,$7}‘ /etc/passwd
root 0 root /bin/bash
operator 11 operator /sbin/nologin
(d)打印列的时候,#来分隔列
[root@localhost ~]# awk -F: ‘/root/ {print $1"###"$7"###"$3}‘ /etc/passwd
root###/bin/bash###0
operator###/sbin/nologin###1
3.BEGIN和END的用法
(a)BEGIN的用法
[root@localhost ~]# df -h |grep /$ |awk ‘{print $4}‘ |awk -F"%" ‘BEGIN{print "root_usage!"} {print $1}‘
root_usage!
14
(b)BEGIN和END一起用,\n用来换行
[root@localhost ~]# df -h |grep /$ |awk ‘{print $4}‘ |awk -F"%" ‘BEGIN{print "\nroot_usage!" "\n==========="} {print $1} END{print"==========="}‘
root_usage!
===========
14
===========
4.FS、OFS、NR、NF的用法
(a)FS等同于-F,以冒号分隔列
[root@localhost ~]# awk ‘BEGIN{FS=":"} /root/ {print $1,$3,$5,$7}‘ /etc/passwd
root 0 root /bin/bash
operator 11 operator /sbin/nologin
(b)OFS改变默认分隔符,用#号分隔列
[root@localhost ~]# awk -F: ‘BEGIN{OFS="###"} /root/ {print $1,$3,$5,$7}‘ /etc/passwd
root###0###root###/bin/bash
operator###11###operator###/sbin/nologin
(c)分号的用法
[root@localhost ~]# awk ‘BEGIN {FS=":";OFS="#"} /root/ {print $1,$7}‘ /etc/passwd
root#/bin/bash
operator#/sbin/nologin
(c)NR用来打印行号,而且列可以自定义位置打印
[root@localhost ~]# awk -F: ‘/root/ {print NR,$1,$7,$5,$3}‘ /etc/passwd
1 root /bin/bash root 0
12 operator /sbin/nologin operator 11
[root@localhost ~]# awk ‘/root/ {print NR,$0}‘ /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
12 operator:x:11:0:operator:/root:/sbin/nologin
(d)打印空行
[root@localhost ~]#awk ‘/^$/{print NR}‘ /etc/passwd
(e)这两个命令等效,但grep的效率更高
[root@localhost ~]# awk ‘/root/ {print NR,$0}‘ /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
12 operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# grep -n ‘root‘ /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
12:operator:x:11:0:operator:/root:/sbin/nologin
(f)NF用来打印列数,NF打印出有7列,$NF打印出最后一列
[root@localhost ~]# awk -F: ‘/root/ {print NR,NF,$NF,$0}‘ /etc/passwd
1 7 /bin/bash root:x:0:0:root:/root:/bin/bash
12 7 /sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin
5.awk中的比较
<
<=
>
>=
== 等于
!=不等于
~匹配
!~不匹配
打印大于等于37行的第一列
[root@localhost ~]# awk -F: ‘NR>=37 {print NR,$1}‘ /etc/passwd
37 xfs
38 gdm
39 pegasus
40 oracle
[root@localhost shell]#cat awk.txt
1 80 1 li
2 60 2 wang
3 70 3 zhang
4 10 4 zhao
[root@localhost shell]# awk ‘$2>=60 {print $4}‘ awk.txt
li
wang
zhang
打印第一列匹配root的字段
[root@localhost ~]# awk -F: ‘$1=="root" {print $1,$3,$5,$7}‘ /etc/passwd
root 0 root /bin/bash
打印全文匹配root的字段
[root@localhost ~]# awk -F: ‘$0~/root/ {print NR,$1,$7,$5,$3}‘ /etc/passwd
1 root /bin/bash root 0
12 operator /sbin/nologin operator 11
打印第六列匹配root的字段
[root@localhost ~]# awk -F: ‘$6 ~/root/{print $1,$5,$6}‘ /etc/passwd
root root /root
operator operator /root
6.if判断
[root@localhost ~]# awk -F: ‘{if($1=="root")print $0}‘ /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# cat /etc/passwd |awk ‘{if($1 ~ "oracle") print NR,$0}‘
40 oracle:x:500:501::/home/oracle:/bin/bash
[root@localhost ~]# awk -F: ‘{if($NF~/bash/) print NR,$1,$NF}‘ /etc/passwd
1 root /bin/bash
31 postgres /bin/bash
35 mysql /bin/bash
40 oracle /bin/bash
本文出自 “卡卡西” 博客,请务必保留此出处http://whnba.blog.51cto.com/1215711/1610643
标签:awk
原文地址:http://whnba.blog.51cto.com/1215711/1610643