第四十二天-企业面试实战题

处理一下文件内容,将域名取出并根据域名进行计数排序处理

例题1:

xiaorui@xiaorui:~$ cat oldboy.log
http://www.etiantian.org/index.html
http://www.etiantian.org/1.html
http://post.etiantian.org/index.html
http://mp3.etiantian.org/index.html
http://www.etiantian.org/3.html
http://www.etiantian.org/index.html
http://post.etiantian.org/2.html
xiaorui@xiaorui:~$ awk -F '/' '{print $3}' oldboy.log|sort |uniq -c|sort -rn
      4 www.etiantian.org
      2 post.etiantian.org
      1 mp3.etiantian.org

例题2:

xiaorui@xiaorui:~$ cat oldboy.txt 
10.0.0.9
10.0.0.10
10.0.0.7
10.0.0.7
10.0.0.7
10.0.0.8
10.0.0.9
10.0.0.8

xiaorui@xiaorui:~$ sort -n -t. -k4,4.2 oldboy.txt 
10.0.0.7
10.0.0.7
10.0.0.7
10.0.0.8
10.0.0.8
10.0.0.9
10.0.0.9
10.0.0.10
xiaorui@xiaorui:~$ sort -n -t. -k4,4.2 oldboy.txt |uniq -c
      3 10.0.0.7
      2 10.0.0.8
      2 10.0.0.9
      1 10.0.0.10
xiaorui@xiaorui:~$ sort -n -t. -k4,4.2 oldboy.txt |uniq -c|sort -rn
      3 10.0.0.7
      2 10.0.0.9
      2 10.0.0.8
      1 10.0.0.10

uniq 去重 -c 计数
sort -n -t. -k3,3 -k4.1,4.3 arp.txt
    -n               按数字排序
    -t.               按点号分割域
    -k3,3          从第三个字段开始排序,到第三个字段结束
    -k4.1,4.3    从第四个字段的第一个字符开始排序,到第四个字段的第三个字符结束
                     (点号连接的是字符,逗号连接的是字段)

例题3:生产服务器某个业务LVS负载均衡上连接状态数量

[xiaorui@server2host ~]$ netstat -an |awk '/^tcp/ {print $6}'|sort|uniq -c|sort -rn -k1
    203 TIME_WAIT
     64 ESTABLISHED
     63 LISTEN
      3 FIN_WAIT2
      3 CLOSE_WAIT
      1 LAST_ACK
      1 FIN_WAIT1
[xiaorui@server2host ~]$ netstat -an|awk '/^tcp/ {++S[$NF]} END {for(a in S)print a,S[a]}'|sort -rn -k2
TIME_WAIT 224
ESTABLISHED 68
LISTEN 63
FIN_WAIT2 9
CLOSE_WAIT 4
LAST_ACK 2
FIN_WAIT1 1

命令拆分              功能说明
/^tcp/               过滤出以tcp开头的行,“^”为正则表达式用法,以...开头,这里是过滤出以tcp开头的行。
S[]                   定义了一个名叫S的数组,在awk中,数组下标通常从 1 开始,而不是 0。
NF                  当前记录里域个数,默认以空格分隔,如上所示的记录,NF域个数等于6
$NF                   表示一行的最后一个域的值,如上所示的记录,$NF也就是$6,表示第6个字段的值,也就是SYN_RECV或TIME_WAIT等。
S[$NF]                 表示数组元素的值,如上所示的记录,就是S[TIME_WAIT]状态的连接数
++S[$NF]             表示把某个数加一,如上所示的记录,就是把S[TIME_WAIT]状态的连接数加一
END
for(key in S)              遍历S[]数组
print key,” ”,S[key]    打印数组的键和值,中间用 制表符分割,显示好一些。

netstat第六列State的状态信息

ESTABLISHED    The socket has an established connection.
socket已经建立连接,表示处于连接的状态,一般认为有一个ESTABLISHED认为是一个服务的并发连接。这个连接状态在生产场景很重要,要重点关注。

SYN_SENT       The socket is actively attempting to establish a connection.
socket正在积极尝试建立一个连接,即处于发送后连接前的一个等待但未匹配进入连接的状态。

SYN_RECV       A connection request has been received from the network.
已经从网络上收到一个连接请求。

