美文网首页Linux运维路线工作生活
linux-文本排序和搜索命令

linux-文本排序和搜索命令

作者: 依然慢节奏 | 来源:发表于2019-07-04 17:21 被阅读8次

    一、sort命令

    image.png
    [root@localhost /home/unnet/data]#whatis sort
    sort (1)             - sort lines of text files
    sort (3pm)           - perl pragma to control sort() behaviour
    [root@localhost /home/unnet/data]#cat c.txt 
    172.20.4.56:a
    172.20.4.57:b
    172.20.4.58:c
    172.20.4.59:d
    172.20.4.60:e
    [root@localhost /home/unnet/data]#sort -t: -k2 c.txt 
    172.20.4.56:a
    172.20.4.57:b
    172.20.4.58:c
    172.20.4.59:d
    172.20.4.60:e
    [root@localhost /home/unnet/data]#sort -t: -k2 -r c.txt 
    172.20.4.60:e
    172.20.4.59:d
    172.20.4.58:c
    172.20.4.57:b
    172.20.4.56:a
    ###产生序列
    [root@localhost /home/unnet/data]#whatis seq
    seq (1)              - print a sequence of numbers
    [root@localhost /home/unnet/data]#seq 5
    1
    2
    3
    4
    5
    [root@localhost /home/unnet/data]#seq 5 | sort -r
    5
    4
    3
    2
    1
    [root@localhost /home/unnet/data]#whatis df
    df (1)               - report file system disk space usage
    [root@localhost /home/unnet/data]#df
    Filesystem              1K-blocks     Used Available Use% Mounted on
    /dev/mapper/centos-root 258994628 49084992 209909636  19% /
    devtmpfs                  3992736        0   3992736   0% /dev
    tmpfs                     4004628        0   4004628   0% /dev/shm
    tmpfs                     4004628   435756   3568872  11% /run
    tmpfs                     4004628        0   4004628   0% /sys/fs/cgroup
    /dev/sda1                 1038336   189832    848504  19% /boot
    overlay                 258994628 49084992 209909636  19% /var/lib/docker/overlay2/cf98a981336cbd8bfb561f8ef34ab2724b8da54221d0f88e6f34826e757e35ca/merged
    shm                         65536        0     65536   0% /var/lib/docker/containers/8653adad98d915365b1f77a6e290a27fdb117d2c0ee5b32efc2a5ad6b6b1fc7d/shm
    overlay                 258994628 49084992 209909636  19% /var/lib/docker/overlay2/c66472c900cc30a6925f6e25dc84d94e5069bbd71b15f2de19652baae9c74fc7/merged
    shm                         65536        0     65536   0% /var/lib/docker/containers/90923ef329df6410a91b9aecfa98ba01b5e2da5991e853175d5d02fbcfda9f7e/shm
    overlay                 258994628 49084992 209909636  19% /var/lib/docker/overlay2/9b12dc96f353724bb318351afe980ee0fccab08183aa19c4e800eb65c8d54979/merged
    shm                         65536        0     65536   0% /var/lib/docker/containers/133d8e1a17fdcd1a54e776144ea45d2af5df93d90cbd09055fb2c38b08edfb02/shm
    overlay                 258994628 49084992 209909636  19% /var/lib/docker/overlay2/dbc4fbc7ec466a9193de44be572d0b27f54cdff57d426fca3c8572373fc283ba/merged
    shm                         65536        0     65536   0% /var/lib/docker/containers/03e4502774fef909f25ffe0cd17425343fc8ecfa4bef662940e0326ee8a7a22e/shm
    overlay                 258994628 49084992 209909636  19% /var/lib/docker/overlay2/3ad706b90f36b0d92a8ccc446188205890f82163bee926f22a00b450f19084c7/merged
    shm                         65536        0     65536   0% /var/lib/docker/containers/f3d27b0ae7cf8a360ce7773199f82814ebda3fd88ec3075a1e9bba0cf91be05b/shm
    tmpfs                      800928        0    800928   0% /run/user/0
    overlay                 258994628 49084992 209909636  19% /var/lib/docker/overlay2/b0996aa7f24835451b337b974f377ea2c583d88175978328e89032440877bf4a/merged
    shm                         65536        0     65536   0% /var/lib/docker/containers/ea3e3d96d9f6426b16caabe0a20f1f64e845ca7fe7acdd31f5eee0c6de562251/shm
    [root@localhost /home/unnet/data]#
    
    ###磁盘利用率取最大值
    [root@localhost /home/unnet/data]#df | tr -s " " : | cut -d: -f5 | tr -d "%" | sort -nr | head -1 
    19
    
    

    二、uniq命令

    image.png
    [root@localhost /home/unnet/data]#cat a.txt 
    172.20.4.56
    172.20.4.56
    172.20.4.56
    172.20.4.57
    172.20.4.58
    172.20.4.58
    172.20.4.58
    172.20.4.56
    172.20.4.59
    172.20.4.59
    172.20.4.60
    172.20.4.60
    172.20.4.59
    172.20.4.59
    172.20.4.60
    172.20.4.60
    [root@localhost /home/unnet/data]#uniq -c a.txt 
          3 172.20.4.56
          1 172.20.4.57
          3 172.20.4.58
          1 172.20.4.56
          2 172.20.4.59
          2 172.20.4.60
          2 172.20.4.59
          2 172.20.4.60
    [root@localhost /home/unnet/data]#uniq -d a.txt 
    172.20.4.56
    172.20.4.58
    172.20.4.59
    172.20.4.60
    172.20.4.59
    172.20.4.60
    [root@localhost /home/unnet/data]#uniq -u a.txt 
    172.20.4.57
    172.20.4.56
    

    三、grep、sed、awk命令

    image.png
    image.png
    image.png

    获取硬盘的磁盘利用率

    [root@localhost /home/unnet/data]#df
    Filesystem              1K-blocks     Used Available Use% Mounted on
    /dev/mapper/centos-root 258994628 49085072 209909556  19% /
    devtmpfs                  3992736        0   3992736   0% /dev
    tmpfs                     4004628        0   4004628   0% /dev/shm
    tmpfs                     4004628   435756   3568872  11% /run
    tmpfs                     4004628        0   4004628   0% /sys/fs/cgroup
    /dev/sda1                 1038336   189832    848504  19% /boot
    overlay                 258994628 49085072 209909556  19% /var/lib/docker/overlay2/cf98a981336cbd8bfb561f8ef34ab2724b8da54221d0f88e6f34826e757e35ca/merged
    shm                         65536        0     65536   0% /var/lib/docker/containers/8653adad98d915365b1f77a6e290a27fdb117d2c0ee5b32efc2a5ad6b6b1fc7d/shm
    overlay                 258994628 49085072 209909556  19% /var/lib/docker/overlay2/c66472c900cc30a6925f6e25dc84d94e5069bbd71b15f2de19652baae9c74fc7/merged
    shm                         65536        0     65536   0% /var/lib/docker/containers/90923ef329df6410a91b9aecfa98ba01b5e2da5991e853175d5d02fbcfda9f7e/shm
    overlay                 258994628 49085072 209909556  19% /var/lib/docker/overlay2/9b12dc96f353724bb318351afe980ee0fccab08183aa19c4e800eb65c8d54979/merged
    shm                         65536        0     65536   0% /var/lib/docker/containers/133d8e1a17fdcd1a54e776144ea45d2af5df93d90cbd09055fb2c38b08edfb02/shm
    overlay                 258994628 49085072 209909556  19% /var/lib/docker/overlay2/dbc4fbc7ec466a9193de44be572d0b27f54cdff57d426fca3c8572373fc283ba/merged
    shm                         65536        0     65536   0% /var/lib/docker/containers/03e4502774fef909f25ffe0cd17425343fc8ecfa4bef662940e0326ee8a7a22e/shm
    overlay                 258994628 49085072 209909556  19% /var/lib/docker/overlay2/3ad706b90f36b0d92a8ccc446188205890f82163bee926f22a00b450f19084c7/merged
    shm                         65536        0     65536   0% /var/lib/docker/containers/f3d27b0ae7cf8a360ce7773199f82814ebda3fd88ec3075a1e9bba0cf91be05b/shm
    tmpfs                      800928        0    800928   0% /run/user/0
    overlay                 258994628 49085072 209909556  19% /var/lib/docker/overlay2/b0996aa7f24835451b337b974f377ea2c583d88175978328e89032440877bf4a/merged
    shm                         65536        0     65536   0% /var/lib/docker/containers/ea3e3d96d9f6426b16caabe0a20f1f64e845ca7fe7acdd31f5eee0c6de562251/shm
    [root@localhost /home/unnet/data]#df | grep /dev/sda | tr -s ' ' % | cut -d% -f5
    19
    

    获取服务器连接的IP地址

    [root@localhost /home/unnet/data]#whatis ss
    ss (8)               - another utility to investigate sockets
    [root@localhost /home/unnet/data]#ss -nt | grep ESTAB
    ESTAB      0      0      172.17.0.1:42164              172.17.0.2:3306               
    ESTAB      0      0      172.17.0.1:44448              172.17.0.2:3306               
    ESTAB      0      0      172.17.0.1:44446              172.17.0.2:3306               
    ESTAB      0      52     172.20.4.56:22                 172.23.17.178:57152              
    ESTAB      0      0      172.17.0.1:56124              172.17.0.4:1521               
    ESTAB      0      0      172.17.0.1:45280              172.17.0.2:3306               
    ESTAB      0      0      172.17.0.1:45276              172.17.0.2:3306               
    ESTAB      0      0       ::ffff:172.20.4.56:3306                 ::ffff:172.17.0.3:42590              
    ESTAB      0      0       ::ffff:172.20.4.56:3306                 ::ffff:172.17.0.3:43422              
    ESTAB      0      0       ::ffff:172.20.4.56:3306                 ::ffff:172.17.0.3:40310              
    ESTAB      0      0       ::ffff:172.20.4.56:1521                 ::ffff:172.17.0.5:46602              
    ESTAB      0      0       ::ffff:172.20.4.56:3306                 ::ffff:172.17.0.3:43426              
    ESTAB      0      0       ::ffff:172.20.4.56:3306                 ::ffff:172.17.0.3:42591
    

    扫描IP段那些主机是开机状态

    [root@localhost /home/unnet/data]#nmap -v -sP 172.20.4.0/24 | grep up -B 1 | grep report
    Nmap scan report for 172.20.4.1
    Nmap scan report for 172.20.4.2
    Nmap scan report for 172.20.4.3
    Nmap scan report for 172.20.4.4
    Nmap scan report for 172.20.4.5
    Nmap scan report for 172.20.4.20
    Nmap scan report for 172.20.4.21
    Nmap scan report for 172.20.4.22
    Nmap scan report for 172.20.4.24
    Nmap scan report for 172.20.4.25
    Nmap scan report for 172.20.4.27
    Nmap scan report for 172.20.4.30
    Nmap scan report for 172.20.4.31
    Nmap scan report for 172.20.4.32
    Nmap scan report for 172.20.4.33
    Nmap scan report for 172.20.4.59
    Nmap scan report for 172.20.4.56
    

    相关文章

      网友评论

        本文标题:linux-文本排序和搜索命令

        本文链接:https://www.haomeiwen.com/subject/kferhctx.html