Shell正则表达式(三)

Shell正则表达式(三)

awk工具

awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息
awk处理过程: 依次对每一行进行处理,然后输出
1.awk工作流程
示例:
awk -F ':' '{print $1,$3,$4}' /etc/passwd
在这里插入图片描述
1)awk命令会逐行读取文件的内容进行处理
2)awk以’:’为分隔符,将第1行数据格式化为7段,每段数据存入$1–$7变量中。$0存储这1行数据
3)一行处理完成继续处理下一行,直到此文件读取结束
2.awk常见用法

awk 选项  '模式或条件 { 编辑指令 }' 文件1 文件2 
awk  -f  脚本文件 文件1 文件2 

特殊的内建变量

命令 作用
$0 表示整个当前行
$1 每行第一个字段
NF 字段数量变量
NR 每行的记录号,多文件递增
FNR 与NR类似,不过多文件记录不递增,每个文件都是从1开始
FILENAME 文件名
\t 制表符
\n 换行符
FS BENGIN时定义分隔符
RS 输入的记录分隔符,默认为了换行符
~ 匹配,与==相比不是精确比较
!~ 不匹配,不精确比较
== 等于,必须全部相等,精确比较
&& 逻辑与
|| 逻辑或
+ 匹配时表示1个或1个以上
/[0-9][0-9]+/ 两个或两个以上数字
[0-9][0-9]* 一个或一个以上数字
OFS 输出字段分隔符, 默认也是空格,可以改为制表符等
ORS 输出的记录分隔符,默认为换行符,即处理结果也是一行一行输出到屏幕
-F'[]' 定义多个分隔符

awk用法示例

创建测试文件
[root@localhost ~]# vim test.txt
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the li
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words

#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.

1)按行输出文本
输出所有内容等同于cat test.txt

[root@localhost ~]# awk '{print}' test.txt

[root@localhost ~]# awk '{print $0}' test.txt

在这里插入图片描述
1-3行内容

[root@localhost ~]# awk 'NR==1,NR==3{print}' test.txt

[root@localhost ~]# awk '(NR>=1)&&(NR<=3){print}' test.txt

在这里插入图片描述
输出奇数行%2求模运算,余数为1是奇数,0为偶数

[root@localhost ~]# awk '(NR%2)==1{print}' test.txt

输出偶数行

[root@localhost ~]# awk '(NR%2)==0{print}' test.txt

在这里插入图片描述
输出以root开头的行

[root@localhost ~]# awk '/^root/{print}' /etc/passwd

在这里插入图片描述
输出以bash结尾的行

[root@localhost ~]# awk '/bash$/{print}' /etc/passwd

在这里插入图片描述
统计以/bin/bash结尾的行

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

[root@localhost ~]# grep -c '/bin/bash$' /etc/passwd

在这里插入图片描述
统计以空行分割的文本段落数

扫描二维码关注公众号,回复: 11708286 查看本文章
[root@localhost ~]# awk 'BEGIN{RS=""};END{print NR}' /etc/sysctl.conf

在这里插入图片描述
2)按字段输出文本
输出每行中以空格或制表位分割的第3个字段

[root@localhost ~]# awk '{print $3}' test.txt

在这里插入图片描述
第1,3字段

[root@localhost ~]# awk '{print $1,$3}' test.txt

在这里插入图片描述
密码为空的行

[root@localhost ~]# awk -F ":" '$2==""{print}' /etc/shadow

在这里插入图片描述
输出以‘:’分割,第7字段包含/bash,的行的第1个字段

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

在这里插入图片描述
输出第1个字段包含nfs;并且包含8个字段的行的第1,2字段

[root@localhost ~]# awk '($1~"nfs")&&(NF==8){print $1,$2}' /etc/services

在这里插入图片描述
输出第7个字段不是/bin/bsh也不是/sbin/nologin的行

[root@localhost ~]# awk -F: '($7!="/bin/bsh")&&($7!="/sbin/nologin"){print}' /etc/passwd

在这里插入图片描述
3)通过管道、双引号调用Shell命令
调用wc -l 命令统计使用bash的用户个数

[root@localhost ~]# awk -F: '/bash$/{print | "wc -l"}' /etc/passwd

[root@localhost ~]# grep -c "bash$" /etc/passwd

在这里插入图片描述
调用w命令,统计在线用户数

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

在这里插入图片描述
调用hostname,并输出当前主机名

[root@localhost ~]# awk 'BEGIN {"hostname"|getline;print $0}'

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46902396/article/details/108628203