美文网首页Linux小推车
Linux Day17:正则表达式

Linux Day17:正则表达式

作者: 泥人吴 | 来源:发表于2018-10-21 12:13 被阅读6次

    REGEXP: REGular EXPression
    Pattern

    正则表达式:

    • Basic REGEXP : 基本
    • Extented REGEXP :扩展

    复习grep的用法:

    -i, --ignore-case
    -v, --invert-match
    -o, --only-matching
    # -A NUM, --after-context=NUM
    # -B NUM, --before-context=NUM
    # -C NUM, -NUM, --context=NUM
    $ cat /proc/cpuinfo
    ...
    cpu MHz     : 2399.996
    cache size  : 4096 KB
    physical id : 0
    siblings    : 16
    core id     : 15
    cpu cores   : 16
    apicid      : 15
    initial apicid  : 15
    fpu     : yes
    fpu_exception   : yes
    cpuid level : 13
    ...
    
    #以core id       : 15为目标项,展示附近。
    # -A NUM, --after-context=NUM
    
    $ grep -A 2 'core id' /proc/cpuinfo
    core id     : 0
    cpu cores   : 16
    apicid      : 0
    --
    core id     : 1
    cpu cores   : 16
    apicid      : 1
    --
    core id     : 2
    cpu cores   : 16
    apicid      : 2
    
    # -B NUM, --before-context=NUM
    $ grep -B 2 '^core id' /proc/cpuinfo
    physical id : 0
    siblings    : 16
    core id     : 0
    --
    physical id : 0
    siblings    : 16
    core id     : 1
    --
    physical id : 0
    siblings    : 16
    core id     : 2
    
    # -C NUM, -NUM, --context=NUM
    $ grep -C 2 '^core id' /proc/cpuinfo
    physical id : 0
    siblings    : 16
    core id     : 0
    cpu cores   : 16
    apicid      : 0
    --
    physical id : 0
    siblings    : 16
    core id     : 1
    cpu cores   : 16
    apicid      : 1
    

    扩展正则表达式

    + 字符匹配:.  [  ]   [ ^ ]
    + 次数匹配: *  ?  +(匹配其前面的字符至少1次) { m,n}扩展正则表达式不需要"\"
    + 位置锚定: ^  $  \<  \>
    + 分组:( ): 分组   ——  \1  \2  \3 ....不需要\转义
    + 或者: a|b: a or b (例子:C|cat表示:
    C or cat,不是Cat or cat。)后面这种应该表示为——运用分组的方法:'(C|c)at'
    
    • 练习:如何查找1-255之间的数字:
    grep -E --color '\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])/>' /etc/passwd`
    或者使用`/etc/passwd | egrep --color '\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])/>' 
    

    友情阅读推荐:

    生信技能树公益视频合辑:学习顺序是linux,r,软件安装,geo,小技巧,ngs组学!
    请猛戳下面链接
    B站链接:https://m.bilibili.com/space/338686099

    YouTube链接:https://m.youtube.com/channel/UC67sImqK7V8tSWHMG8azIVA/playlists

    生信工程师入门最佳指南:https://mp.weixin.qq.com/s/vaX4ttaLIa19MefD86WfUA

    学徒培养:https://mp.weixin.qq.com/s/3jw3_PgZXYd7FomxEMxFmw

    相关文章

      网友评论

        本文标题:Linux Day17:正则表达式

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