1
、获取根分区剩余大小
df -h / |tail -1|awk '{print $4}'
2
、获取当前机器
ip
地址
ifconfig ens33|grep "inet "|awk '{print $2}'
3
、统计出
apache
的
access.log
中访问量最多的
5
个
IP
cat /etc/httpd/logs/access_log |awk '{print $1}'|sort|uniq -c
4
、打印
/etc/passwd
中
UID
大于
500
的用户名和
uid
cat /etc/passwd |awk -F : '$3>500 {printf "%-20s %-5s\n", $1,$3}'
5
、
/etc/passwd
中匹配包含
root
或
net
或
ucp
的任意行
cat /etc/passwd|awk '$n~/root|net|ucp/ {print $0}'
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
awk -F '/' '{print $3}' test.txt |sort|uniq -c
7
、请打印出
/etc/passwd
第一个域,并且在第一个域所有的内容前面加上
“
用户帐号:
”
awk -F : '{printf "用户账号:"} {print $1}' /etc/passwd
8
、请打印出
/etc/passwd
第三个域和第四个域
awk -F : 'BEGIN{printf "%-5s %-5s\n","uid","gid"}{printf "%-5s %-5s\n",$3,$4}' /etc/passwd
9
、请打印第一域,并且打印头部信息为:这个是系统用户,打印尾部信息为
:
"================"
awk -F : 'BEGIN{printf "这个是系统用户\n"}{print $1}END{print "==========="}' /etc/passwd
10
、请打印出第一域匹配
daemon
的信息
.
awk -F : '$1~/daemon/ {print $0}' /etc/passwd
11
、请将
/etc/passwd
中的
root
替换成
gongda
,记住是临时替换输出屏幕看到效果即可
.
awk -F: 'gsub(/root/,"gongda")' /etc/passwd
12
、请匹配
passwd
最后一段域
bash
结尾的信息,有多少条
awk -F : '$7~/bash/ {print $0}' /etc/passwd |wc -l
13
、请同时匹配
passwd
文件中,带
mail
或
bash
的关键字的信息
awk '$0~/mail|bash/ {print $0}' /etc/passwd