美文网首页
十八 文本操作——正则表达式

十八 文本操作——正则表达式

作者: supermanto | 来源:发表于2020-04-06 15:20 被阅读0次

    元字符


    (1)查询文件中包含hello的行

    user1@SC02ZRC4KMD6N ~ % cat test.txt 
    hello testera
    hello testerb
    Hello testerc
    user1@SC02ZRC4KMD6N ~ % grep hello test.txt
    hello testera
    hello testerb
    

    (2).匹配除换行符外的任意单个字符

    user1@SC02ZRC4KMD6N ~ % grep he... test.txt
    hello testera
    hello testerb
    

    (3)*匹配任意一个跟在它前面的字符

    user1@SC02ZRC4KMD6N ~ % grep he.* test.txt
    hello testera
    hello testerb
    

    注意:前面介绍过通配符*,是可以单独使用的。这里的元字符*是不能单独使用的。
    (4)$匹配结尾

    user1@SC02ZRC4KMD6N ~ % grep he.*$ test.txt
    hello testera
    hello testerb
    

    (5)^匹配开头

    user1@SC02ZRC4KMD6N ~ % grep ^H test.txt
    Hello testerc
    

    (6)[]匹配方括号中的任意一个字符

    user1@SC02ZRC4KMD6N ~ % grep ^[Hh] test.txt
    hello testera
    hello testerb
    Hello testerc
    

    (7)\转义后面的特殊字符
    (8)正则匹配之后,操作匹配内容:以空格进行分割后取第一个字段

    user1@SC02ZRC4KMD6N ~ % grep hello test.txt | cut -d " " -f 1
    hello
    hello
    

    扩展元字符

    相关文章

      网友评论

          本文标题:十八 文本操作——正则表达式

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