Linux——shell练习题之awk

12 篇文章 0 订阅

1、获取根分区剩余大小

[root@localhost ~]# df -h | awk '{print $4}'
Avail
883M
901M
892M
901M
14G
0
836M
181M

2、获取当前机器ip地址

[root@localhost ~]# ifconfig | awk 'NR==2{print $2}'
192.168.15.128

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

[root@localhost httpd]# awk '{print $1}' /var/log/httpd/access_log-20220617 | sort | uniq -c | sort -nr | head -5

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

[root@localhost ~]# awk -F: '$3>500 {print $3}' /etc/passwd
65534
999
998
997
996
995
1000
994
993
1001

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、处理以下文件内容,将域名取出并根据域名进行计数排序处理(百度搜狐面试题) test.txt http://www.baidu.com/index.html http://www.baidu.com/1.html http://post.baidu.com/index.html http://mp3.baidu.com/index.html http://www.baidu.com/3.html http://post.baidu.com/2.html

[root@localhost ex04]# cat test.txt
http://www.baidu.com/index.html
http://www.baidu.com/1.html
http://post.baidu.com/index.html
http://mp3.baidu.com/index.html
http://www.baidu.com/3.html 
http://post.baidu.com/2.html 
[root@localhost ex04]# awk -F '/' '{print $3}' test.txt |sort -nr| uniq -c 
      3 www.baidu.com
      2 post.baidu.com
      1 mp3.baidu.com

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

[root@localhost ~]# awk -F: '{print "用户账号:" $1}' /etc/passwd
用户账号:root
用户账号:bin
用户账号:daemon
用户账号:adm
用户账号:lp
用户账号:sync
用户账号:shutdown
用户账号:halt
用户账号:mail
用户账号:operator

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

[root@localhost ~]# awk -F: '{printf "%-10s %-10s\n",$3, $4}' /etc/passwd

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

[root@localhost ~]# awk 'BEGIN{print "这个是系统用户"}{print $1}END{print "================"}' /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
nobody:x:65534:65534:Kernel
dbus:x:81:81:System
systemd-coredump:x:999:997:systemd
systemd-resolve:x:193:193:systemd
tss:x:59:59:Account
polkitd:x:998:996:User

rpcuser:x:29:29:RPC
================

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

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

11、请将/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

12、请匹配passwd最后一段域bash结尾的信息,有多少条

[root@localhost ex04]# awk -F: '{if($NF~/bash/)print $0}' /etc/passwd |wc -l
24

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

[root@localhost ex04]# awk  '$0~/mail|bash/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
qyx:x:1000:1000:qyx:/home/qyx:/bin/bash
alice:x:1001:1001::/home/alice:/bin/bash
haha01:x:1002:1002::/home/haha01:/bin/bash
haha02:x:1003:1003::/home/haha02:/bin/bash
haha03:x:1004:1004::/home/haha03:/bin/bash
haha04:x:1005:1005::/home/haha04:/bin/bash
haha05:x:1006:1006::/home/haha05:/bin/bash
haha06:x:1007:1007::/home/haha06:/bin/bash
haha07:x:1008:1008::/home/haha07:/bin/bash
haha08:x:1009:1009::/home/haha08:/bin/bash
haha09:x:1010:1010::/home/haha09:/bin/bash
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值