Shell中的awk的运用

本文深入探讨了awk工具,包括其内置变量、工作原理和不同版本。详细介绍了awk的基础用法,如打印文件内容和提取特定列。进一步展示了如何使用awk提取不同元素,如IP地址和时间,以及解决实际问题,如统计文件系统类型和单词出现的次数,提取数字等。

一、awk工具

1.1awk内置变量

  • FS:指定每行文本的字段分隔符,缺省为空格或制表位
  • NF:当前处理的行的字段个数
  • NR:当前处理的行的行号(序数)
  • $0:当前处理的行的整行内容
  • $n:当前处理行的第n个字段(第n列)

1.2awk的用法

  • 按行输出文本
  • 按字段输出文本
  • 通过管道、双引号调用Shell命令

1.3awk工作原理

  • 逐行读取文本,默认以空格为分隔符,将分割所得的各个字段保存到内建变量中,并按模式或者条件执行编辑命令

1.4AWK的版本

  • AWK:原先来源于AT & T 实验室的AWK
  • NAWK:New awk, AT & T 实验室的AWK的升级版
  • GAWK:即GUN AWK。所有的GUN/Linux发布版都自带GAWK,它与AWK和NAWK完全兼容
  • GUN AWK 用户手册文档

1.5awk详解

在Linux/UNIX系统中,awk是一个功能强大的编辑工具,逐行读取输入文本,默认以空格或tab键作为分隔符作为分隔,并按模式或者条件执行编辑命令。而awk比较倾向于将一行分成多个字段然后进行处理。AWK信息的读入也是逐行指定的匹配模式进行查找,对符合条件的内容进行格式化输出或者过滤处理,可以在无交互的情况下实现相当复杂的文本操作,被广泛应用于shell脚本,完成各种自动化配置任务。

二、基础用法

[root@localhost ky15]# awk '{print}'
#再打印一遍
aa
aa
bb
bb
cc
cc
[root@localhost ky15]# awk '{print "hello"}'
1
hello
2
hello
3
hello[root@localhost ky15]# awk 'BEGIN {print "hello"}'
#BEGIN比较特殊值打一行
hello

2.1打印挂载点中的第五列的内容

[root@localhost ky15]# df |awk '{print $5}'
已用%
10%
0%
0%
1%
0%
4%
1%
0%

2.2有多行就打印多少hello

[root@localhost ky15]# awk '{print "hello"}' /etc/passwd
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello

2.3打印网卡里面的IP地址

