1>獲取文件lll.txt的最後一行的倒數第二列字段及最後一列字段
[root@lll3 apache]# cat lll.txt 1 邵陽 slgjosjgsgjlj agjsdlajaosg 歐陽子任 2 長沙 ashgiodagidas asgjksaogsgg 諸葛孔明 3 懷化 slgasojgsijsi alskgehoiasg 人中風林 4 永州 salgjeoagjeib sagldkjalsjd 司馬中達 5 婁底 lsdajgodsaige sdlgfoasiegl 西門吹雪 [root@lll3 apache]# awk 'NR==3{print $(NF-1)"---"$NF}' lll.txt alskgehoiasg---人中風林
2>取出/var/log/messages-20191124中前五行數據的天時分,並且帶有順序標號
[root@lll3 /]# cat -n /var/log/messages-20191124|head -5 1 Nov 14 21:35:03 localhost rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="1095" x-info="http://www.rsyslog.com"] rsyslogd was HUPed 2 Nov 14 21:40:01 localhost systemd: Started Session 9 of user root. 3 Nov 14 21:40:01 localhost systemd: Starting Session 9 of user root. 4 Nov 14 21:50:01 localhost systemd: Started Session 10 of user root. 5 Nov 14 21:50:01 localhost systemd: Starting Session 10 of user root. [root@lll3 /]# cat -n /var/log/messages-20191124|head -5|awk -F ":" '{print $1 $2}' 1 Nov 14 2135 2 Nov 14 2140 3 Nov 14 2140 4 Nov 14 2150 5 Nov 14 2150
3>取出網卡ens33的ip地址
[root@lll3 data]# ifconfig ens33|awk 'NR==2'|awk -F " " '{print $2}' 192.168.0.104
4>取出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@lll3 data]# cut -d "/" -f3 lll.txt |sort|uniq -c 1 mp3.baidu.com 2 post.baidu.com 3 www.baidu.com [root@lll3 data]# awk -F "/" '{print $3}' lll.txt|sort|uniq -c 1 mp3.baidu.com 2 post.baidu.com 3 www.baidu.com
5>取出/etc/passwd文件中的第一列數據也就是取出用戶名
[root@lll3 data]# awk -F ":" '{print $1}' /etc/passwd
6>
# cat a
a b 1
c d 2
e f 3
g h 3
i j 2
1>獲取第三字段最大值.
2>打印第三字段最大行.
[root@lll3 opt]# cat lll1.txt a b 1 c d 2 e f 3 g h 3 i j 2 [root@lll3 opt]# awk 'BEGIN{max=0}{if($3>max)max=$3}END{print max}' lll1.txt 3
[root@lll3 opt]# awk 'BEGIN{max=0}{a[$0]=$3;if($3>max)max=$3}END{for(v in a)if(a[v]==max)print v}' lll1.txt g h 3 e f 3