zoukankan      html  css  js  c++  java
  • 扩展正则表达式

    一egrep及扩展的正则表达式

    正则表达式对于转义要写斜线,写起来比较麻烦

    扩展的正则表达式和基本正则表达式区别在于不需要加了

    egrep = grep -E
    egrep [OPTIONS] PATTERN [FILE...]
    扩展正则表达式的元字符:
    字符匹配:
    . 任意单个字符

    [ ] 指定范围的字符
    [^] 不在指定范围的字符

    注意下面全部都少了

    次数匹配:
    * :匹配前面字符任意次
    ?: 0 或1次 次
    + :1 次或多次
    {m} :匹配m次 次
    {m,n} :至少m ,至多n次

    位置锚定:
    ^ : 行首
    $ : 行尾
    <,  : 语首
    >,  : 语尾
    分组:
    ()
    后向引用:1, 2, ...
    或者:
    a|b: a 或b
    C|cat: C 或cat
    (C|c)at:Cat 或cat

    示例一

     1 、显示/proc/meminfo 文件中以大小s 开头的行( 要求:使用两种方法)

    法1:

    [root@centos72 ~]# cat  /proc/meminfo  |  grep  -i   "^s.*"
    SwapCached:            0 kB
    SwapTotal:       2097148 kB
    SwapFree:        2097148 kB
    Shmem:              7796 kB
    Slab:              67184 kB
    SReclaimable:      29520 kB
    SUnreclaim:        37664 kB

    法2:

    [root@centos72 ~]# cat  /proc/meminfo  |  grep  -i   "^[Ss]"
    SwapCached:            0 kB
    SwapTotal:       2097148 kB
    SwapFree:        2097148 kB
    Shmem:              7796 kB
    Slab:              67184 kB
    SReclaimable:      29520 kB
    SUnreclaim:        37664 kB

    法3:

    [root@centos72 ~]# cat  /proc/meminfo  |  grep  -i   "^s"
    SwapCached:            0 kB
    SwapTotal:       2097148 kB
    SwapFree:        2097148 kB
    Shmem:              7796 kB
    Slab:              67184 kB
    SReclaimable:      29520 kB
    SUnreclaim:        37664 kB

    法4:

    上面都是忽略大小写,还可以进行分组,也就意味着写出所有的可能

     2 、显示/etc/passwd 文件中不以/bin/bash结尾的行

    逆向思维,先显示以/bin/bash结尾的行,再取反

    [root@centos72 ~]# cat /etc/passwd  |  grep -v  "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
    apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    
    [root@centos72 ~]# cat /etc/passwd  |  grep -v  "/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
    apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

    3 、显示wang账号默认的shell 程序

     法1:

    -w :匹配整个单词

    [root@centos72 ~]#  cat /etc/passwd  |  grep -w   "wang"
    wang:x:1000:1000:wang:/home/wang:/bin/bash
    [root@centos72 ~]#  cat /etc/passwd  |  grep -w   "wang"   | cut  -d:  -f7
    /bin/bash

    注意用户名是在文件的行首

    法2,更严谨的写法

    加上w是精确匹配

    [root@centos72 ~]# cat /etc/passwd  |  grep    -w   "^wang"  
    wang:x:1000:1000:wang:/home/wang:/bin/bash
    [root@centos72 ~]# cat /etc/passwd  |  grep    -w   "^wang"  | cut  -d:  -f7
    /bin/bash

    法3:

    完全匹配,词首和词尾都锚定

    [root@centos72 ~]#  cat /etc/passwd  |  grep -w   "<wang>"   | cut  -d:  -f7
    /bin/bash

    法4:

    脱义字符和词尾结合

    [root@centos72 ~]#  cat /etc/passwd  |  grep -w   "^wang>" 
    wang:x:1000:1000:wang:/home/wang:/bin/bash
    [root@centos72 ~]#  cat /etc/passwd  |  grep -w   "^wang>"   | cut  -d:  -f7
    /bin/bash

    法5:

    最简便的方法,只用一个管道

    [root@centos72 ~]# grep /etc/passwd  -w   "^wang>"  /etc/passwd  | cut  -d:  -f7
    grep: ^wang>: No such file or directory
    [root@centos72 ~]# grep  -w   "^wang>"  /etc/passwd  | cut  -d:  -f7
    /bin/bash

    4 、找出/etc/passwd中的两位或三位数

    法1

    注意要锚定词首词尾,都是在前面的,因为ID可能是四位数,五位数,会匹配两三位数

    [root@centos72 ~]# grep  -o   "<[0-9]{2,3}>"  /etc/passwd
    12
    11
    12
    100
    14
    50
    99
    99
    192
    192
    81
    81
    999
    998
    74
    74
    89
    89
    48
    48

    法2

    [[:digit:]]和[0-9]等价的

    写法不同,意思相同,就像中国可以写成CHINA

    [root@centos72 ~]# grep  -o   "<[[:digit:]]{2,3}>"  /etc/passwd
    12
    11
    12
    100
    14
    50
    99
    99
    192
    192
    81
    81
    999
    998
    74
    74
    89
    89
    48
    48
    [root@centos72 ~]# grep  -o   "<[[:digit:]]{2,3}>"  /etc/passwd | wc
         20      20      65
    [root@centos72 ~]# grep  -o   "<[0-9]{2,3}>"  /etc/passwd  | wc
         20      20      65

    反例,没锚定的结果

    [root@centos72 ~]# echo   123456789  | grep  -o  "[0-9]{2,3}"
    123
    456
    789
    [root@centos72 ~]# echo   123456789  | grep  "[0-9]{2,3}"
    123456789

    正确的写法是搜索不出来的

    [root@centos72 ~]# echo   123456789  | grep  -o  "<[0-9]{2,3}>"
    [root@centos72 ~]# echo   123456789  | grep  -o "<[[:digit:]]{2,3}>"

    只有两位数和三位数可以

    [root@centos72 ~]# echo   12 | grep  -o "<[[:digit:]]{2,3}>"
    12
    [root@centos72 ~]# echo   123 | grep  -o "<[[:digit:]]{2,3}>"
    123

     5 、显示CentOS7 的/etc/grub2.cfg 文件中,至少以一个空白字符开头的且后面存非空白字符的行

    法1:

    使用space范围更广

    [root@centos72 ~]# grep  "^[[:space:]]+[^[:space:]]"  /etc/grub2.cfg 
      load_env
       set default="${next_entry}"
       set next_entry=
       save_env next_entry
       set boot_once=true
       set default="${saved_entry}"
      menuentry_id_option="--id"
      menuentry_id_option=""
      set saved_entry="${prev_saved_entry}"
      save_env saved_entry
      set prev_saved_entry=
      save_env prev_saved_entry
      set boot_once=true
      if [ -z "${boot_once}" ]; then
        saved_entry="${chosen}"
        save_env saved_entry
      fi
      if [ x$feature_all_video_module = xy ]; then
        insmod all_video
      else
        insmod efi_gop
        insmod efi_uga
        insmod ieee1275_fb
        insmod vbe
        insmod vga
        insmod video_bochs
        insmod video_cirrus
      fi
      set timeout_style=menu
      set timeout=5
      set timeout=5
      source ${prefix}/user.cfg
      if [ -n "${GRUB2_PASSWORD}" ]; then
        set superusers="root"
        export superusers
        password_pbkdf2 root ${GRUB2_PASSWORD}
      fi
        load_video
        set gfxpayload=keep
        insmod gzio
        insmod part_msdos
        insmod xfs
        set root='hd0,msdos1'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  92886c3f-42a3-40f4-8cf7-c6890ca3a52e
        else
          search --no-floppy --fs-uuid --set=root 92886c3f-42a3-40f4-8cf7-c6890ca3a52e
        fi
        linux16 /vmlinuz-3.10.0-862.el7.x86_64 root=UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 ro rhgb quiet LANG=en_US.UTF-8
        initrd16 /initramfs-3.10.0-862.el7.x86_64.img
        load_video
        insmod gzio
        insmod part_msdos
        insmod xfs
        set root='hd0,msdos1'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  92886c3f-42a3-40f4-8cf7-c6890ca3a52e
        else
          search --no-floppy --fs-uuid --set=root 92886c3f-42a3-40f4-8cf7-c6890ca3a52e
        fi
        linux16 /vmlinuz-0-rescue-cb26ac281315402a9928e9a4c3bedfcd root=UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 ro rhgb quiet
        initrd16 /initramfs-0-rescue-cb26ac281315402a9928e9a4c3bedfcd.img
      source ${config_directory}/custom.cfg
      source $prefix/custom.cfg;

    法2:

    [root@centos72 ~]# grep  "^[[:blank:]]+[^[:blank:]]"  /etc/grub2.cfg 
      load_env
       set default="${next_entry}"
       set next_entry=
       save_env next_entry
       set boot_once=true
       set default="${saved_entry}"
      menuentry_id_option="--id"
      menuentry_id_option=""
      set saved_entry="${prev_saved_entry}"
      save_env saved_entry
      set prev_saved_entry=
      save_env prev_saved_entry
      set boot_once=true
      if [ -z "${boot_once}" ]; then
        saved_entry="${chosen}"
        save_env saved_entry
      fi
      if [ x$feature_all_video_module = xy ]; then
        insmod all_video
      else
        insmod efi_gop
        insmod efi_uga
        insmod ieee1275_fb
        insmod vbe
        insmod vga
        insmod video_bochs
        insmod video_cirrus
      fi
      set timeout_style=menu
      set timeout=5
      set timeout=5
      source ${prefix}/user.cfg
      if [ -n "${GRUB2_PASSWORD}" ]; then
        set superusers="root"
        export superusers
        password_pbkdf2 root ${GRUB2_PASSWORD}
      fi
        load_video
        set gfxpayload=keep
        insmod gzio
        insmod part_msdos
        insmod xfs
        set root='hd0,msdos1'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  92886c3f-42a3-40f4-8cf7-c6890ca3a52e
        else
          search --no-floppy --fs-uuid --set=root 92886c3f-42a3-40f4-8cf7-c6890ca3a52e
        fi
        linux16 /vmlinuz-3.10.0-862.el7.x86_64 root=UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 ro rhgb quiet LANG=en_US.UTF-8
        initrd16 /initramfs-3.10.0-862.el7.x86_64.img
        load_video
        insmod gzio
        insmod part_msdos
        insmod xfs
        set root='hd0,msdos1'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  92886c3f-42a3-40f4-8cf7-c6890ca3a52e
        else
          search --no-floppy --fs-uuid --set=root 92886c3f-42a3-40f4-8cf7-c6890ca3a52e
        fi
        linux16 /vmlinuz-0-rescue-cb26ac281315402a9928e9a4c3bedfcd root=UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 ro rhgb quiet
        initrd16 /initramfs-0-rescue-cb26ac281315402a9928e9a4c3bedfcd.img
      source ${config_directory}/custom.cfg
      source $prefix/custom.cfg;
    
    [root@centos72 ~]# grep  "^[[:blank:]]+[^[:blank:]]"  /etc/grub2.cfg  | wc
         64     167    2089
    [root@centos72 ~]# grep  "^[[:space:]]+[^[:space:]]"  /etc/grub2.cfg  | wc
         64     167    2089

    6 、找出“netstat -tan” 命令的结果中以‘LISTEN’ 后跟任意多个空白字符结尾的行

    锚定的空白字符,如果后面是tab键就不能匹配了

    注意空白符要考虑到tab键

    [root@centos72 ~]# netstat -tan  |  grep   "LISTEN *$"
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
    tcp6       0      0 :::22                   :::*                    LISTEN     
    tcp6       0      0 ::1:25                  :::*                    LISTEN  

    法1

    [root@centos72 ~]# netstat -tan  |  grep   "LISTEN[[:space:]]*$"
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
    tcp6       0      0 :::22                   :::*                    LISTEN     
    tcp6       0      0 ::1:25                  :::*                    LISTEN    

    法2:

    [root@centos72 ~]# netstat -tan  |  grep   "LISTEN[[:blank:]]*$"
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
    tcp6       0      0 :::22                   :::*                    LISTEN     
    tcp6       0      0 ::1:25                  :::*                    LISTEN 

    7 、显示CentOS7 上所有系统用户的用户名和UID

    [root@centos72 ~]# cat /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
    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
    apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    rooter:x:1001:1001::/home/rooter:/bin/bash

    先切再找,这样就可以保证切出来的UID

    [root@centos72 ~]# cat /etc/passwd  | cut -d: -f1,3 
    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
    apache:48
    rooter:1001

    如果是先找,那么就把GID也显示了

    [root@centos72 ~]# cat /etc/passwd   | grep "<[[:digit:]]{1,3}"
    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
    apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    rooter:x:1001:1001::/home/rooter:/bin/bash

     法1:

    {1,3}表示最少一个数,最多3个数

    注意要锚定词首和行尾

    行首行尾是不能的,但是不能锚定词首词尾,因为如果用户名就是字符串,也会显示此用户

    锚定行尾确保就是UID

    [root@centos72 ~]# cut -d: -f1,3 /etc/passwd |grep "<[[:digit:]]{1,3}$"
    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
    apache:48

    法2:

    [root@centos72 ~]# cut -d: -f1,3 /etc/passwd |grep "<[0-9]{1,3}$"
    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
    apache:48

    创建一个账号

    [root@centos72 ~]# id  123
    uid=1002(123) gid=1002(123) groups=1002(123)

    不能锚定词首词尾,因为如果用户名就是字符串,也会显示此用户

    而此用户不是系统用户

    [root@centos72 ~]# cat /etc/passwd  | cut -d: -f1,3 | grep "<[[:digit:]]{1,3}>"
    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
    apache:48
    123:1002
    [root@centos72 ~]# cut -d: -f1,3 /etc/passwd |grep "<[0-9]{1,3}>"
    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
    apache:48
    123:1002

    8 、 找出/etc/passwd 中用户名和shell名称一样的行

    比较难

    使用到了后向引用

    显示从行首开始的信息

    [root@centos72 ~]# grep "^(.*)" /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
    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
    apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    rooter:x:1001:1001::/home/rooter:/bin/bash
    123:x:1002:1002::/home/123:/bin/bash

    还是不严谨

    [root@centos72 ~]# grep "^(.*):.*1$" /etc/passwd
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt

    法1:

    还要锚定词首

    ^(.*):

    使用到了行首和词尾,这样就可以取出一个单词,而且直接写出了单词的分隔符是冒号:

    <1$

    使用到了词首和行尾,这样就可以取出一个单词,而且单词的分隔符是/

    注意在文件里面出现了2个单词的分隔符,冒号:和斜线反斜线/

    [root@centos72 ~]# grep "^(.*):.*<1$" /etc/passwd
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt

    法2:

    ^(.*)>

    使用到了行首和词尾,这样就可以取出一个单词,而且单词的分隔符是冒号:

    <1$

    使用到了词首和行尾,这样就可以取出一个单词,而且单词的分隔符是/

    注意在文件里面出现了2个单词的分隔符,冒号:和斜线反斜线/

    [root@centos72 ~]# grep "^(.*)>.*<1$" /etc/passwd
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt

     9 、利用df 和grep取出磁盘各分区利用率,并从大到小排序

    执行过程

    [root@centos72 ~]# df | grep  /dev/sd 
    /dev/sda2       52403200 1134844  51268356   3% /
    /dev/sda3       20961280   32944  20928336   1% /app
    /dev/sda1        1038336  126596    911740  13% /boot
    [root@centos72 ~]# df | grep  /dev/sd | tr -s ' '
    /dev/sda2 52403200 1134844 51268356 3% /
    /dev/sda3 20961280 32944 20928336 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

    最终命令

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

    法1:

    先把硬盘分区过滤出来

    [root@centos72 ~]# df  |grep "/dev/sd" 
    /dev/sda2       52403200 1188336  51214864   3% /
    /dev/sda3       20961280   32980  20928300   1% /app
    /dev/sda1        1038336  126596    911740  13% /boot

     加上%确保数字就是加在百分号后面的。因为数字很多,要找到不同点,精确匹配

    [root@centos72 ~]# df  |grep "/dev/sd" |grep -o "<[[:digit:]]{1,3}%"
    3%
    1%
    13%

    最后过滤出不带%的

    [root@centos72 ~]# df  |grep "/dev/sd" |grep -o "<[[:digit:]]{1,3}%" |grep -o "[[:digit:]]{1,3}" |sort -nr
    13
    3
    1

    法2:

    和法1差不多的

    [root@centos72 ~]# df  |grep "/dev/sd" |grep -o "<[0-9]{1,3}%" |grep -o "[0-9]{1,3}" |sort -nr
    13
    3
    1

    注意要考虑到其他的条件是否是一位数到三位数,比如可用空间,以免显示了不需要显示的内容

    [root@centos72 ~]# df
    Filesystem     1K-blocks    Used Available Use% Mounted on
    /dev/sda2       52403200 1188336  51214864   3% /
    devtmpfs          487952       0    487952   0% /dev
    tmpfs             498976       0    498976   0% /dev/shm
    tmpfs             498976    7796    491180   2% /run
    tmpfs             498976       0    498976   0% /sys/fs/cgroup
    /dev/sda3       20961280   32980  20928300   1% /app
    /dev/sda1        1038336  126596    911740  13% /boot
    /dev/sr0         4364408 4364408         0 100% /mnt
    tmpfs              99796       0     99796   0% /run/user/0

    如果分区的利用率大于几就报警

    [root@centos72 ~]# df  |grep "/dev/sd" |grep -o "<[0-9]{1,3}%" |grep -o "[0-9]{1,3}" |sort -nr | head  -n1
    13

    10使用正则表达式取出IP地址

    先分析IP地址的特点是最少是一位数,最多是三位数

    法1:

    =1900表示出第一个数字

    [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 5205  bytes 451782 (441.1 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 9182  bytes 13571409 (12.9 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    ifconfig ens33  | grep -o "([0-9]{1,3}

    后面加上点,因为有在正则表达式里面特殊含义,任意一个字符,所以要对其进行转义

    ifconfig ens33  | grep -o "([0-9]{1,3}.

    进行分组,并且重复3次,也就是显示了192.168.137.

    注意显示了3个点

    ifconfig ens33  | grep -o "([0-9]{1,3}.){3}
    [root@centos72 ~]# ifconfig ens33  | grep -o "([0-9]{1,3}.){3}[0-9]{1,3}"  |  head -n1
    192.168.137.72

     192.168.137.可以归纳为xxx.xxx.xxx.

    法2:使用扩展的正则表达式,去掉反斜线

    [root@centos72 ~]# ifconfig ens33 |grep -Eo  "([0-9]{1,3}.){3}[0-9]{1,3}" |  head -n1
    192.168.137.72

    示例二

    下面都是扩展的正则表达式

    1使用egrep 取出/etc/rc.d/init.d/functions 中其基名,也就是去除路径部分的文件名functions

    查看老命令72

    [root@centos72 ~]# basename   /etc/issue
    issue
    [root@centos72 ~]# basename   /etc/passwd
    passwd
    [root@centos72 ~]# basename   /etc/services 
    services

    注意结构

    [^/]+$:表示查找1 次或多次不带反斜线到行尾之间的内容

    [root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o   "[^/]+$"  
    functions

    假设是下面这种情况,就是基名带斜线,比如/etc/rc.d/init.d/

    ?: 0 或1次 ,

    那么/?表示此斜线可有可无,这样两种情况都照顾到了

    [root@centos72 ~]# echo "/etc/rc.d/init.d/" | egrep -o  "[^/]+/?$"  
    init.d/
    [root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o  "[^/]+/?$"  
    functions

    基本正则表达式,加上斜线转义

    [root@centos72 ~]# echo "/etc/rc.d/init.d/functions"  | grep -o  "[^/]+/?$"  
    functions

    2使用egrep取出/etc/rc.d/init.d/functions的目录名,也就/etc/rc.d/init.d/

    [root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o  "^/.*/"  
    /etc/rc.d/init.d/

    ^/表示行首是/,没加无法匹配

    [root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o  "/.*/$"  

    不保留目录的/

    先取出带目录的

    [root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o  "^/.*/"
    /etc/rc.d/init.d/

    再进行过滤

    [root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o  "^/.*/" | grep -o  "^.*[^/]"
    /etc/rc.d/init.d
    [root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o  "^/.*/" | grep -o  "^/.*[^/]"
    /etc/rc.d/init.d

    如果就是目录,要取上一级上面的就不适用了

    [root@centos72 ~]# echo "/etc/rc.d/init.d/" |egrep -o  "^/.*/"
    /etc/rc.d/init.d/


    3显示三个用户root、ftp、wang的UID和默认shell类型

    首先要进行分组,并且用户是第1个字段,要锚定行首

    [root@centos72 ~]#  cat /etc/passwd |grep "(^root|^ftp|^wang)" 
    root:x:0:0:root:/root:/bin/bash
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    wang:x:1000:1000:wang:/home/wang:/bin/bash

    以冒号作为分割符,取出两个字段用户的UID和默认shell类型

    [root@centos72 ~]#  cat /etc/passwd |grep "(^root|^ftp|^wang)"  |  cut -d ":" -f3,7
    0:/bin/bash
    14:/sbin/nologin
    1000:/bin/bash


    4找出/etc/rc.d/init.d/functions文件中行首为某单词或者下划线后面跟一个小括号的行

     注意中间也可以出现下划线

    *表示匹配前面字符任意次,也就是可以出现0次到多次

    [root@centos72 ~]# cat /etc/rc.d/init.d/functions |egrep "^(_|[[:alpha:]])[[:alnum:]_]*[[:space:]]*()" -o
    systemctl_redirect ()
    checkpid()
    __kill_pids_term_kill_checkpids()
    __kill_pids_term_kill()
    __pids_var_run()
    __pids_pidof()
    daemon()
    killproc()
    pidfileofproc()
    pidofproc()
    status()
    echo_success()
    echo_failure()
    echo_passed()
    echo_warning()
    update_boot_stage()
    success()
    failure()
    passed()
    warning()
    action()
    strstr()
    is_ignored_file()
    convert2sec()
    is_true()
    is_false()
    apply_sysctl()


    5、统计last命令中以root登录的每个主机IP地址登录次数

    [root@centos72 ~]# last
    root     pts/0        192.168.137.1    Mon Jun 24 16:19   still logged in   
    reboot   system boot  3.10.0-862.el7.x Mon Jun 24 15:40 - 12:32  (20:52)    
    root     pts/0        192.168.137.1    Sun Jun 23 12:23 - crash (1+03:16)   
    reboot   system boot  3.10.0-862.el7.x Sun Jun 23 12:21 - 12:32 (2+00:10)   
    root     pts/0        192.168.137.1    Sat Jun 22 16:43 - 10:55  (18:11)    
    root     pts/0        192.168.137.1    Sat Jun 22 16:41 - 16:42  (00:00)    
    root     pts/1        192.168.137.1    Sat Jun 22 16:37 - 16:39  (00:01)    
    root     pts/0        gateway          Sat Jun 22 16:28 - 16:39  (00:11)    
    reboot   system boot  3.10.0-862.el7.x Sat Jun 22 16:24 - 12:32 (2+20:07)   
    root     pts/1        gateway          Fri Jun 21 09:16 - 12:46 (1+03:30)   
    root     pts/0        192.168.137.2    Sun Jan 13 00:48 - 11:25 (159+10:37) 
    root     pts/0        192.168.137.2    Sun Jan 13 00:46 - 00:48  (00:02)    
    root     tty1                          Sun Jan 13 00:35 - 12:47 (160+12:12) 
    reboot   system boot  3.10.0-862.el7.x Sun Jan 13 00:34 - 12:48 (160+12:13) 
    
    wtmp begins Sun Jan 13 00:34:55 2019

    锚定行首和词尾以确定是第1个字段

    [root@centos72 ~]# last | grep "^root"
    root     pts/0        192.168.137.1    Mon Jun 24 16:19   still logged in   
    root     pts/0        192.168.137.1    Sun Jun 23 12:23 - crash (1+03:16)   
    root     pts/0        192.168.137.1    Sat Jun 22 16:43 - 10:55  (18:11)    
    root     pts/0        192.168.137.1    Sat Jun 22 16:41 - 16:42  (00:00)    
    root     pts/1        192.168.137.1    Sat Jun 22 16:37 - 16:39  (00:01)    
    root     pts/0        gateway          Sat Jun 22 16:28 - 16:39  (00:11)    
    root     pts/1        gateway          Fri Jun 21 09:16 - 12:46 (1+03:30)   
    root     pts/0        192.168.137.2    Sun Jan 13 00:48 - 11:25 (159+10:37) 
    root     pts/0        192.168.137.2    Sun Jan 13 00:46 - 00:48  (00:02)    
    root     tty1                          Sun Jan 13 00:35 - 12:47 (160+12:12)
    [root@centos72 ~]# last | grep "^root>"
    root     pts/0        192.168.137.1    Mon Jun 24 16:19   still logged in   
    root     pts/0        192.168.137.1    Sun Jun 23 12:23 - crash (1+03:16)   
    root     pts/0        192.168.137.1    Sat Jun 22 16:43 - 10:55  (18:11)    
    root     pts/0        192.168.137.1    Sat Jun 22 16:41 - 16:42  (00:00)    
    root     pts/1        192.168.137.1    Sat Jun 22 16:37 - 16:39  (00:01)    
    root     pts/0        gateway          Sat Jun 22 16:28 - 16:39  (00:11)    
    root     pts/1        gateway          Fri Jun 21 09:16 - 12:46 (1+03:30)   
    root     pts/0        192.168.137.2    Sun Jan 13 00:48 - 11:25 (159+10:37) 
    root     pts/0        192.168.137.2    Sun Jan 13 00:46 - 00:48  (00:02)    
    root     tty1                          Sun Jan 13 00:35 - 12:47 (160+12:12) 

    以空格作为分割符

    [root@centos72 ~]#  last | grep "^root>" | cut -d " " -f1,14 
    root 192.168.137.1
    root 192.168.137.1
    root 192.168.137.1
    root 192.168.137.1
    root 192.168.137.1
    root gateway
    root gateway
    root 192.168.137.2
    root 192.168.137.2
    root 

     sort -t " " -k2表示分隔符是空格,-k表示列

    按照第2列进行排序

    [root@centos72 ~]#  last | grep "^root>" | cut -d " " -f1,14 |   sort -t " " -k2
    root 
    root 192.168.137.1
    root 192.168.137.1
    root 192.168.137.1
    root 192.168.137.1
    root 192.168.137.1
    root 192.168.137.2
    root 192.168.137.2
    root gateway
    root gateway

    -c:  显示每行重复出现的次数

    [root@centos72 ~]#  last | grep "^root" |cut -d " " -f1,14 |sort -t " " -k2 |uniq -c
          1 root 
          5 root 192.168.137.1
          2 root 192.168.137.2
          2 root gateway

     按照数字进行排序

    [root@centos72 ~]# last | grep "^root" |cut -d " " -f1,14 |sort -t " " -k2 |uniq -c | sort -rn
          5 root 192.168.137.1
          2 root gateway
          2 root 192.168.137.2
          1 root 


    6、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255

     0-9

    [root@centos72 ~]#  echo {1..1000} | egrep -o "[0-9]"
    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@centos72 ~]#  echo {1..1000} | egrep -o "<[0-9]>"
    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@centos72 ~]#  echo {1..1000} | egrep -o "<[0-9]>" | tr "
    " " " 
    1 2 3 4 5 6 7 8 9 [root@centos72 ~]# 
    [root@centos72 ~]#  echo {1..1000} | egrep -o "<[0-9]>" | tr "
    " " " ;echo
    1 2 3 4 5 6 7 8 9 

    10-99

    [root@centos72 ~]# echo {1..1000} | egrep -o "[0-9]{2}" | tr "
    " " " ;echo
    10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
    [root@centos72 ~]# echo {1..1000} | egrep -o "<[0-9]{2}>" | tr "
    " " " ;echo
    10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 

    100-199

    [root@centos72 ~]# echo {1..1000} | egrep -o "<[0-9]{3}>" | tr "
    " " " ;echo
    100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 

    200-249

    第1个数字2是固定的

    [root@centos72 ~]# echo {1..1000} | egrep -o "2[0-4][0-9]" | tr "
    " " " ;echo
    200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 
    [root@centos72 ~]# echo {1..1000} | egrep -o "<2[0-4][0-9]>" | tr "
    " " " ;echo
    200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

    250-255

    [root@centos72 ~]# echo {1..1000} | egrep -o "<25[0-5]>"  
    250
    251
    252
    253
    254
    255

    表示把换行变成空格

    [root@centos72 ~]# echo {1..1000} | egrep -o "<25[0-5]>" | tr "
    "   " " 
    250 251 252 253 254 255 [root@centos72 ~]# 
    [root@centos72 ~]# echo {1..1000} | egrep -o "[0-9]" | tr "
    " " " ;echo
    1 2 3 4 5 6 7 8 9 


    7、显示ifconfig命令结果中所有IPv4地址

     比较难理解

    法1:

    [root@centos72 ~]# ifconfig |egrep "([0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))(.([0-9]|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))){3}"  -o
    192.168.137.72
    255.255.255.0
    192.168.137.255
    127.0.0.1
    255.0.0.0

    法2:

    [root@centos72 ~]# ifconfig |egrep -o  "<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])>"
    192.168.137.72
    255.255.255.0
    192.168.137.255
    127.0.0.1
    255.0.0.0


    8、将下面字符串中的每个字符去重并排序,重复次数多的排到前面

    [root@centos72 ~]# echo "welcome to happy to study linux"
    welcome to happy to study linux
    [root@centos72 ~]# echo "welcome to happy to study linux"   |grep "[[:alnum:]]"
    welcome to happy to study linux
    [root@centos72 ~]# echo "welcome to happy to study linux"   |grep "[[:alnum:]]" -o
    w
    e
    l
    c
    o
    m
    e
    t
    o
    h
    a
    p
    p
    y
    t
    o
    s
    t
    u
    d
    y
    l
    i
    n
    u
    x

    进行分类

    [root@centos72 ~]# echo "welcome to happy to study linux"   |grep "[[:alnum:]]" -o |sort
    a
    c
    d
    e
    e
    h
    i
    l
    l
    m
    n
    o
    o
    o
    p
    p
    s
    t
    t
    t
    u
    u
    w
    x
    y
    y

    进行统计

    [root@centos72 ~]# echo "welcome to happy to study linux"   |grep "[[:alnum:]]" -o |sort |  uniq -c  
          1 a
          1 c
          1 d
          2 e
          1 h
          1 i
          2 l
          1 m
          1 n
          3 o
          2 p
          1 s
          3 t
          2 u
          1 w
          1 x
          2 y

    按照数字进行排序,并且是按照第2列

    [root@centos72 ~]# echo "welcome to happy to study linux"   |grep "[[:alnum:]]" -o |sort |  uniq -c | sort -nrt " " -k2 
          3 t
          3 o
          2 y
          2 u
          2 p
          2 l
          2 e
          1 x
          1 w
          1 s
          1 n
          1 m
          1 i
          1 h
          1 d
          1 c
          1 a

    以空格作为分割符

    [root@centos72 ~]# echo "welcome to happy to study linux"   |grep "[[:alnum:]]" -o |sort |  uniq -c | sort -nrt " " -k2  | cut -d " " -f8
    t
    o
    y
    u
    p
    l
    e
    x
    w
    s
    n
    m
    i
    h
    d
    c
    a

    删除换行符

    [root@centos72 ~]# echo "welcome to happy to study linux"   |grep "[[:alnum:]]" -o |sort |  uniq -c | sort -nrt " " -k2  | cut -d " " -f8 | tr -d "
    "
    toyuplexwsnmihdca[root@centos72 ~]# 
    [root@centos72 ~]# echo "welcome to happy to study linux"   |grep "[[:alnum:]]" -o |sort |  uniq -c | sort -nrt " " -k2  | cut -d " " -f8 | tr -d "
    ";echo 
    toyuplexwsnmihdca

     注意每行显示一个匹配的字符

    [root@centos72 ~]# echo "1234567"   | grep "[[:alnum:]]" -o
    1
    2
    3
    4
    5
    6
    7
    [root@centos72 ~]# echo "1234567"   | grep "[[:alnum:]]" 
    1234567


    9、用正则表达式表示出QQ号

    QQ号最少是5位的,现在是10位了

    [root@centos72 ~]# echo  "66666546"  |  egrep   -o  "[1-9][0-9]{4,14}"
    66666546
    
    [root@centos72 ~]# echo  "44445"  |  egrep   -o  "[1-9][0-9]{4,14}"
    44445
    [root@centos72 ~]# echo  "666666"  |  egrep   -o  "[1-9][0-9]{4,14}"
    666666
    [root@centos72 ~]# echo  "7777777"  |  egrep   -o  "[1-9][0-9]{4,14}"
    7777777
    [root@centos72 ~]# echo  "88888888"  |  egrep   -o  "[1-9][0-9]{4,14}"
    88888888
    [root@centos72 ~]# echo  "888888889"  |  egrep   -o  "[1-9][0-9]{4,14}"
    888888889
    [root@centos72 ~]# echo  "1888888889"  |  egrep   -o  "[1-9][0-9]{4,14}"
    1888888889
    [root@centos72 ~]# echo  "11888888889"  |  egrep   -o  "[1-9][0-9]{4,14}"
    11888888889


    10用正则表达式表示手机号

    [root@centos72 ~]# echo  "13333313313"  |   egrep   -o  13[0-9]{9} 
    13333313313


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

  • 相关阅读:
    Map1: iOS开发中定位和地图介绍
    GCD11: 创建计时器
    GCD10: 用GCD构建自己的分派队列
    GCD9: 用GCD将任务分组
    GCD8: 在GCD上让一个任务最多执行一次
    GCD7: 利用GCD延时后执行任务
    GCD6: 在GCD上异步执行非UI相关任务
    GCD5: 用GCD同步执行非UI相关的任务
    回文数
    字符串置换
  • 原文地址:https://www.cnblogs.com/wang618/p/11083920.html
Copyright © 2011-2022 走看看