[root@localhost ky15]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.157.101  netmask 255.255.255.0  broadcast 192.168.157.255
        inet6 fe80::771:e554:a619:3bf2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:1b:e2:c6  txqueuelen 1000  (Ethernet)
        RX packets 12173  bytes 890456 (869.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4836  bytes 539129 (526.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        
[root@localhost ky15]# ifconfig ens33|sed -n '2p'
        inet 192.168.157.101  netmask 255.255.255.0  broadcast 192.168.157.255

  • awk sed组合提取
[root@localhost ky15]# ifconfig ens33|sed -n '2p'|awk '{print $2}'
192.168.157.101

  • awk简化提取
[root@localhost opt]# ifconfig ens33 |awk '/netmask/{print $2}'
192.168.157.101

  • sed复杂提取
[root@localhost ~]# ifconfig ens33|sed -rn '2s/[^0-9]+([0-9.]+) .*/\1/p'
192.168.157.101

2.4查看文件所有类容

[root@localhost ky15]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
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

打印第一列的内容

[root@localhost ky15]# cat /etc/passwd|awk -F: '{print $1}'
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
abrt
libstoragemgmt
rpc

打印前两列内容

[root@localhost ky15]# cat /etc/passwd|awk -F: '{print $1,$2}'
root x
bin x
daemon x
adm x
lp x
sync x
shutdown x
halt x
mail x
operator x
games x
ftp x
nobody x
systemd-network x
dbus x
polkitd x
abrt x
libstoragemgmt x
rpc x
colord x

2.5查看挂载点

[root@localhost ky15]# df
文件系统                   1K-块    已用     可用 已用% 挂载点
/dev/mapper/centos-root 52403200 5190952 47212248   10% /
devtmpfs                  917604       0   917604    0% /dev
tmpfs                     933524       0   933524    0% /dev/shm
tmpfs                     933524    9224   924300    1% /run
tmpfs                     933524       0   933524    0% /sys/fs/cgroup
/dev/sda1                5232640  182364  5050276    4% /boot
tmpfs                     186708      12   186696    1% /run/user/42
tmpfs                     186708       0   186708    0% /run/user/0

打印第五列内容

[root@localhost ky15]# df|awk '{print $5}'
已用%
10%
0%
0%
1%
0%
4%
1%
0%

只打印数字

[root@localhost ky15]# df|awk '{print $5}'|awk -F% '{print $1}'
已用
10
0
0
1
0
4
1
0

三、提取不同元素

3.1提取下面的字段中的IP地址和时间

58.87.87.99 - - [09/Jun/2020:03:42:43 +0800] “POST /wp-cron.php? doing_wp_cron=1591645363.2316548824310302734375 HTTP/1.1” ""sendfileon
128.14.209.154 - - [09/Jun/2020:03:42:43 +0800] “GET / HTTP/1.1” ""sendfileon
64.90.40.100 - - [09/Jun/2020:03:43:11 +0800] “GET /wp-login.php HTTP/1.1” ""sendfileo

实操

[root@localhost opt]# cat host.txt |awk -F"[ .]" '{print $2}'
87
2316548824310302734375
14
90


将提取出来的数值再转移到原文件,形成新的模块

[root@localhost opt]# cat host.txt |awk -F"[ .]" '{print $2}' >>host.txt 
[root@localhost opt]# cat host.txt
58.87.87.99 - - [09/Jun/2020:03:42:43 +0800] "POST /wp-cron.php?
doing_wp_cron=1591645363.2316548824310302734375 HTTP/1.1" ""sendfileon
128.14.209.154 - - [09/Jun/2020:03:42:43 +0800] "GET / HTTP/1.1" ""sendfileon
64.90.40.100 - - [09/Jun/2020:03:43:11 +0800] "GET /wp-login.php HTTP/1.1"
""sendfileo
87
2316548824310302734375
14
90


3.2提取全部元素

[root@localhost opt]# awk -F: '{print $0}' /opt/host.txt 
58.87.87.99 - - [09/Jun/2020:03:42:43 +0800] "POST /wp-cron.php?
doing_wp_cron=1591645363.2316548824310302734375 HTTP/1.1" ""sendfileon
128.14.209.154 - - [09/Jun/2020:03:42:43 +0800] "GET / HTTP/1.1" ""sendfileon
64.90.40.100 - - [09/Jun/2020:03:43:11 +0800] "GET /wp-login.php HTTP/1.1"
""sendfileo
87
2316548824310302734375
14
90

3.3提取第一列

[root@localhost opt]# awk -F: '{print $0}' /opt/host.txt 
58.87.87.99 - - [09/Jun/2020:03:42:43 +0800] "POST /wp-cron.php?
doing_wp_cron=1591645363.2316548824310302734375 HTTP/1.1" ""sendfileon
128.14.209.154 - - [09/Jun/2020:03:42:43 +0800] "GET / HTTP/1.1" ""sendfileon
64.90.40.100 - - [09/Jun/2020:03:43:11 +0800] "GET /wp-login.php HTTP/1.1"
""sendfileo
87
2316548824310302734375
14
90

四、解决问题

4.1统计/etc/fstab文件中每个文件系统类型出现的次数

显示文件夹中的信息

[root@localhost opt]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Tue Sep 14 23:05:13 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=1b82584e-a8d3-49a9-9a5a-7b4ae5412e57 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb1  /tp  xfs  defaults  0 0

提取出9-11行的内容

[root@localhost opt]# cat /etc/fstab |sed -n '9,11p'
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=1b82584e-a8d3-49a9-9a5a-7b4ae5412e57 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

[root@localhost opt]# cat /etc/fstab |sed -n '9,11p'|awk '{print $3}'
xfs
xfs
swap

4.2统计/etc/fstab文件中每个单词出现的次数

[root@localhost opt]# egrep -o "\<[[:alpha:]]+\>" /etc/fstab
etc
fstab
Created
by
anaconda
on
Tue
Sep
Accessible
filesystems
by
reference
are
maintained
under
dev
disk
See
man
pages
fstab
findfs
mount
and
or
blkid
for
more
info
dev
mapper
centos
root
xfs
defaults
UUID
boot
xfs
defaults
dev
mapper
centos
swap
swap
swap
defaults
dev
tp
xfs
defaults

4.3提取出字符串Yd$C@M05MB%9&Bdh7dq+YVixp3vpw中所有的数字

[root@localhost opt]# echo "Yd$C@M05MB%9&Bdh7dq+YVixp3vpw"|grep -Eo "[0-9]+"
05
9
7
3

4.4提取主机名并放入原文件

[root@localhost opt]# cat host.txt 
http://mail.kgc.com/index.html
http://www.kgc.com/test.html
http://study.kgc.com/index.html
http://blog.kgc.com/index.html
http://www.kgc.com/images/logo.jpg
http://blog.kgc.com/20080102.html
http://www.kgc.com/images/kgc.jpg

[root@localhost opt]# awk -F"[/.]" '{print $3}' host.txt >>host.txt 
[root@localhost opt]# cat host.txt 
http://mail.kgc.com/index.html
http://www.kgc.com/test.html
http://study.kgc.com/index.html
http://blog.kgc.com/index.html
http://www.kgc.com/images/logo.jpg
http://blog.kgc.com/20080102.html
http://www.kgc.com/images/kgc.jpg

mail
www
study
blog
www
blog
www


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老赵学coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值