shell——awk练习题

1、获取根分区剩余大小

[root@localhost ~]# df -h | grep /$ | awk '{print $4}'
31G

2、获取当前机器ip地址

[root@localhost ~]# ifconfig ens160 | grep "inet " | awk '{print $2}'
192.168.190.133

3、统计出apache的access.log中访问量最多的5个IP

[root@localhost ~]# awk '{print $1}' /var/log/httpd/access_log-20221120 | sort | uniq -c | sort -nr | head -5
    213 192.168.190.1
      8 192.168.190.133

4、打印/etc/passwd中UID大于500的用户名和uid

[root@localhost ~]# awk -F: '$3>500 {printf "%-20s%-s\n",$1,$3}' /etc/passwd
nobody              65534
systemd-coredump    999
polkitd             998
geoclue             997
pipewire            996
clevis              995
unbound             994
gluster             993
chrony              992
setroubleshoot      991
saslauth            990
libstoragemgmt      989
dnsmasq             981
sssd                980
cockpit-ws          979
cockpit-wsinstance  978
flatpak             977
colord              976
gnome-initial-setup 975
rhel                1000
myuser              1001
helen               1002
user                1003
sarah               1004
alex                3456
admin               3457
redhat              3458
xiaoming            3459
xiaohei             3460
xiaohong            3461
centos              3462
spark               3463

5、/etc/passwd 中匹配包含root或net或ucp的任意行

[root@localhost ~]# awk '/(root|net|ucp)/ {print $0}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

6、请打印出/etc/passwd 第一个域,并且在第一个域所有的内容前面加上“用户帐号:”

[root@localhost ~]# awk -F: '{print "用户账号:" $1}' /etc/passwd
用户账号:root
用户账号:bin
用户账号:daemon
用户账号:adm
用户账号:lp
用户账号:sync
用户账号:shutdown
用户账号:halt
用户账号:mail
用户账号:operator
用户账号:games
用户账号:ftp
用户账号:nobody
用户账号:dbus
用户账号:systemd-coredump
用户账号:systemd-resolve
用户账号:tss
用户账号:polkitd
用户账号:geoclue
用户账号:rtkit
用户账号:pipewire
用户账号:pulse
用户账号:qemu
用户账号:clevis
用户账号:usbmuxd
用户账号:unbound
用户账号:gluster
用户账号:rpc
用户账号:avahi
用户账号:chrony
用户账号:setroubleshoot
用户账号:saslauth
用户账号:libstoragemgmt
用户账号:dnsmasq
用户账号:radvd
用户账号:sssd
用户账号:cockpit-ws
用户账号:cockpit-wsinstance
用户账号:flatpak
用户账号:colord
用户账号:rpcuser
用户账号:gdm
用户账号:gnome-initial-setup
用户账号:tcpdump
用户账号:sshd
用户账号:rhel
用户账号:apache
用户账号:myuser
用户账号:helen
用户账号:user
用户账号:sarah
用户账号:alex
用户账号:admin
用户账号:redhat
用户账号:named
用户账号:xiaoming
用户账号:xiaohei
用户账号:xiaohong
用户账号:centos
用户账号:spark

7、请打印出/etc/passwd 第三个域和第四个域

[root@localhost ~]# awk -F: '{printf"%-10s%-10s\n",$3,$4}' /etc/passwd
0         0
1         1
2         2
3         4
4         7
5         0
6         0
7         0
8         12
11        0
12        100
14        50
65534     65534
81        81
999       997
193       193
59        59
998       996
997       995
172       172
996       992
171       171
107       107
995       989
113       113
994       988
993       987
32        32
70        70
992       986
991       984
990       76
989       983
981       981
75        75
980       980
979       979
978       978
977       977
976       976
29        29
42        42
975       975
72        72
74        74
1000      1000
48        48
1001      1001
1002      40001
1003      1002
1004      1004
3456      3456
3457      600
3458      3458
25        25
3459      3459
3460      3460
3461      1001
3462      3462
3463      3463

8、请打印第一域,并且打印头部信息为:这个是系统用户,打印尾部信息为:"================"

[root@localhost ~]# awk -F: 'BEGIN {print "这个是系统用户"} {print $1} END {print"================"}' /etc/passwd
这个是系统用户
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
dbus
systemd-coredump
systemd-resolve
tss
polkitd
geoclue
rtkit
pipewire
pulse
qemu
clevis
usbmuxd
unbound
gluster
rpc
avahi
chrony
setroubleshoot
saslauth
libstoragemgmt
dnsmasq
radvd
sssd
cockpit-ws
cockpit-wsinstance
flatpak
colord
rpcuser
gdm
gnome-initial-setup
tcpdump
sshd
rhel
apache
myuser
helen
user
sarah
alex
admin
redhat
named
xiaoming
xiaohei
xiaohong
centos
spark
================

9、请打印出第一域匹配daemon的信息.

[root@localhost ~]# awk -F: '$1=="daemon"' /etc/passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin

10、请将/etc/passwd 中的root替换成gongda,记住是临时替换输出屏幕看到效果即可.

[root@localhost ~]# awk  'gsub(/root/,"gongda")' /etc/passwd
gongda:x:0:0:gongda:/gongda:/bin/bash
operator:x:11:0:operator:/gongda:/sbin/nologin

11、请同时匹配passwd文件中,带mail或bash的关键字的信息

[root@localhost ~]# awk -F: '$0~/mail|bash/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
myuser:x:1001:1001::/home/myuser:/bin/bash
helen:x:1002:40001::/helen:/bin/bash
user:x:1003:1002::/home/user:/bin/bash
alex:x:3456:3456:alian:/home/alex:/bin/bash
admin:x:3457:600:teshu:/home/admin:/bin/bash
redhat:x:3458:3458::/home/redhat:/bin/bash
xiaoming:x:3459:3459::/home/xiaoming:/bin/bash
xiaohei:x:3460:3460::/home/xiaohei:/bin/bash
xiaohong:x:3461:1001::/home/xiaohong:/bin/bash
centos:x:3462:3462::/home/centos:/bin/bash
spark:x:3463:3463::/home/spark:/bin/bash  
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

SUPER COW

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

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

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

打赏作者

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

抵扣说明:

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

余额充值