FIN_WAIT1       The socket is closed, and the connection is shutting down.
socket已关闭,连接正在或正要关闭。

FIN_WAIT2       Connectionisclosed,andthesocket is waiting for a shutdown from the remote end.
连接已关闭,并且socket正在等待远端结束。

TIME_WAIT        The socket is waiting after close to handle packets still in the network.
socket正在等待关闭处理仍在网络上的数据包,这个连接状态在生产场景很重要,要重点关注。
CLOSED        The socket is not being used.| socket不在被占用了。

CLOSE_WAIT     The remote end has shutdown, waiting for the socket to close.
远端已经结束,等待socket关闭。

LAST_ACK       The remote end has shut down, and the socket is closed. Waiting for acknowl-edgement.|
远端已经结束,并且socket也已关闭,等待acknowl-edgement。

LISTEN          Thesocketislisteningforincoming connections.Such sockets are not
included in the output unless you specify the --listening (-l) or --all (-a) option.
socket正在监听连接请求。

CLOSING       Both sockets are shut down but we still don’t have all our data sent.
sockets关闭,但是我们仍旧没有发送数据。

UNKNOWN         The state of the socket is unknown
未知的状态。

例题4:

xiaorui@xiaorui:~$ sort -n -k4 oldboy.txt
004 name kuqi 8k
006 name xiaofan 8k
003 name jeacen 10k
005 name yideng 10k
001 name wodi 12k
002 name yingsui 15k

xiaorui@xiaorui:~$ cat oldboy.txt |awk '/^00/ {++S[$NF]} END {for (key in S) print key,S[key]}'
12k 1
15k 1
8k 2
10k 2

xiaorui@xiaorui:~$ awk '/^00/ {++S[$NF]} END {for (key in S) print key,S[key]}' oldboy.txt
12k 1
15k 1
8k 2
10k 2

例题5:统计apache日志单ip访问请求数排名(这个常用,考试也常考)
假设apache日志内容access.log内容为:

xiaorui@xiaorui:~$ cat access.log
10.0.0.41--[03/Dec/2010:23:27:01 +0800] "HEAD /checkstatus.jsp HTTP/1.0" 200
10.0.0.43--[03/Dec/2010:23:27:01 +0800] "HEAD /checkstatus.jsp HTTP/1.0" 200
10.0.0.42--[03/Dec/2010:23:27:01 +0800] "HEAD /checkstatus.jsp HTTP/1.0" 200
10.0.0.46--[03/Dec/2010:23:27:02 +0800] "HEAD /checkstatus.jsp HTTP/1.0" 200
10.0.0.42--[03/Dec/2010:23:27:02 +0800] "HEAD /checkstatus.jsp HTTP/1.0" 200
10.0.0.47--[03/Dec/2010:23:27:02 +0800] "HEAD /checkstatus.jsp HTTP/1.0" 200
10.0.0.41--[03/Dec/2010:23:27:02 +0800] "HEAD /checkstatus.jsp HTTP/1.0" 200
10.0.0.47--[03/Dec/2010:23:27:02 +0800] "HEAD /checkstatus.jsp HTTP/1.0" 200
10.0.0.41--[03/Dec/2010:23:27:03 +0800] "HEAD /checkstatus.jsp HTTP/1.0" 200
10.0.0.46--[03/Dec/2010:23:27:03 +0800] "HEAD /checkstatus.jsp HTTP/1.0" 200


xiaorui@xiaorui:~$ awk -F " " '{print $1}' access.log|sort |uniq -c|sort -rn -k1
      3 10.0.0.41
      2 10.0.0.47
      2 10.0.0.46
      2 10.0.0.42
      1 10.0.0.43

xiaorui@xiaorui:~$ awk '{++S[$1]}END {for (key in S) print key,S[key]}' access.log |sort -nr -k2
10.0.0.41 3
10.0.0.47 2
10.0.0.46 2
10.0.0.42 2
10.0.0.43 1

xiaorui@xiaorui:~$ sed 's#--.*$##g' access.log |sort |uniq -c|sort -rn
      3 10.0.0.41
      2 10.0.0.47
      2 10.0.0.46
      2 10.0.0.42
      1 10.0.0.43
原文地址:https://www.cnblogs.com/migongci0412/p/5107886.html