• grep / egrep


    grep命令

    语法:grep [-cinvABC] ‘word’ filename

    • -c:打印符合要求的行数
    • -i:不区分大小写
    • -n:在输出符合要求的行的同时连同行号一起输出
    • -v:打印不符合要求的行
    • -r:遍历所有子目录
    • -A:后跟一个数字(有无空格都可以),例如–A2则表示打印符合要求的行以及下面两行
    • -B:后跟一个数字,例如–B2则表示打印符合要求的行以及上面两行
    • -C:后跟一个数字,例如–C2则表示打印符合要求的行以及上下各两行
    [root@centos-01 ~]# ls
    1.txt  2.txt  anaconda-ks.cfg  test
    [root@centos-01 ~]# mkdir grep
    [root@centos-01 ~]# cd grep/
    [root@centos-01 grep]# cp /etc/passwd .
    [root@centos-01 grep]# ls
    passwd
    [root@centos-01 grep]# pwd
    /root/grep
    [root@centos-01 grep]# grep 'nologin' passwd 
    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
    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:997:User for polkitd:/:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    chrony:x:998:996::/var/lib/chrony:/sbin/nologin
    [root@centos-01 grep]# which grep
    alias grep='grep --color=auto'
        /usr/bin/grep
    [root@centos-01 grep]# grep -c 'nologin' passwd 
    15
    [root@centos-01 grep]# grep -n 'nologin' passwd 
    2:bin:x:1:1:bin:/bin:/sbin/nologin
    3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
    4:adm:x:3:4:adm:/var/adm:/sbin/nologin
    5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10:operator:x:11:0:operator:/root:/sbin/nologin
    11:games:x:12:100:games:/usr/games:/sbin/nologin
    12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13:nobody:x:99:99:Nobody:/:/sbin/nologin
    14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15:dbus:x:81:81:System message bus:/:/sbin/nologin
    16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin
    17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
    

    [root@centos-01 grep]# vim passwd

    在文件最后添加

    NOLogin

    保存退出


    [root@centos-01 grep]# grep -n 'nologin' passwd 
    2:bin:x:1:1:bin:/bin:/sbin/nologin
    3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
    4:adm:x:3:4:adm:/var/adm:/sbin/nologin
    5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10:operator:x:11:0:operator:/root:/sbin/nologin
    11:games:x:12:100:games:/usr/games:/sbin/nologin
    12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13:nobody:x:99:99:Nobody:/:/sbin/nologin
    14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15:dbus:x:81:81:System message bus:/:/sbin/nologin
    16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin
    17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
    [root@centos-01 grep]# grep -ni 'nologin' passwd 
    2:bin:x:1:1:bin:/bin:/sbin/nologin
    3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
    4:adm:x:3:4:adm:/var/adm:/sbin/nologin
    5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10:operator:x:11:0:operator:/root:/sbin/nologin
    11:games:x:12:100:games:/usr/games:/sbin/nologin
    12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13:nobody:x:99:99:Nobody:/:/sbin/nologin
    14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15:dbus:x:81:81:System message bus:/:/sbin/nologin
    16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin
    17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
    20:NOLogin
    
    [root@centos-01 grep]# grep -vni 'nologin' passwd 
    1:root:x:0:0:root:/root:/bin/bash
    6:sync:x:5:0:sync:/sbin:/bin/sync
    7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    8:halt:x:7:0:halt:/sbin:/sbin/halt
    
    [root@centos-01 grep]# grep -nA2 'root' passwd 
    1:root:x:0:0:root:/root:/bin/bash
    2-bin:x:1:1:bin:/bin:/sbin/nologin
    3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
    --
    10:operator:x:11:0:operator:/root:/sbin/nologin
    11-games:x:12:100:games:/usr/games:/sbin/nologin
    12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    [root@centos-01 grep]# grep -nB2 'root' passwd 
    1:root:x:0:0:root:/root:/bin/bash
    --
    8-halt:x:7:0:halt:/sbin:/sbin/halt
    9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10:operator:x:11:0:operator:/root:/sbin/nologin
    [root@centos-01 grep]# grep -nC2 'root' passwd 
    1:root:x:0:0:root:/root:/bin/bash
    2-bin:x:1:1:bin:/bin:/sbin/nologin
    3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
    --
    8-halt:x:7:0:halt:/sbin:/sbin/halt
    9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10:operator:x:11:0:operator:/root:/sbin/nologin
    11-games:x:12:100:games:/usr/games:/sbin/nologin
    12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    
    [root@centos-01 ~]# grep -n 'root' /etc/passwd
    1:root:x:0:0:root:/root:/bin/bash
    10:operator:x:11:0:operator:/root:/sbin/nologin
    [root@centos-01 ~]# grep -nv 'nologin' /etc/passwd
    1:root:x:0:0:root:/root:/bin/bash
    6:sync:x:5:0:sync:/sbin:/bin/sync
    7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    8:halt:x:7:0:halt:/sbin:/sbin/halt
    [root@centos-01 ~]# cd grep/
    [root@centos-01 grep]# ls
    passwd
    
    [root@centos-01 grep]# grep '[0-9]' 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:997:User for polkitd:/:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    chrony:x:998:996::/var/lib/chrony:/sbin/nologin
    [root@centos-01 grep]# grep -v '[0-9]' passwd 
    NOLogin
    
    • 在正则表达式中,“^”表示行的开始,“$”表示行的结尾,空行则可以用“^$”表示。
    [root@centos-01 grep]# grep -n '^#' /etc/inittab 
    1:# inittab is no longer used when using systemd.
    2:#
    3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
    4:#
    5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
    6:#
    7:# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
    8:#
    9:# multi-user.target: analogous to runlevel 3
    10:# graphical.target: analogous to runlevel 5
    11:#
    12:# To view current default target, run:
    13:# systemctl get-default
    14:#
    15:# To set a default target, run:
    16:# systemctl set-default TARGET.target
    17:#
    [root@centos-01 grep]# cp /etc/inittab ./
    [root@centos-01 grep]# vim inittab
    

    随便添加一行字符串

    abcdefg

    保存退出

    [root@centos-01 grep]# grep -nv '^#' inittab 
    18:abcdefg

    [root@centos-01 grep]# vim inittab

    添加一行数字

    12345

    保存退出

    [root@centos-01 grep]# grep '^[^0-9]' inittab 
    # inittab is no longer used when using systemd.
    #
    # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
    #
    # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
    #
    # systemd uses 'targets' instead of runlevels. By default, there are two main targets:
    #
    # multi-user.target: analogous to runlevel 3
    # graphical.target: analogous to runlevel 5
    #
    # To view current default target, run:
    # systemctl get-default
    #
    # To set a default target, run:
    # systemctl set-default TARGET.target
    #
    abcdefg
    

    • .:表示任意一个字符
    • *:表示零个或多个前面的字符
    • .*:表示零个或多个任意字符,空行也包含在内
    [root@centos-01 grep]# grep 'r.o' passwd 
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    [root@centos-01 grep]# grep 'oo*' 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
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    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:997:User for polkitd:/:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    chrony:x:998:996::/var/lib/chrony:/sbin/nologin
    NOLogin
    [root@centos-01 grep]# grep '.*' 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:997:User for polkitd:/:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    chrony:x:998:996::/var/lib/chrony:/sbin/nologin
    NOLogin
    [root@centos-01 grep]# grep 'sshd.*login' passwd 
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    
    • { },其内部为数字,表示前面的字符要重复的次数。注意,{ }左右都需要加上脱义字符‘’。另外,使用{ }还可以表示一个范围的,具体格式是‘{n1,n2}’,其中n1
    [root@centos-01 grep]# grep 'o{2}' passwd 
    root:x:0:0:root:/root:/bin/bash
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    [root@centos-01 grep]# grep 'o{0,3}' 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:997:User for polkitd:/:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    chrony:x:998:996::/var/lib/chrony:/sbin/nologin
    NOLogin
    
    • grep -E==egrep
    [root@centos-01 grep]# grep -E 'o{2}' passwd 
    root:x:0:0:root:/root:/bin/bash
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    
    • +:表示一个或一个以上前面的字符
    [root@centos-01 grep]# grep 'o+o' passwd 
    root:x:0:0:root:/root:/bin/bash
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    • ?:表示零个或一个前面的字符
    [root@centos-01 grep]# grep 'i?v' passwd 
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    chrony:x:998:996::/var/lib/chrony:/sbin/nologin
    • |:筛选字符串1或者字符串2
    [root@centos-01 grep]# grep -E 'root|nologin' 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
    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:997:User for polkitd:/:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    chrony:x:998:996::/var/lib/chrony:/sbin/nologin
    

    egrep命令

    egrep是grep的扩展版本,可以用egrep完成grep不能完成的工作,grep能完成的egrep完全可以完成。

    [root@centos-01 grep]# egrep 'o{2}' passwd 
    root:x:0:0:root:/root:/bin/bash
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    
    [root@centos-01 grep]# egrep 'o+o' passwd 
    root:x:0:0:root:/root:/bin/bash
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    [root@centos-01 grep]# egrep 'o+l' passwd 
    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
    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:997:User for polkitd:/:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    chrony:x:998:996::/var/lib/chrony:/sbin/nologin
    
    [root@centos-01 grep]# egrep 'f?or' passwd 
    operator:x:11:0:operator:/root:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    polkitd:x:999:997:User for polkitd:/:/sbin/nologin
    
    • ( )表示一个整体,例如(o){2}就表示‘oo’。
    [root@centos-01 grep]# egrep '(o){2}' passwd 
    root:x:0:0:root:/root:/bin/bash
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    
  • 相关阅读:
    后缀树(suffix tree)
    哈希表(Hash Table)
    ansible报错Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this
    windows上python上传下载文件到linux服务器指定路径【转】
    MySQL参数最大连接数max_connections
    linux服务器last查看关机记录
    /etc/fstab文件详解【转】
    MySQL5.7更改用户名密码
    awk对列/行进行统计求和【转】
    passwd: Have exhausted maximum number of retries for service【转】
  • 原文地址:https://www.cnblogs.com/Genesis2018/p/9079763.html
Copyright © 2020-2023  润新知