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 
    
  • 相关阅读:
    面向对象之类属性、类方法,静态方法
    python面向对象之继承
    python之面向对象练习
    python面向对象
    CentOS7.5最小化安装之后的配置
    怎么在一台电脑上安装win7与centos7双系统
    安装Win7时删除系统保留的100M隐藏分区
    win7在安装时跳过输入用户名界面,直接开启管理员用户
    ESP8266 wifi干扰钓鱼实现
    ESP8266 wifi干扰、钓鱼实现
  • 原文地址:https://www.cnblogs.com/Berryxiong/p/6398535.html
Copyright © 2011-2022 走看看