zoukankan      html  css  js  c++  java
  • shell中的连接符 及grep的使用

    shell中的连接符
    [root@tyzz ~]# ls 1.txt && ls 2.txt
    1.txt
    2.txt
    [root@tyzz ~]# ls 10.txt && ls 2.txt
    ls: cannot access 10.txt: No such file or directory
    [root@tyzz ~]# ls 1.txt && ls 20.txt
    1.txt
    ls: cannot access 20.txt: No such file or directory

    && 左边的命令执行成功后 才会执行右边的

    [root@tyzz ~]# ls 1.txt || ls 2.txt
    1.txt
    [root@tyzz ~]# ls 10.txt || ls 2.txt
    ls: cannot access 10.txt: No such file or directory
    2.txt

    || 左边的命令执行不成功 才会执行右
    ; 左右两边的命令执行 不影响,相当于连续执行两条命令。


    grep命令的基本用法

    -n 显示匹配行的行号
    -v 反向匹配即匹配不包含匹配字的行

    [root@tyzz ~]# grep -n 'root' 1.txt 
    1:root:x:0:0:root:/root:/bin/bash
    10:operator:x:11:0:operator:/root:/sbin/nologin
    [root@tyzz ~]# grep -v 'root' 1.txt 
    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

    -A x 显示匹配行及匹配行的后x行

    [root@tyzz ~]# grep -n -A 2 'root' 1.txt 
    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

    -i 不区分大小写

    [root@tyzz ~]# grep -i 'ROOT' 1.txt  
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin

    [0-9] 匹配任意一个数字
    类似的还有 [a-z][A-Z]

    [root@tyzz ~]# grep '[0-9]' 1.txt 
    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

    ^a 以a开头的行
    a$ 以a结尾的行
    ^$ 空行

    [root@tyzz ~]# grep -n '^a' 1.txt 
    6:adm:x:3:4:adm:/var/adm:/sbin/nologin
    24:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
    25:avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
    27:abrt:x:173:173::/etc/abrt:/sbin/nologin
    
    [root@tyzz ~]# grep -n 'nologin$' 1.txt 
    4:bin:x:1:1:bin:/bin:/sbin/nologin
    5:daemon:x:2:2:daemon:/sbin:/sbin/nologin
    6:adm:x:3:4:adm:/var/adm:/sbin/nologin
    7:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    16:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    
    
    [root@tyzz ~]# grep -n '^$' 1.txt 
    9:
    10:

    . 任意一个字符
    * 0个或者多个前面的字符
    .
    任意个任意字符
    ? 0个或者1个?前面的字符
    + 1个或者多个+前面的字符

    [root@tyzz ~]# grep -n 'r.o' 1.txt 
    1:root:x:0:0:root:/root:/bin/bash
    14:operator:x:11:0:operator:/root:/sbin/nologin
    [root@tyzz ~]# grep -n 'r*o' 1.txt 
    1:root:x:0:0:root:/root:/bin/bash
    4:bin:x:1:1:bin:/bin:/sbin/nologin
    5:daemon:x:2:2:daemon:/sbin:/sbin/nologin
    6:adm:x:3:4:adm:/var/adm:/sbin/nologin
    7:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    11:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    13:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
  • 相关阅读:
    【线段树】【积累】主席树杂题积累 2016CCPC长春K SequenceII
    【积累】最小不能表示正整数 (以及一些做法
    【字符串】回文树&&回文自动机PAM
    【字符串】后缀自动机SAM
    【字符串】AC自动机
    【字符串】Trie树
    StringUtils类中isEmpty与isBlank的区别
    【Git】pull遇到错误:error: Your local changes to the following files would be overwritten by merge:
    jsp 与jstl
    listener 作用
  • 原文地址:https://www.cnblogs.com/aallenn/p/6700588.html
Copyright © 2011-2022 走看看