Shell编程之正则表达式(三)

发布时间:2020-06-14 12:31:47 作者:SiceLc
来源:网络 阅读:3321

awk工具

Linux/UNIX 系统中,awk 是一个功能强大的编辑工具,逐行读取输入文本,并根据指定的匹配模式进行查找,对符合条件的内容进行格式化输出或者过滤处理,可以在无交互的情况下实现相当复杂的文本操作,被广泛应用于 Shell 脚本,完成各种自动化配置任务。

1. awk常见用法

内建变量 说明
FS 指定每行文本的字段分隔符,默认为空格或制表位
NF 当前处理的行的字段个数
NR 当前处理的行的行号(序数)
$0 当前处理的行的整行内容。
$n 当前处理行的第 n 个字段(第 n 列)
FILENAME 被处理的文件名
RS 数据记录分隔,默认为\n,即每行为一条记录

2.用法示例

1)按行输出文本

[root@localhost opt]# awk '{print}' bbb.txt    //输出所有内容,等同于 cat bbb.txt
this is
the wood
wood
wod
the wood
this is test
[root@localhost opt]# awk '{print $0}' bbb.txt       //输出所有内容,等同于 cat bbb.txt
this is
the wood
wood
wod
the wood
this is tes
[root@localhost opt]# awk 'NR==1,NR==3{print}' bbb.txt    //输出第 1~3 行内容
this is
the wood
wood
[root@localhost opt]# awk '(NR>=1)&&(NR<=3){print}' bbb.txt   //输出第 1~3 行内容
this is
the wood
wood
[root@localhost opt]# awk 'NR==1||NR==3{print}' bbb.txt   //输出第 1 行、第 3 行内容
this is
wood
[root@localhost opt]# awk '(NR%2)==1{print}' bbb.txt     //输出所有奇数行的内容
this is
wood
the wood
[root@localhost opt]# awk '(NR%2)==0{print}' bbb.txt    //输出所有偶数行的内容
the wood
wod
this is test
[root@localhost opt]# awk '/^the/{print}' bbb.txt       //输出所有以the开头的行
the wood
the wood
[root@localhost opt]# awk '/wood$/{print}' bbb.txt      //输出所有以wood结尾的行
the wood
wood
the wood
[root@localhost opt]# awk 'BEGIN {x=0};/\/bin\/bash$/{x++};END {print x}' /etc/passwd
2                           //统计以/bin/bash 结尾的行数,等同于 grep -c "/bin/bash$" /etc/passwd
[root@localhost opt]# awk 'BEGIN {RS=""};END{print NR}' httpd.txt   //统计以空行分隔的文本段落数
38

2)按字段输出文本

[root@localhost opt]# vim bbb.txt     //编辑文本内容
this is txt
the wood aaa
wood is bbb                      //编辑添加内容
wod is ccc
the wood AAA
this is test
~                                                                            
~                                                                                                 
:wq                              //保存退出
[root@localhost opt]# awk '{print $3}' bbb.txt          //输出每行中(以空格或制表位分隔)的第 3 个字段
txt
aaa
bbb
ccc
AAA
test
[root@localhost opt]# awk '{print $1,$3}' bbb.txt       //输出每行中的第 1、3 个字段
this txt
the aaa
wood bbb
wod ccc
the AAA
this test
[root@localhost opt]# awk -F ":" '$2=="*"{print}' /etc/shadow  //输出密码为“*”的用户的shadow记录
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::
games:*:17110:0:99999:7:::
ftp:*:17110:0:99999:7:::
nobody:*:17110:0:99999:7:::
[root@localhost opt]# awk 'BEGIN{FS=":"};$2=="*";END{print}' /etc/shadow 
bin:*:17110:0:99999:7:::                        //输出密码为“*”的用户的shadow记录
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::
games:*:17110:0:99999:7:::
ftp:*:17110:0:99999:7:::
nobody:*:17110:0:99999:7:::
named:!!:18178::::::
[root@localhost opt]# awk -F ":" '$7~"/bash"{print $1}' /etc/passwd
root                               //输出以冒号分隔且第 7 个字段中包含/bash 的行的第 1 个字段 
sun
[root@localhost opt]# awk '($1~"nfs")&&(NF==8){print $1,$2}' /etc/services 
nfs 2049/tcp                          //输出包含 8 个字段且第 1 个字段中包含 nfs 的行的第 1、2 个字段
nfs 2049/udp
nfs 2049/sctp
netconfsoaphttp 832/tcp
netconfsoaphttp 832/udp
netconfsoapbeep 833/tcp
netconfsoapbeep 833/udp
[root@localhost opt]# awk -F ":" '($7!="/bin/bash")&&($7!="/sbin/nologin"){print}' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync         //输出第 7 个字段既不为/bin/bash 也不为/sbin/nologin 的所有行 
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

3)通过管道、双引号调用 Shell 命令

[root@localhost opt]# awk -F : '/bash/{print | "wc -l"}' /etc/passwd
2                    //调用wc -l 命令统计使用bash 的用户个数,等同于 grep -c "bash$" /etc/passwd
[root@localhost opt]# awk 'BEGIN {while ("w" | getline) n++ ; {print n-2}}'
1     //调用w 命令,并用来统计在线用户数,使用while循环匹配w 命令输出行数,getline为只显示行数,n-2为减去前两行
[root@localhost opt]# awk 'BEGIN { "hostname" | getline ; print $0}'  
localhost.localdomain           //调用hostname,并输出当前的主机名
推荐阅读:
  1. 四天精通shell编程(三)
  2. shell编程之正则表达式(三)awk工具

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

shell linux awk he hell

上一篇:linux命令——cp

下一篇:关于使用PopupWindow

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》