1. 程式人生 > 其它 >shell程式設計之awk(第八次作業)

shell程式設計之awk(第八次作業)

技術標籤:linux運維shelllinuxshell運維sshapache

shell程式設計之awk

1、獲取根分割槽剩餘大小
2、獲取當前機器ip地址
3、統計出apache的access.log中訪問量最多的5個IP
4、列印/etc/passwd中UID大於500的使用者名稱和uid
5、/etc/passwd 中匹配包含root或net或ucp的任意行
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
7、請打印出/etc/passwd 第一個域,並且在第一個域所有的內容前面加上“使用者帳號:”
8、請打印出/etc/passwd 第三個域和第四個域
9、請列印第一域,並且列印頭部資訊為:這個是系統使用者,列印尾部資訊為:"================"
10、請打印出第一域匹配daemon的資訊.
11、請將/etc/passwd 中的root替換成gongda,記住是臨時替換輸出螢幕看到效果即可.
12、請匹配passwd最後一段域bash結尾的資訊,有多少條
13、請同時匹配passwd檔案中,帶mail或bash的關鍵字的資訊

1、獲取根分割槽剩餘大小

[[email protected] ~]# df -h / | tail -1 | awk '{print $4}'
6.4G

2、獲取當前機器ip地址

[[email protected] ~]# ifconfig eno16777736 | grep "inet " | awk '{print $2}'
192.168.150.140

3、統計出apache的access.log中訪問量最多的5個IP

4、列印/etc/passwd中UID大於500的使用者名稱和uid

[[email protected] ~]# awk -F: '$3>500 {printf "%-25s%-s\n",$1,$3}' /etc/passwd

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

[[email protected] ~]# awk '/(root|net|ucp)/ {print $0}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
systemd-network:x:998:996:systemd Network Management:/:/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

[[email protected] ~]# cat baidu_test.txt 
http://www.baidu.com/more/ 
http://www.baidu.com/guding/more.html 
http://www.baidu.com/events/20060105/photomore.html 
http://hi.baidu.com/browse/ 
http://www.sina.com.cn/head/www20021123am.shtml 
http://www.sina.com.cn/head/www20041223am.shtml
[[email protected] ~]# awk -F '/' '{print $3}' baidu_test.txt | sort | uniq -c | sort -nr
      3 www.baidu.com
      2 www.sina.com.cn
      1 hi.baidu.com

7、請打印出/etc/passwd 第一個域,並且在第一個域所有的內容前面加上“使用者帳號:”

[[email protected] ~]# awk -F: '{print "使用者賬號:" $1}' /etc/passwd

8、請打印出/etc/passwd 第三個域和第四個域

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

9、請列印第一域,並且列印頭部資訊為:這個是系統使用者,列印尾部資訊為:"================"

[[email protected] ~]# awk -F: 'BEGIN {print "這個是系統使用者"}{print $1} END {print"================"}' /etc/passwd
這個是系統使用者
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
avahi-autoipd
systemd-bus-proxy
systemd-network
dbus
polkitd
colord
tss
unbound
usbmuxd
saslauth
libstoragemgmt
geoclue
abrt
rpc
setroubleshoot
rtkit
pulse
gdm
chrony
rpcuser
nfsnobody
radvd
qemu
mysql
gnome-initial-setup
avahi
postfix
sshd
ntp
tcpdump
apache
named
redhat
================

10、請打印出第一域匹配daemon的資訊.

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

11、請將/etc/passwd 中的root替換成gongda,記住是臨時替換輸出螢幕看到效果即可.

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

gsub函式則使得在所有正則表示式被匹配的時候都發生替換
gsub(regular expression, subsitution string, target string);
簡稱 gsub(r,s,t)

sub匹配第一次出現的符合模式的字串,相當於 sed ‘s//’
gsub匹配所有的符合模式的字串,相當於 sed ‘s//g’

12、請匹配passwd最後一段域bash結尾的資訊,有多少條

[[email protected] ~]# awk -F: '{$NF~/bash/}{print NR}' /etc/passwd | wc -l
46

13、請同時匹配passwd檔案中,帶mail或bash的關鍵字的資訊

[[email protected] ~]# awk -F: '$0~/root|mail/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin