zoukankan      html  css  js  c++  java
  • grep和rgrep和fgrep

    Linux环境下。

    (1)grep对标签元字符的表示。

    [berry@berry:practice] grep 'w(es).*1' text
    northwest NW Charles Main 3.0 .98 3 34
    [berry@berry:practice] grep -E 'w(es).*1' text
    grep: Invalid back reference
    [berry@berry:practice] grep -E 'w(es).*1' text
    northwest NW Charles Main 3.0 .98 3 34
    [berry@berry:practice] egrep  'w(es).*1' text
    northwest NW Charles Main 3.0 .98 3 34

    (2)grep对锚定单词的元字符的表示。

    [berry@berry:practice] grep '<no>' text 
    no one 
    no no123 
    [berry@berry:practice] grep -E '<no>' text 
    no one 
    no no123 
    [berry@berry:practice] egrep  '<no>' text 
    no one 
    no no123 

    (3)对重复作用的元字符的表示。

    [berry@berry:practice] grep '.[0-9]{2}[^0-9]' text
    northwest NW Charles Main 3.0 .98 3 34
    northwest NW Charles Main 3.0 .98 3 34
    [berry@berry:practice] grep -E '.[0-9]{2}[^0-9]' text
    northwest NW Charles Main 3.0 .98 3 34
    northwest NW Charles Main 3.0 .98 3 34
    [berry@berry:practice] egrep '.[0-9]{2}[^0-9]' text
    northwest NW Charles Main 3.0 .98 3 34
    northwest NW Charles Main 3.0 .98 3 34
    

     (4)Unix环境下,(Solaris egrep)

    # egrep ‘.[0-9]{2}[^0-9]' datafile
    <no output; not recognized with or without backslashes>
    

     说明
    1 扩展元字符{}用来表示重复。除非在前面加上反斜杠,否则Gnu 和UNIX 版本的正规grep 无法
    识别这个扩展元字符。该正则表达式的意思是:搜索这样的行,该行包含这样的字符串,第一个
    字符是一个点号.,紧跟着一个0~9之间的数字[0-9],如果该字符串重复两次{2},则后面就是
    一个非数字的字符[^0-9]。
    2 若使用扩展grep或者grep -E选项,则在表示重复的元字符{2}的括号前面就不需要加反斜杠了。
    3 因为Gnu 版本的egrep 跟grep -E的功能一样,因此这个例子的结果跟前面相同。
    4 这是标准的UNIX下的egrep,不论是否有反斜杠它都无法识别花括号元字符{}。

    (5)rgrep的使用,跟其他grep家族成员不同,rgrep 可以递归访问目录树。rgrep 有一些命令行选项支持跟
    标准的grep 一样的选项。

    [berry@berry:practice] grep "hello" text
    adfdfdfdfddhellodffdf  
    adfdfdfdfddhellodffdf  
    adfdfdfdfddhellodffdf  
    adfdfdfdfddhellodffdf  
    adfdfdfdfddhellodffdf  
    hello word
    hello word
    hello berry!Good morning,every one!
    hello berry!Good morning,every one!
    hello berry!Good morning,every one!
    hello word
    hello berry!Good morning,every one!
    hello berry!Good morning,every one!
    hello word
    hello berry!Good morning,every one!
    hello word
    hello berry!Good morning,every one!
    [berry@berry:practice] rgrep "hello" .
    ./b.txt:A hello world
    ./text:adfdfdfdfddhellodffdf  
    ./text:adfdfdfdfddhellodffdf  
    ./text:adfdfdfdfddhellodffdf  
    ./text:adfdfdfdfddhellodffdf  
    ./text:adfdfdfdfddhellodffdf  
    ./text:hello word
    ./text:hello word
    ./text:hello berry!Good morning,every one!
    ./text:hello berry!Good morning,every one!
    ./text:hello berry!Good morning,every one!
    ./text:hello word
    ./text:hello berry!Good morning,every one!
    ./text:hello berry!Good morning,every one!
    ./text:hello word
    ./text:hello berry!Good morning,every one!
    ./text:hello word
    ./text:hello berry!Good morning,every one!
    ./dir1/a.txt:hello
    

    (6)fgrep 的命令行形式跟grep 相似,但是它不识别任何正则表达式元字符。所有的字符都
    表示它们自己。一个插入符号就表示插入符号,一个美元符号就表示美元符号等等。-F 选项
    使得Gnu grep 精确地模仿fgrep。

    [berry@berry:practice] fgrep '[A-Z]****[0-9]..$5.00' text
    [A-Z]****[0-9]..$5.00
    [berry@berry:practice] grep -F '[A-Z]****[0-9]..$5.00' text
    [A-Z]****[0-9]..$5.00
    

    (7)grep -w no text 和grep '<no>' text的效果是一样的,都是表示按照单词方式匹配,而不是只是作为

    单词的一部分进行匹配。

    [berry@berry:practice] grep '<no>' text
    no one 
    no one 
    no no123 
    [berry@berry:practice] grep 'no' text
    no one 
    no one 
    no no123 
    northwest NW Charles Main 3.0 .98 3 34
    northwest NW Charles Main 3.0 .98 3 34
    noone
    [berry@berry:practice] grep -w "no" text
    no one 
    no one 
    no no123 
    
  • 相关阅读:
    https://和http://区别
    选择排序法
    冒泡排序法-----一点也不简单喔
    hadoop启动后没有datanode怎么办
    洛谷 1379 八数码难题
    Floyd 算法详解
    datagridview 点击列标题排序
    Python【每日一问】05
    Python【每日一问】04
    Python【每日一问】03
  • 原文地址:https://www.cnblogs.com/Berryxiong/p/6398535.html
Copyright © 2011-2022 走看看