zoukankan      html  css  js  c++  java
  • 文本处理工具上

    本章内容:
      各种文本工具来查看、分析、统计文本
      正则表达式grep
      扩展正则表达式egrep

    windows的配置是放在注册表里面的,管理起来不方便;在Linux学习的服务基本是修改配置文件。

    注意不能使用文本工具处理二进制程序。

    正则表达式是重中之重,在很多地方反复使用,学习工作中经常使用的,同时也是难点。理解了正则表达式那么理解扩展的正则表达式也不难的。

    文本处理的三剑客是grep,sed,awk

    一文件查看

    文件 查看命令:cat, tac,rev
    cat [OPTION]... [FILE]...
    -E:  显示行结束符$
    -n:  对显示出的每一行进行 编号
    -A :显示所有控制符
    -b: : 非空行编号
    -s :压缩连续的空行成一行

    (一)cat

    [root@centos72 ~]# ls
    anaconda-ks.cfg
    [root@centos72 ~]# cp  anaconda-ks.cfg   /app/
    [root@centos72 ~]# ls  /app/
    anaconda-ks.cfg
    [root@centos72 ~]# file  /app/anaconda-ks.cfg 
    /app/anaconda-ks.cfg: ASCII text
    [root@centos72 ~]# 
    [root@centos72 ~]# cat  /app/anaconda-ks.cfg 
    #version=DEVEL
    # System authorization information
    auth --enableshadow --passalgo=sha512
    # Use CDROM installation media
    cdrom
    # Use graphical install
    graphical
    # Run the Setup Agent on first boot
    firstboot --enable
    ignoredisk --only-use=sda
    # Keyboard layouts
    keyboard --vckeymap=us --xlayouts='us'
    # System language
    lang en_US.UTF-8
    
    # Network information
    network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
    network  --hostname=centos72.huawei.com
    
    # Root password
    rootpw --iscrypted $6$3ZpKJEd3ctkruWkF$ACv/Y4HSNb4lTqk4Gbol157B2lHw0AVcKM1rjEshEOrMcIIXw1DvoPPCZy3y3i.SijcTdTAfvFs/uFPwLxKd51
    # System services
    services --disabled="chronyd"
    # System timezone
    timezone Asia/Shanghai --isUtc --nontp
    user --name=wang --password=$6$PqqaCIq7qipkXclF$5idE9A8TzG/yLzqHbmlSg9cVaNUmxPG85y/K81a0KSrosFH/srLzY0HQxeTUMZKs.KVoyJOphaA8Xz.nidUF// --iscrypted --gecos="wang"
    # System bootloader configuration
    bootloader --location=mbr --boot-drive=sda
    # Partition clearing information
    clearpart --none --initlabel
    # Disk partitioning information
    part swap --fstype="swap" --ondisk=sda --size=2048
    part /app --fstype="xfs" --ondisk=sda --size=20480
    part / --fstype="xfs" --ondisk=sda --size=51200
    part /boot --fstype="xfs" --ondisk=sda --size=1024
    
    %packages
    @^minimal
    @core
    
    %end
    
    %addon com_redhat_kdump --disable --reserve-mb='auto'
    
    %end
    
    %anaconda
    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    %end

    (1)-n:  对显示出的每一行进行编号

    包括空行也加行号

    注意选项可以写在中间或者最后

    [root@centos72 ~]# cat  /app/anaconda-ks.cfg   -n
         1    #version=DEVEL
         2    # System authorization information
         3    auth --enableshadow --passalgo=sha512
         4    # Use CDROM installation media
         5    cdrom
         6    # Use graphical install
         7    graphical
         8    # Run the Setup Agent on first boot
         9    firstboot --enable
        10    ignoredisk --only-use=sda
        11    # Keyboard layouts
        12    keyboard --vckeymap=us --xlayouts='us'
        13    # System language
        14    lang en_US.UTF-8
        15    
        16    # Network information
        17    network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
        18    network  --hostname=centos72.huawei.com
        19    
        20    # Root password
        21    rootpw --iscrypted $6$3ZpKJEd3ctkruWkF$ACv/Y4HSNb4lTqk4Gbol157B2lHw0AVcKM1rjEshEOrMcIIXw1DvoPPCZy3y3i.SijcTdTAfvFs/uFPwLxKd51
        22    # System services
        23    services --disabled="chronyd"
        24    # System timezone
        25    timezone Asia/Shanghai --isUtc --nontp
        26    user --name=wang --password=$6$PqqaCIq7qipkXclF$5idE9A8TzG/yLzqHbmlSg9cVaNUmxPG85y/K81a0KSrosFH/srLzY0HQxeTUMZKs.KVoyJOphaA8Xz.nidUF// --iscrypted --gecos="wang"
        27    # System bootloader configuration
        28    bootloader --location=mbr --boot-drive=sda
        29    # Partition clearing information
        30    clearpart --none --initlabel
        31    # Disk partitioning information
        32    part swap --fstype="swap" --ondisk=sda --size=2048
        33    part /app --fstype="xfs" --ondisk=sda --size=20480
        34    part / --fstype="xfs" --ondisk=sda --size=51200
        35    part /boot --fstype="xfs" --ondisk=sda --size=1024
        36    
        37    %packages
        38    @^minimal
        39    @core
        40    
        41    %end
        42    
        43    %addon com_redhat_kdump --disable --reserve-mb='auto'
        44    
        45    %end
        46    
        47    %anaconda
        48    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
        49    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
        50    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
        51    %end

    添加行号

    [root@centos72 ~]# nl   /etc/issue
         1    S
         2    Kernel 
     on an m
           
    [root@centos72 ~]# cat     /etc/issue  -n
         1    S
         2    Kernel 
     on an m
         3    
    [root@centos72 ~]# cat     /etc/issue   |  wc
          3       6      23

    (2)-b: 非空行编号

    [root@centos72 ~]# cat  /app/anaconda-ks.cfg   -b
         1    #version=DEVEL
         2    # System authorization information
         3    auth --enableshadow --passalgo=sha512
         4    # Use CDROM installation media
         5    cdrom
         6    # Use graphical install
         7    graphical
         8    # Run the Setup Agent on first boot
         9    firstboot --enable
        10    ignoredisk --only-use=sda
        11    # Keyboard layouts
        12    keyboard --vckeymap=us --xlayouts='us'
        13    # System language
        14    lang en_US.UTF-8
    
        15    # Network information
        16    network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
        17    network  --hostname=centos72.huawei.com
    
        18    # Root password
        19    rootpw --iscrypted $6$3ZpKJEd3ctkruWkF$ACv/Y4HSNb4lTqk4Gbol157B2lHw0AVcKM1rjEshEOrMcIIXw1DvoPPCZy3y3i.SijcTdTAfvFs/uFPwLxKd51
        20    # System services
        21    services --disabled="chronyd"
        22    # System timezone
        23    timezone Asia/Shanghai --isUtc --nontp
        24    user --name=wang --password=$6$PqqaCIq7qipkXclF$5idE9A8TzG/yLzqHbmlSg9cVaNUmxPG85y/K81a0KSrosFH/srLzY0HQxeTUMZKs.KVoyJOphaA8Xz.nidUF// --iscrypted --gecos="wang"
        25    # System bootloader configuration
        26    bootloader --location=mbr --boot-drive=sda
        27    # Partition clearing information
        28    clearpart --none --initlabel
        29    # Disk partitioning information
        30    part swap --fstype="swap" --ondisk=sda --size=2048
        31    part /app --fstype="xfs" --ondisk=sda --size=20480
        32    part / --fstype="xfs" --ondisk=sda --size=51200
        33    part /boot --fstype="xfs" --ondisk=sda --size=1024
    
        34    %packages
        35    @^minimal
        36    @core
    
        37    %end
    
        38    %addon com_redhat_kdump --disable --reserve-mb='auto'
    
        39    %end
    
        40    %anaconda
        41    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
        42    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
        43    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
        44    %end

     和此选项等价的命令

    [root@centos72 ~]# nl   /etc/issue
         1    S
         2    Kernel 
     on an m
    [root@centos72 ~]# cat     /etc/issue   -b
         1    S
         2    Kernel 
     on an m

    (3)

    -E:  显示行结束符$,也就是换行

    [root@centos72 ~]# cat  /app/anaconda-ks.cfg   -E
    #version=DEVEL$
    # System authorization information$
    auth --enableshadow --passalgo=sha512$
    # Use CDROM installation media$
    cdrom$
    # Use graphical install$
    graphical$
    # Run the Setup Agent on first boot$
    firstboot --enable$
    ignoredisk --only-use=sda$
    # Keyboard layouts$
    keyboard --vckeymap=us --xlayouts='us'$
    # System language$
    lang en_US.UTF-8$
    $
    # Network information$
    network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate$
    network  --hostname=centos72.huawei.com$
    $
    # Root password$
    rootpw --iscrypted $6$3ZpKJEd3ctkruWkF$ACv/Y4HSNb4lTqk4Gbol157B2lHw0AVcKM1rjEshEOrMcIIXw1DvoPPCZy3y3i.SijcTdTAfvFs/uFPwLxKd51$
    # System services$
    services --disabled="chronyd"$
    # System timezone$
    timezone Asia/Shanghai --isUtc --nontp$
    user --name=wang --password=$6$PqqaCIq7qipkXclF$5idE9A8TzG/yLzqHbmlSg9cVaNUmxPG85y/K81a0KSrosFH/srLzY0HQxeTUMZKs.KVoyJOphaA8Xz.nidUF// --iscrypted --gecos="wang"$
    # System bootloader configuration$
    bootloader --location=mbr --boot-drive=sda$
    # Partition clearing information$
    clearpart --none --initlabel$
    # Disk partitioning information$
    part swap --fstype="swap" --ondisk=sda --size=2048$
    part /app --fstype="xfs" --ondisk=sda --size=20480$
    part / --fstype="xfs" --ondisk=sda --size=51200$
    part /boot --fstype="xfs" --ondisk=sda --size=1024$
    $
    %packages$
    @^minimal$
    @core$
    $
    %end$
    $
    %addon com_redhat_kdump --disable --reserve-mb='auto'$
    $
    %end$
    $
    %anaconda$
    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty$
    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok$
    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty$
    %end$
    [root@centos72 ~]# nano   aaa
    -bash: nano: command not found
    [root@centos72 ~]# yum  install   bash*
    Loaded plugins: fastestmirror
    Determining fastest mirrors
    base                                                                             | 3.6 kB  00:00:00     
    Package bash-4.2.46-30.el7.x86_64 already installed and latest version
    Package 1:bash-completion-2.1-6.el7.noarch already installed and latest version
    Nothing to do
    [root@centos72 ~]# yum  whatprovides  nano
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    nano-2.3.1-10.el7.x86_64 : A small text editor
    Repo        : base
    
    
    
    [root@centos72 ~]# yum  install  nano  -y
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Resolving Dependencies
    --> Running transaction check
    ---> Package nano.x86_64 0:2.3.1-10.el7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ========================================================================================================
     Package              Arch                   Version                         Repository            Size
    ========================================================================================================
    Installing:
     nano                 x86_64                 2.3.1-10.el7                    base                 440 k
    
    Transaction Summary
    ========================================================================================================
    Install  1 Package
    
    Total download size: 440 k
    Installed size: 1.6 M
    Downloading packages:
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : nano-2.3.1-10.el7.x86_64                                                             1/1 
      Verifying  : nano-2.3.1-10.el7.x86_64                                                             1/1 
    
    Installed:
      nano.x86_64 0:2.3.1-10.el7                                                                            
    
    Complete!

     (4)-A显示所有控制符

    使用nano工具,并且按tab键

    保存的时候按ctrl+x。再按y,回车就可以保存了

    ^I表示tab键

    [root@centos72 ~]# nano  aaa
    [root@centos72 ~]# ls  aaa 
    aaa
    [root@centos72 ~]# cat  aaa 
    a
        b
            c
    [root@centos72 ~]# cat  aaa -E
    a$
        b$
            c$
    [root@centos72 ~]# cat  aaa -A
    a$
    ^Ib$
    ^I^Ic$

    这个很有意义的,比如多行重定向

    EOF结尾,如果多个空格就不会退出

    [root@centos72 ~]# cat  aaa -A
    a$
    ^Ib$
    ^I^Ic$
    [root@centos72 ~]# mail  -s   hi wang  <<  EOF
    > hello
    > how old are you
    > EOF
    

    在aaa文件里面,c后面补一个空格

    肉眼看不到有空格,但是加上选项A就看到了

    [root@centos72 ~]# !na
    nano  aaa
    [root@centos72 ~]# cat  aaa 
    a
        b
            c 
    [root@centos72 ~]# ll  aaa 
    -rw-r--r--. 1 root root 10 May  7 13:09 aaa
    [root@centos72 ~]# ll  aaa   -A
    -rw-r--r--. 1 root root 10 May  7 13:09 aaa
    [root@centos72 ~]# cat  aaa  -A
    a$
    ^Ib$
    ^I^Ic $

    使用二进制的文本工具也可以通过分析二进制的编码看到

    09是tab键,20是空格

    [root@centos72 ~]# hexdump   -C  aaa
    00000000  61 0a 09 62 0a 09 09 63  20 0a                    |a..b...c .|
    0000000a

    (5)-s压缩连续的空行成一行

     多行空行就变成了一行空行了

    [root@centos72 ~]# !na
    nano  aaa
    [root@centos72 ~]# cat  aaa 
    a
        b
            c 
    
    
    a
    
    
    
    d
    
    
    f
    [root@centos72 ~]# cat  aaa  -n
         1    a
         2        b
         3            c 
         4    
         5    
         6    a
         7    
         8    
         9    
        10    d
        11    
        12    
        13    f
    [root@centos72 ~]# cat  aaa  -s
    a
        b
            c 
    
    a
    
    d
    
    f
    [root@centos72 ~]# cat  aaa  -sn
         1    a
         2        b
         3            c 
         4    
         5    a
         6    
         7    d
         8    
         9    f

    查看帮助文档,可以单独显示tab键

    [root@centos72 ~]# cat  aaa  -T
    a
    ^Ib
    ^I^Ic 
    
    
    a
    
    
    
    d
    
    
    f

    (二)tac反向显示

    注意是行的反向显示

    [root@centos72 ~]# cat  aaa 
    aa
    bb
    cc
    [root@centos72 ~]# tac  aaa 
    cc
    bb
    aa

    列的反向显示

    [root@centos72 ~]# echo   abcdefg  |  rev
    gfedcba

    二分页查看文件内容

    (一)more:  分页查看文件


    more [OPTIONS...]    FILE...
    -d:  显示翻页及退出提示

    显示一页内容,到了最后一页会退出。回车是换行,按空格就会往下翻页

    [root@centos72 ~]# more    anaconda-ks.cfg 
    #version=DEVEL
    # System authorization information
    auth --enableshadow --passalgo=sha512
    # Use CDROM installation media
    cdrom
    # Use graphical install
    graphical
    # Run the Setup Agent on first boot
    firstboot --enable
    ignoredisk --only-use=sda
    # Keyboard layouts
    keyboard --vckeymap=us --xlayouts='us'
    # System language
    lang en_US.UTF-8
    
    # Network information
    network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
    network  --hostname=centos72.huawei.com
    
    # Root password
    rootpw --iscrypted $6$3ZpKJEd3ctkruWkF$ACv/Y4HSNb4lTqk4Gbol157B2lHw0AVcKM1rjEshEOrMcIIXw1DvoPPCZy3y3i.Si
    --More--(37%)

    按B就是往前翻页,但是没有less方便

    可以进行管道传输,但是此时B往前翻页不起作用了

    [root@centos72 ~]# cat  anaconda-ks.cfg   |  more

    -d:  显示翻页及退出提示

    注意此选项要加在命令的中间

    --More--(37%)[Press space to continue, 'q' to quit.]

    [root@centos72 ~]# more   -d   anaconda-ks.cfg   
    #version=DEVEL
    # System authorization information
    auth --enableshadow --passalgo=sha512
    # Use CDROM installation media
    cdrom
    # Use graphical install
    graphical
    # Run the Setup Agent on first boot
    firstboot --enable
    ignoredisk --only-use=sda
    # Keyboard layouts
    keyboard --vckeymap=us --xlayouts='us'
    # System language
    lang en_US.UTF-8
    
    # Network information
    network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
    network  --hostname=centos72.huawei.com
    
    # Root password
    rootpw --iscrypted $6$3ZpKJEd3ctkruWkF$ACv/Y4HSNb4lTqk4Gbol157B2lHw0AVcKM1rjEshEOrMcIIXw1DvoPPCZy3y3i.Si
    --More--(37%)[Press space to continue, 'q' to quit.]

    如果是加在最后面就无效了

    [root@centos72 ~]# more  anaconda-ks.cfg   -d
    ::::::::::::::
    anaconda-ks.cfg
    ::::::::::::::
    #version=DEVEL
    # System authorization information
    auth --enableshadow --passalgo=sha512
    # Use CDROM installation media
    cdrom
    # Use graphical install
    graphical
    # Run the Setup Agent on first boot
    firstboot --enable
    ignoredisk --only-use=sda
    # Keyboard layouts
    keyboard --vckeymap=us --xlayouts='us'
    # System language
    lang en_US.UTF-8
    
    # Network information
    network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
    network  --hostname=centos72.huawei.com
    --More--(29%)

    (二)less的使用


    less :是man 命令使用的分页器,也就是使用man实际上是使用less

    一页一页地查看文件或STDIN 输出。按右上角的pgdn是往下翻页,pgup是往上翻页


    /搜索关键字,n跳到匹配的下一个,N跳到匹配的上一个

    配合其他命令分页显示

    [root@centos72 ~]# tree  /etc/ |  less
    /etc/
    ├── adjtime
    ├── aliases
    ├── aliases.db
    ├── alternatives
    │   ├── ld -> /usr/bin/ld.bfd
    │   ├── libnssckbi.so.x86_64 -> /usr/lib64/pkcs11/p11-kit-trust.so
    │   ├── mta -> /usr/sbin/sendmail.postfix
    │   ├── mta-aliasesman -> /usr/share/man/man5/aliases.postfix.5.gz
    │   ├── mta-mailq -> /usr/bin/mailq.postfix
    │   ├── mta-mailqman -> /usr/share/man/man1/mailq.postfix.1.gz
    │   ├── mta-newaliases -> /usr/bin/newaliases.postfix
    │   ├── mta-newaliasesman -> /usr/share/man/man1/newaliases.postfix.1.gz
    │   ├── mta-pam -> /etc/pam.d/smtp.postfix
    │   ├── mta-rmail -> /usr/bin/rmail.postfix
    │   ├── mta-sendmail -> /usr/lib/sendmail.postfix
    │   └── mta-sendmailman -> /usr/share/man/man1/sendmail.postfix.1.gz
    ├── anacrontab
    ├── asound.conf
    ├── audisp
    │   ├── audispd.conf
    :

    三显示文本前或后行内容

    (一)head显示文本前行

    head   [OPTION]... [FILE]...
    -c #:  指定获取前# 字节
    -n #:  指定获取前#行

    (1)默认显示的是文件前10行

    [root@centos72 ~]# head  /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin

    (2)head默认是读取键盘输入的

    每输入一行就打印一行,像鹦鹉学舌一样

    输入10行就会自动退出

    [root@centos72 ~]# head 
    abc
    abc
    1234
    1234
    qwert
    qwert
    ^C

    (3)-n #:  指定获取前#行

    [root@centos72 ~]# head  -n   3
    1
    1
    2
    2
    3
    3
    [root@centos72 ~]# cat  anaconda-ks.cfg   |  head   -n  6
    #version=DEVEL
    # System authorization information
    auth --enableshadow --passalgo=sha512
    # Use CDROM installation media
    cdrom
    # Use graphical install

    (4)显示文件的前几行并且显示行号

    [root@centos72 ~]# cat  anaconda-ks.cfg  -n  |  head   -n  6
         1    #version=DEVEL
         2    # System authorization information
         3    auth --enableshadow --passalgo=sha512
         4    # Use CDROM installation media
         5    cdrom
         6    # Use graphical install
    [root@centos72 ~]# cat -n    anaconda-ks.cfg    |  head   -n  6
         1    #version=DEVEL
         2    # System authorization information
         3    auth --enableshadow --passalgo=sha512
         4    # Use CDROM installation media
         5    cdrom
         6    # Use graphical install
    [root@centos72 ~]#    anaconda-ks.cfg     head   -n  6
    -bash: anaconda-ks.cfg: command not found
    [root@centos72 ~]#     head   -n  6  anaconda-ks.cfg 
    #version=DEVEL
    # System authorization information
    auth --enableshadow --passalgo=sha512
    # Use CDROM installation media
    cdrom
    # Use graphical install
    [root@centos72 ~]# 

    注意-是必须要写的,否则会认为n和6是文件

    [root@centos72 ~]# head   n  6  anaconda-ks.cfg 
    head: cannot open ‘n’ for reading: No such file or directory
    head: cannot open ‘6’ for reading: No such file or directory
    ==> anaconda-ks.cfg <==
    #version=DEVEL
    # System authorization information
    auth --enableshadow --passalgo=sha512
    # Use CDROM installation media
    cdrom
    # Use graphical install
    graphical
    # Run the Setup Agent on first boot
    firstboot --enable
    ignoredisk --only-use=sda

    (5)-c #:  指定获取前# 字节

    显示一行的前几个字节

    注意对于字母来说一个字母就是一个字节

    而一个汉字就相当于3个字节

    [root@centos72 ~]# head   anaconda-ks.cfg 
    #version=DEVEL
    # System authorization information
    auth --enableshadow --passalgo=sha512
    # Use CDROM installation media
    cdrom
    # Use graphical install
    graphical
    # Run the Setup Agent on first boot
    firstboot --enable
    ignoredisk --only-use=sda
    [root@centos72 ~]# head  -c6    anaconda-ks.cfg 
    #versi[root@centos72 ~]# 

    一个大字占3个字节,0a表示回车

    [root@centos72 ~]# echo   大  >  f2
    [root@centos72 ~]# hexdump   -C  f2
    00000000  e5 a4 a7 0a                                       |....|
    00000004
    [root@centos72 ~]# ll  f2
    -rw-r--r--. 1 root root 4 May  7 14:42 f2
    [root@centos72 ~]# 

    生成30位的随机口令,30位也就是30个字符

    法1:

    [root@centos72 ~]# openssl   rand  -base64 30
    WSK7auwuiDyVxpcwm6ZMNZZegm0jmQ+keScHpKRd
    [root@centos72 ~]# openssl   rand  -base64 30 |  head  -c  30
    Ylia6CtKO6G6mNxg864f5Z38cRy2aL[root@centos72 ~]# 

    注意每次都不一样

    [root@centos72 ~]# openssl   rand  -base64 30 |  head  -c  30
    Ylia6CtKO6G6mNxg864f5Z38cRy2aL[root@centos72 ~]# openssl   rand  -base64 30 |  head  -c  30
    NN5Hp7R7uvRRkBloRTGeVwteEuWq2T[root@centos72 ~]# openssl   rand  -base64 30 |  head  -c  30
    znYiUJ69Tg6QoDXU09oF8Rcj86rYvq[root@centos72 ~]# openssl   rand  -base64 30 |  head  -c  30
    R/Vdc5GXYNztTvjrJYojrEgumbJ9oK[root@centos72 ~]# openssl   rand  -base64 30 |  head  -c  30
    NM1clK4uYuhiDIW+B1X0jWKAIYx+Fn[root@centos72 ~]# openssl   rand  -base64 30 |  head  -c  30
    YOcnJeFHuoOK0OIAhmEPe3TTG93XnJ[root@centos72 ~]# openssl   rand  -base64 30 |  head  -c  30
    /zd/aG5dH4ltysyVfdGXqK6jFGoD5U[root@centos72 ~]# openssl   rand  -base64 30 |  head  -c  30
    PEfIFn7flWOSNiyZyTetwCMfhy86wv[root@centos72 ~]# openssl   rand  -base64 30 |  head  -c  30
    WrnLntoumwz78R9kTk0opsTU9A1MwN[root@centos72 ~]# openssl   rand  -base64 30 |  head  -c  30
    0rmgIAjWc+GpiwpMTLvB6ICPbqG8Z5[root@centos72 ~]# openssl   rand  -base64 30 |  head  -c  30
    bpv7uk38XGixkh1BlOZMYF+9vK2qfB[root@centos72 ~]# openssl   rand  -base64 30 |  head  -c  30
    lDWBCo/kq1YhFE0B58j7xlax2Tp6xG[root@centos72 ~]# 
    [root@centos73 ~]# openssl   rand  -base64 30 
    HZV4gx6Y8RNWgtkTDwM0BnsD31amivawVQW4XV1d
    [root@centos73 ~]# openssl   rand  -base64 30 
    vn/9HAH1NWaQ+0zdiKrmKky5nKW1jwPhs176vrAC
    [root@centos73 ~]# openssl   rand  -base64 30 
    kkaa6w6pN71D2YLFj4lkLOjDihqmjj+eTKvx+jsx
    [root@centos73 ~]# openssl   rand  -base64 30 
    CHS1gSJkfqDRoEMz5Oy7EvR505yGypWSI2QlQtRb
    [root@centos73 ~]# openssl   rand  -base64 30 
    hV+1C0SAHHET8ddjiff3RL6nBHwcwFpYoypR9icL
    [root@centos73 ~]# openssl   rand  -base64 30 
    ELUYAUuF1LeVkQVpeL6latrqHVPTJPMm7DWGzNKQ

    法2:

    使用设备生成随机数

    但是默认显示的很多随机数是乱码的,要对其进行过滤,取出前30个没有乱码的字符

    取出数字加字母就够用了

    [root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    GcCS7V3zdbxf7ULACZxyileWOkVbb4[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    4HRYAPkEmR9IU5DmwUCfPH5yFchmWk[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    Xai5M56ExMu3lPTk3uItOrqOIyrHBi[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    EM19jKg8elb0XaBZ5fBt6HlzvADj6V[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    2nGHfnxibhXymvYY50933w9IKZY2a1[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    FtlUPlUBAJqGuCF8D8mBWUA1Qy4mIE[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    pP7Qq0iqqv3ualzDdf6JYcyA7nnL0X[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    mcrr0qTGkAgFQ4Qp8wQYEOXkth6KBh[root@centos72 ~]# 

     命令解析:

    tr -dc '[:alnum:]'表示删除除了字母数字的其他字符

    不加引号的结果:

    [root@centos73 ~]# cat  /dev/urandom  |  tr  -dc   [:alnum:] |  head  -c30
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[root@centos73 ~]# cat  /dev/urandom  |  tr  -dc   [:alnum:] |  head  -c30
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[root@centos73 ~]# cat  /dev/urandom  |  tr  -dc   [:alnum:] |  head  -c30
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[root@centos73 ~]# cat  /dev/urandom  |  tr  -dc   [:alnum:] |  head  -c30
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[root@centos73 ~]# cat  /dev/urandom  |  tr  -dc   [:alnum:] |  head  -c30
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[root@centos73 ~]# 

    使用另外一种写法:

    [root@centos72 ~]#  tr  -dc   '[:alnum:]'  < /dev/urandom   |  head  -c30
    dq3LYW62eVGZAnZlUPeXbEWlYKK9Dn[root@centos72 ~]#  tr  -dc   '[:alnum:]'  < /dev/urandom   |  head  -c30
    0Q5SLHiu3XeUVuCtIFxfIYwkhRC4No[root@centos72 ~]#  tr  -dc   '[:alnum:]'  < /dev/urandom   |  head  -c30
    MKuhLHDBkAgrtN5oLM9HQADaI6SHGZ[root@centos72 ~]#  tr  -dc   '[:alnum:]'  < /dev/urandom   |  head  -c30
    ZhnykU0EaHzXm8GfojZ9KgXWvfpjRI[root@centos72 ~]#  tr  -dc   '[:alnum:]'  < /dev/urandom   |  head  -c30
    SGRmOMPhc9svC9BKpcclwLgDUp3DC0[root@centos72 ~]# 

    (二)tail显示文本后行内容

    tail [OPTION]... [FILE]...
    -c #:  指定获取后# 字节
    -n #:  指定获取后#行 行
    -#: :
    -f:  跟踪显示文件fd 新追加的内容, 常用日志监控
    于 相当于 --follow=descriptor
    -F:  跟踪文件名,相当于—follow=name --retry

    (1)默认显示后10行

    [root@centos72 ~]# tail   anaconda-ks.cfg 
    
    %addon com_redhat_kdump --disable --reserve-mb='auto'
    
    %end
    
    %anaconda
    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    %end

    支持重定向

    [root@centos72 ~]# cat  anaconda-ks.cfg    -n  |  tail
        42    
        43    %addon com_redhat_kdump --disable --reserve-mb='auto'
        44    
        45    %end
        46    
        47    %anaconda
        48    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
        49    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
        50    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
        51    %end

    (2)-n #:  指定获取后#行

    [root@centos72 ~]# cat  anaconda-ks.cfg    -n  |  tail  -n5
        47    %anaconda
        48    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
        49    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
        50    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
        51    %end

    (3)-f:  跟踪显示文件fd新追加的内容

    常用日志监控相当于 --follow=descriptor,并不会退出,因为文件是时时刻刻变化

     tailf 类似tail –f,当文件不增长时并不访问文件,所以性能更好,更加节省资源

    [root@centos72 ~]# tail  -f  anaconda-ks.cfg 
    
    %addon com_redhat_kdump --disable --reserve-mb='auto'
    
    %end
    
    %anaconda
    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    %end

    开启新终端,并且往文件里面输入内容

    [root@centos72 ~]# echo   asdfghjkl  >>  /app/anaconda-ks.cfg 
    [root@centos72 ~]# echo   aaaaaaaaaa  >>  /app/anaconda-ks.cfg 
    [root@centos72 ~]# echo   bbbbbbbbbb >>  /app/anaconda-ks.cfg 
    [root@centos72 ~]# echo   cccccccccc  >>  /app/anaconda-ks.cfg 

    可以看到文件有变化了,而且是马上显示

    [root@centos72 ~]# tail  -f  /app/anaconda-ks.cfg 
    
    %addon com_redhat_kdump --disable --reserve-mb='auto'
    
    %end
    
    %anaconda
    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    %end
    asdfghjkl
    aaaaaaaaaa
    bbbbbbbbbb
    cccccccccc

    为了不影响执行命令,那么就放到后台执行,那么就不占用现在开启的终端界面

    这样如果有新的情况会显示的

    [root@centos72 ~]# echo   1111111111  >>  /app/anaconda-ks.cfg 
    [root@centos72 ~]# tail  -f  /app/anaconda-ks.cfg   &
    [2] 11919
    [root@centos72 ~]# 
    %anaconda
    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    %end
    asdfghjkl
    aaaaaaaaaa
    bbbbbbbbbb
    cccccccccc
    cat  aaa 
    aa
    bb
    cc
    [root@centos72 ~]# 1111111111
    1111111111

    恢复到前台

    [root@centos72 ~]# fg  1
    tail -f /app/anaconda-ks.cfg
    ^C
    [root@centos72 ~]# tail  -f  /app/anaconda-ks.cfg   
    %anaconda
    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    %end
    asdfghjkl
    aaaaaaaaaa
    bbbbbbbbbb
    cccccccccc
    1111111111

    实现文件如果没有变化就在终端执行其他命令;如果有变化就显示变化,但不影响其他命令的执行

    [root@centos72 ~]# tail  -f  /app/anaconda-ks.cfg   -n0  &
    [3] 11929
    [root@centos72 ~]# 222222
    222222
    ^C
    [root@centos72 ~]# 
    [root@centos72 ~]# echo   222222  >>  /app/anaconda-ks.cfg 

    查看进程是否运行

    [root@centos72 ~]# ps  aux  |  tail
    root       2263  0.0  0.0      0     0 ?        S    13:41   0:02 [kworker/0:0]
    postfix   11664  0.0  0.4  89724  4052 ?        S    14:18   0:00 pickup -l -t unix -u
    root      11878  0.0  0.0      0     0 ?        S    15:12   0:00 [kworker/0:1]
    root      11879  0.0  0.5 154588  5388 ?        Ss   15:13   0:00 sshd: root@pts/0
    root      11883  0.0  0.2 115832  2640 pts/0    Ss   15:13   0:00 -bash
    root      11919  0.0  0.0 107988   612 pts/1    S    15:19   0:00 tail -f /app/anaconda-ks.cfg
    root      11928  0.0  0.0      0     0 ?        S    15:25   0:00 [kworker/0:2]
    root      11929  0.0  0.0 107988   612 pts/1    S    15:26   0:00 tail -f /app/anaconda-ks.cfg -n0
    root      11931  0.0  0.1 155324  1860 pts/0    R+   15:28   0:00 ps aux
    root      11932  0.0  0.0 107984   648 pts/0    R+   15:28   0:00 tail

    (4)-F:  跟踪文件名,相当于—follow=name --retry

    在另外一个终端把文件删除了

    [root@centos72 ~]# rm  -f  /app/f2 

    如果文件删除了就显示文件不可访问

    [root@centos72 ~]# touch   /app/f2
    [root@centos72 ~]# tail  -F   /app/
    anaconda-ks.cfg  f2               
    [root@centos72 ~]# tail  -F   /app/f2 
    tail: ‘/app/f2’ has become inaccessible: No such file or directory

    四按列抽取文本cut

    也就是剪切粘贴

    (一)常见选项

     cut [OPTION]... [FILE]...
    -d DELIMITER:  指明分隔符,默认tab
    -f FILEDS:
    #:  第# 个字段
    #,#[,#] :离散的多个字段,例如1,3,6
    #-# :连续的多个字段,  例如1-6
    混合使用:1-3,7
    -c 按字符切割
    --output-delimiter=STRING

    (二)cut实例

    注意列不一定是表的列,所以要指明什么才是列。比如:冒号分割开的也是列

    [root@centos72 ~]# ls  /app/
    anaconda-ks.cfg  passwd
    [root@centos72 ~]# cat  /app/passwd 
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    wang:x:1000:1000:wang:/home/wang:/bin/bash

    (1)指定分隔符为冒号

    [root@centos72 ~]# cut  -d:  -f1,3  /app/passwd 
    root:0
    bin:1
    daemon:2
    adm:3
    lp:4
    sync:5
    shutdown:6
    halt:7
    mail:8
    operator:11
    games:12
    ftp:14
    nobody:99
    systemd-network:192
    dbus:81
    polkitd:999
    sshd:74
    postfix:89
    wang:1000

     (2)--output-delimiter=STRING 指定输出分隔符

    指定的分隔符可以不加双引号

    [root@centos72 ~]# cut  -d:  -f1,3  /app/passwd  --output-delimiter="======="
    root=======0
    bin=======1
    daemon=======2
    adm=======3
    lp=======4
    sync=======5
    shutdown=======6
    halt=======7
    mail=======8
    operator=======11
    games=======12
    ftp=======14
    nobody=======99
    systemd-network=======192
    dbus=======81
    polkitd=======999
    sshd=======74
    postfix=======89
    wang=======1000
    [root@centos72 ~]# cut  -d:  -f1,3  /app/passwd  --output-delimiter=++++++
    root++++++0
    bin++++++1
    daemon++++++2
    adm++++++3
    lp++++++4
    sync++++++5
    shutdown++++++6
    halt++++++7
    mail++++++8
    operator++++++11
    games++++++12
    ftp++++++14
    nobody++++++99
    systemd-network++++++192
    dbus++++++81
    polkitd++++++999
    sshd++++++74
    postfix++++++89
    wang++++++1000
    [root@centos72 ~]# 
    [root@centos72 ~]# cut  -d:  -f1,3    --output-delimiter=++++++   /app/passwd 
    root++++++0
    bin++++++1
    daemon++++++2
    adm++++++3
    lp++++++4
    sync++++++5
    shutdown++++++6
    halt++++++7
    mail++++++8
    operator++++++11
    games++++++12
    ftp++++++14
    nobody++++++99
    systemd-network++++++192
    dbus++++++81
    polkitd++++++999
    sshd++++++74
    postfix++++++89
    wang++++++1000

    (三)cut示例

    (1)使用cut获取访问日志里面客户端的IP地址

    在生产中cut是很实用的工具

    [root@centos72 ~]# rpm  -q  httpd
    httpd-2.4.6-80.el7.centos.x86_64
    [root@centos72 ~]# systemctl start  httpd
    [root@centos72 ~]# ss -tnl
    State      Recv-Q Send-Q       Local Address:Port                      Peer Address:Port              
    LISTEN     0      128                      *:22                                   *:*                  
    LISTEN     0      100              127.0.0.1:25                                   *:*                  
    LISTEN     0      128                     :::80                                  :::*                  
    LISTEN     0      128                     :::22                                  :::*                  
    LISTEN     0      100                    ::1:25                                  :::*

     把防火墙关闭了

    [root@centos72 ~]# systemctl  stop  firewalld.service 
    [root@centos72 ~]# systemctl  disable    firewalld.service 
    Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    [root@centos72 ~]# setenforce  0

    日志文件,现在没有访问就没有记录

    [root@centos72 ~]# cat  /var/log/httpd/access_log 

     访问此机器的web服务

    [root@centos72 ~]# tailf   /var/log/httpd/access_log 
    192.168.137.1 - - [07/May/2019:18:20:50 +0800] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
    192.168.137.1 - - [07/May/2019:18:20:50 +0800] "GET /noindex/css/bootstrap.min.css HTTP/1.1" 200 19341 "http://192.168.137.72/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
    192.168.137.1 - - [07/May/2019:18:20:50 +0800] "GET /noindex/css/open-sans.css HTTP/1.1" 200 5081 "http://192.168.137.72/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
    192.168.137.1 - - [07/May/2019:18:20:50 +0800] "GET /images/poweredby.png HTTP/1.1" 200 3956 "http://192.168.137.72/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
    192.168.137.1 - - [07/May/2019:18:20:50 +0800] "GET /images/apache_pb.gif HTTP/1.1" 200 2326 "http://192.168.137.72/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
    192.168.137.1 - - [07/May/2019:18:20:50 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
    192.168.137.1 - - [07/May/2019:18:20:50 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.woff HTTP/1.1" 404 241 "http://192.168.137.72/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
    192.168.137.1 - - [07/May/2019:18:20:50 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.woff HTTP/1.1" 404 239 "http://192.168.137.72/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
    192.168.137.1 - - [07/May/2019:18:20:50 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://192.168.137.72/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
    192.168.137.1 - - [07/May/2019:18:20:50 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://192.168.137.72/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"

    使用cut获取客户端的IP地址

    注意空格使用双引号,中间有一个空格

    [root@centos72 ~]# cut   -d" "   -f1  /var/log/httpd/access_log 
    192.168.137.1
    192.168.137.1
    192.168.137.1
    192.168.137.1
    192.168.137.1
    192.168.137.1
    192.168.137.1
    192.168.137.1
    192.168.137.1
    192.168.137.1

    (2)显示访问web网站排名前几的机器

    要使用到统计和排序

    显示的正在连接的客户端IP地址

    [root@centos72 ~]# ss -tn
    State      Recv-Q Send-Q       Local Address:Port                      Peer Address:Port              
    ESTAB      0      52          192.168.137.72:22                       192.168.137.1:57568              
    ESTAB      0      0           192.168.137.72:22                       192.168.137.1:58228 

    删除连续的空格字符,那么多个空格就变成了一个空格

    [root@centos72 ~]# ss -tn  | tr  -s  " "
    State Recv-Q Send-Q Local Address:Port Peer Address:Port 
    ESTAB 0 52 192.168.137.72:22 192.168.137.1:57568 
    ESTAB 0 0 192.168.137.72:22 192.168.137.1:58228

    过滤出IP地址的那行

    [root@centos72 ~]# ss -tn  | tr  -s  " "  |  grep   ESTAB
    ESTAB 0 52 192.168.137.72:22 192.168.137.1:57568 
    ESTAB 0 0 192.168.137.72:22 192.168.137.1:58228

    以空格作为分隔符,并且取第5个字段

    [root@centos72 ~]# ss -tn  | tr  -s  " "  |  grep   ESTAB  |  cut  -d" "   -f5  
    192.168.137.1:57568
    192.168.137.1:58228

    以冒号作为分隔符,取第1个字段

    完成

    [root@centos72 ~]# ss -tn  | tr  -s  " "  |  grep   ESTAB  |  cut  -d" "   -f5   |  cut  -d:  -f1
    192.168.137.1
    192.168.137.1

    法2:

    空格替换成冒号

    注意tr本身就可以进行替换

    [root@centos73 ~]# ss -tn  | tr  -s  " " :
    State:Recv-Q:Send-Q:Local:Address:Port:Peer:Address:Port:
    ESTAB:0:0:192.168.137.73:22:192.168.137.1:49698:
    ESTAB:0:52:192.168.137.73:22:192.168.137.1:58957:

    以冒号作为分隔符,并且取第6个字段

    [root@centos72 ~]# ss -tn  | tr  -s  " " :  |  grep   ESTAB  |  cut  -d:  -f6
    192.168.137.1
    192.168.137.1

    统计连接数,如果一个客户端的连接数多,那么可能是黑客开启多线程就打开了很多的连接,分别测试口令

    把登录的用户名取出

    [root@centos72 ~]# who
    root     tty1         2019-01-13 00:35
    root     pts/0        2019-05-07 15:13 (192.168.137.1)
    root     pts/1        2019-05-07 12:41 (192.168.137.1)
    [root@centos72 ~]# who |  cut  -d" "  -f1
    root
    root
    root

    (3)分区的使用情况

    [root@centos72 ~]# df
    Filesystem     1K-blocks    Used Available Use% Mounted on
    /dev/sda2       52403200 1134820  51268380   3% /
    devtmpfs          487952       0    487952   0% /dev
    tmpfs             498976       0    498976   0% /dev/shm
    tmpfs             498976   14108    484868   3% /run
    tmpfs             498976       0    498976   0% /sys/fs/cgroup
    /dev/sda3       20961280   32952  20928328   1% /app
    /dev/sda1        1038336  126596    911740  13% /boot
    tmpfs              99796       0     99796   0% /run/user/0
    /dev/sr0         4364408 4364408         0 100% /mnt

    取出分区利用率,如果利用率达到了多少就要报警了

    [root@centos72 ~]# df  |  grep  /dev/sd
    /dev/sda2       52403200 1134820  51268380   3% /
    /dev/sda3       20961280   32952  20928328   1% /app
    /dev/sda1        1038336  126596    911740  13% /boot
    [root@centos72 ~]# df  |  grep  /dev/sd  |  tr  -s  ' '   
    /dev/sda2 52403200 1134820 51268380 3% /
    /dev/sda3 20961280 32952 20928328 1% /app
    /dev/sda1 1038336 126596 911740 13% /boot
    [root@centos72 ~]# df  |  grep  /dev/sd  |  tr  -s  ' '   |  cut  -d" "  -f5
    3%
    1%
    13%

    去除%

    [root@centos72 ~]# df  |  grep  /dev/sd  |  tr  -s  ' '   |  cut  -d" "  -f5  | cut  -d%  -f1
    3
    1
    13

     法2:

    搜索的时候使用%替换

    [root@centos72 ~]# df  |  grep  /dev/sd  |  tr  -s  ' '  % |  cut  -d%   -f5
    3
    1
    13

    (4)取出网卡对应的IP地址

    [root@centos72 ~]# ifconfig 
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.137.72  netmask 255.255.255.0  broadcast 192.168.137.255
            inet6 fe80::b029:2522:876f:5456  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:fc:69:f8  txqueuelen 1000  (Ethernet)
            RX packets 16994  bytes 1708821 (1.6 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 16198  bytes 16210329 (15.4 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 1000  (Local Loopback)
            RX packets 10  bytes 680 (680.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 10  bytes 680 (680.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    [root@centos72 ~]# ifconfig   ens33
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.137.72  netmask 255.255.255.0  broadcast 192.168.137.255
            inet6 fe80::b029:2522:876f:5456  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:fc:69:f8  txqueuelen 1000  (Ethernet)
            RX packets 17017  bytes 1710815 (1.6 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 16213  bytes 16212741 (15.4 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    法1:

    显示前面2行

    [root@centos72 ~]# ifconfig   ens33  |  head  -n2 
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.137.72  netmask 255.255.255.0  broadcast 192.168.137.255

    显示前面2行的倒数第1行,惯用的套路

    [root@centos72 ~]#  ifconfig   ens33  |  head  -n2  | tail  -n1
            inet 192.168.137.72  netmask 255.255.255.0  broadcast 192.168.137.255

    以空格作为分割符,删除了多余的空格,只有一个空格

    [root@centos72 ~]#  ifconfig   ens33  |  head  -n2  | tail  -n1 | tr  -s  ' ' 
     inet 192.168.137.72 netmask 255.255.255.0 broadcast 192.168.137.255

    以空格作为分隔符,取第3个字段

    [root@centos72 ~]# ifconfig   ens33  |  head  -n2  | tail  -n1 | tr  -s  ' '  |   cut  -d" "   -f3
    192.168.137.72

    法2:

    直接把分隔符空格变成冒号

    [root@centos72 ~]# ifconfig   ens33  |  head  -n2  | tail  -n1 | tr  -s  ' ' :   |  cut  -d:  -f3
    192.168.137.72

    法3:

    过滤出关键字grep netmask

    tr  -s  ' ' 表示把多个空格压缩为1个空格

    [root@centos72 ~]# ifconfig   ens33  | grep  netmask
            inet 192.168.137.72  netmask 255.255.255.0  broadcast 192.168.137.255
    [root@centos72 ~]# ifconfig   ens33  | grep  netmask  |  tr  -s  ''
            inet 192.168.137.72  netmask 255.255.255.0  broadcast 192.168.137.255
    [root@centos72 ~]# ifconfig   ens33  | grep  netmask  |  tr  -s  ' '
     inet 192.168.137.72 netmask 255.255.255.0 broadcast 192.168.137.255
    [root@centos72 ~]# ifconfig   ens33  | grep  netmask  |  tr  -s  ' '  |  cut  -d" "  -f3
    192.168.137.72

    取文件的第1列到第3列,第7列

    [root@centos72 ~]# cat  /app/passwd   |  cut  -d:  -f1-3,7
    root:x:0:/bin/bash
    bin:x:1:/sbin/nologin
    daemon:x:2:/sbin/nologin
    adm:x:3:/sbin/nologin
    lp:x:4:/sbin/nologin
    sync:x:5:/bin/sync
    shutdown:x:6:/sbin/shutdown
    halt:x:7:/sbin/halt
    mail:x:8:/sbin/nologin
    operator:x:11:/sbin/nologin
    games:x:12:/sbin/nologin
    ftp:x:14:/sbin/nologin
    nobody:x:99:/sbin/nologin
    systemd-network:x:192:/sbin/nologin
    dbus:x:81:/sbin/nologin
    polkitd:x:999:/sbin/nologin
    sshd:x:74:/sbin/nologin
    postfix:x:89:/sbin/nologin
    wang:x:1000:/bin/bash

    五合并文件paste

    paste  合并两个文件同行号的列到一行
    paste [OPTION]... [FILE]...
    -d  分隔符: 指定分隔符,默认用TAB
    -s :  所有行合成一行显示
    paste f1 f2
    paste -s f1 f2

    使用cat同时打印文件是横向合并

    [root@centos72 ~]# cp  /etc/issue  /app/
    [root@centos72 ~]# cp  /etc/centos-release    /app/
    [root@centos72 ~]# cat  /app/issue   /app/centos-release 
    S
    Kernel 
     on an m
    
    CentOS Linux release 7.5.1804 (Core) 

    进行纵向合并

    [root@centos72 ~]# paste   /app/issue   /app/centos-release 
    S    CentOS Linux release 7.5.1804 (Core) 
    Kernel 
     on an m    
        
    [root@centos72 ~]# cat  /app/issue 
    S
    Kernel 
     on an m
    
    [root@centos72 ~]# cat  /app/centos-release 
    CentOS Linux release 7.5.1804 (Core) 
    

    默认是使用制表符作为分割符

    [root@centos72 ~]# paste   /app/issue   /app/centos-release   >   /app/f3
    [root@centos72 ~]# cat  -A    /app/f3
    S^ICentOS Linux release 7.5.1804 (Core) $
    Kernel 
     on an m^I$
    ^I$

    指定分隔符,用法和tr,cut类似

    [root@centos72 ~]# paste  -d:  /app/f3
    S    CentOS Linux release 7.5.1804 (Core) 
    Kernel 
     on an m    
        
    [root@centos72 ~]# paste   /app/issue   /app/centos-release 
    S    CentOS Linux release 7.5.1804 (Core) 
    Kernel 
     on an m    
        
    [root@centos72 ~]# paste  -d:   /app/issue   /app/centos-release 
    S:CentOS Linux release 7.5.1804 (Core) 
    Kernel 
     on an m:
    :

    注意如果分隔符是多个相同符号只显示一个

    [root@centos72 ~]# paste  -d=====  /app/issue   /app/centos-release 
    S=CentOS Linux release 7.5.1804 (Core) 
    Kernel 
     on an m=
    =
    [root@centos72 ~]# paste  -d"====="  /app/issue   /app/centos-release 
    S=CentOS Linux release 7.5.1804 (Core) 
    Kernel 
     on an m=
    =

    -s :  所有行合成一行显示

    [root@centos72 ~]# paste -s    /app/issue   /app/centos-release 
    S    Kernel 
     on an m    
    CentOS Linux release 7.5.1804 (Core) 
    [root@centos72 ~]# cat     /app/issue 
    S
    Kernel 
     on an m
    
    [root@centos72 ~]# cat     /app/centos-release 
    CentOS Linux release 7.5.1804 (Core) 

    创建两个文件

    [root@centos72 ~]# ls  /app/
    anaconda-ks.cfg  centos-release  f3  issue  passwd
    [root@centos72 ~]# cat >  /app/f1
    aa
    bb
    cc
    dd
    ^C^C
    [root@centos72 ~]# cat  /app/f1
    aa
    bb
    cc
    dd
    [root@centos72 ~]# cat >  /app/f2
    11
    22
    33
    44
    ^C
    [root@centos72 ~]# cat  /app/f2
    11
    22
    33
    44

    相当于逆时针转了90度,对于图案的调整是很好的

    [root@centos72 ~]# paste  /app/f1  /app/f2
    aa    11
    bb    22
    cc    33
    dd    44
    [root@centos72 ~]# paste  /app/f1    /app/f2
    aa    11
    bb    22
    cc    33
    dd    44

    所有同列的行合成一行显示

    [root@centos72 ~]# paste  -s   /app/f1    /app/f2
    aa    bb    cc    dd
    11    22    33    44


    作者:wang618
    出处:https://www.cnblogs.com/wang618/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

  • 相关阅读:
    JS获取本周、本季度、本月、上月的开始日期、结束日期
    《大教堂和集市》笔记——为什么一个本科生业余作品却成了全世界最流行的操作系统之一Linux?
    “平头哥”半导体公司
    阻挡不住的变化
    给我五分钟,带你走出迷茫
    涂子沛:从幼稚到成熟,我们这个时代的数据革命
    统计思维就是透过现象看本质
    十个有趣的“大数据”挖掘案例
    信息时代与大数据相关的8个专业
    大数据招聘分析
  • 原文地址:https://www.cnblogs.com/wang618/p/11062785.html
Copyright © 2011-2022 走看看