shell正则表达式—— awk命令

awk概述

awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大。简单来说awk就是把文件逐行的读入,默认以空格为分隔符将每行切片,切开的部分再进行各种分析处理。 awk是行处理器,相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息。

在使用 awk 命令的过程中,可以使用逻辑操作符“&&”表示“与”、“||” 表示“或”、“!”表示“非”;还可以进行简单的数学运算,如+、-、*、/、%、^分别表示加、减、乘、除、取余和乘方。

awk使用

ask内建变量

awk 包含几个特殊的内建变量(可直接用)如下所示:
FS:指定每行文本的字段分隔符,默认为空格或制表位。
NF:当前处理的行的字段个数。
NR:当前处理的行的行号(序数)。
$0:当前处理的行的整行内容。
$n:当前处理行的第 n 个字段(第 n 列)。
FILENAME:被处理的文件名
RS:数据记录分隔,默认为\n,即每行为一条记录。

根据要出输出内容

输出全部内容

输出文件内容,和cat差不多

[root@1centos /]# awk '{print}' /zhengzebiaodashi/the.txt 
1thethethe
2thethethe
3thethethethethe
4thethethethethethethe
5thethethethe
6hethethe
7thethethe
8thethethethethe
9thethethethethethethe
10thethethethe

$0也是全部内容
[root@1centos /]# awk '{print $0}' /zhengzebiaodashi/the.txt 
1thethethe
2thethethe
3thethethethethe
4thethethethethethethe
5thethethethe
6hethethe
7thethethe
8thethethethethe
9thethethethethethethe
10thethethethe

输出 1-3 行内容

[root@1centos zhengzebiaodashi]# awk 'NR==1,NR==3{print}' the.txt 
1thethethe
2thethethe
3thethethethethe

输出第 1 行和第 3 行内容

[root@1centos zhengzebiaodashi]# awk 'NR==1||NR==3{print}' the.txt 
1thethethe
3thethethethethe

输出奇数行

[root@1centos zhengzebiaodashi]# awk '(NR%2!=0){print}' the.txt
1thethethe
3thethethethethe
5thethethethe
7thethethe
9thethethethethethethe

输出偶数行

[root@1centos zhengzebiaodashi]# awk '(NR%2==0){print}' the.txt
2thethethe
4thethethethethethethe
6hethethe
8thethethethethe
10thethethethe

输出 root 开头的行

这边直接对 /etc/passwd 进行筛查吧

[root@1centos zhengzebiaodashi]# awk '/^root/{print}' /etc/passwd
root:x:0:0:root:/root:/bin/bash

输出不能登录的行

[root@1centos zhengzebiaodashi]# awk '/nologin$/{print}' /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
operator:x:11:0:operator:/root:/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

统计以 /bin/bash 结尾的行的数量

//bin/bash \转义

[root@1centos zhengzebiaodashi]# awk 'BEGIN{x=0};/\/bin\/bash$/{x++};END{print x}' /etc/passwd
4

按字段输入文本

稍加修改下上一次用的 the.txt 方便这次观察

[root@1centos zhengzebiaodashi]# vim the.txt 
1the 2the 3the
2the 2the 3the
3the 2the 3the 4the 5the
4hello hi the word world
5 2 3 4 5 6 7 8

以空格为分割第三个字段

[root@1centos zhengzebiaodashi]# awk '{print $3}' the.txt 
3the
3the
3the
the
3

以空格为分割第一/三个字段

[root@1centos zhengzebiaodashi]# awk '{print $1,$3}' the.txt 
1the 3the
2the 3the
3the 3the
4hello the
5 3

输出以冒号分隔且第 7 个字段中包含/bash 的行的第 1 个字段

[root@1centos zhengzebiaodashi]# awk -F: '$7~"/bash"{print $1}' /etc/passwd
root
postgres
tom
jerry
kongkong

输出第一个字段是root的第七个字段

[root@1centos zhengzebiaodashi]# awk -F: '$1~"root"{print $7}' /etc/passwd
/bin/bash

输出第 7 个字段既不为/bin/bash 也不为/sbin/nologin 的所有行

[root@1centos zhengzebiaodashi]# awk -F: '($7!="/bin/bash")&&($7!="/sbin/nologin"){print}' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
named:x:25:25:Named:/var/named:/bin/false

统计末尾是 /bash 的用户个数

[root@1centos zhengzebiaodashi]# awk -F: '/bash$/{print |"wc -l"}' /etc/passwd
5

[root@1centos zhengzebiaodashi]# egrep -c "bash$" /etc/passwd
5

调用命令

统计在线用户个数

[root@1centos zhengzebiaodashi]# awk 'BEGIN {while("w" |getline)n++;{print n-2}}'
1

调用hostname,并输出当前的主机名

[root@1centos zhengzebiaodashi]# awk 'BEGIN{"hostname" |getline;print $0}'
1centos

猜你喜欢

转载自blog.csdn.net/Ora_G/article/details/107587040