扩展grep(grep -E 或者 egrep)

来源:互联网 发布:淘宝网生产许可证编号 编辑:程序博客网 时间:2024/06/05 11:36

扩展grep(grep -E 或者 egrep):
使用扩展grep的主要好处是增加了额外的正则表达式元字符集。

 

打印所有包含NW或EA的行。如果不是使用egrep,而是grep,将不会有结果查出。

    # egrep 'NW|EA' testfile         northwest       NW      Charles Main        3.0     .98     3       34    eastern         EA      TB Savage           4.4     .84     5       20

 

对于标准grep,如果在扩展元字符前面加\,grep会自动启用扩展选项-E。

#grep 'NW\|EA' testfilenorthwest       NW      Charles Main        3.0     .98     3       34eastern         EA      TB Savage           4.4     .84     5       20

 

搜索所有包含一个或多个3的行。

复制代码
# egrep '3+' testfile# grep -E '3+' testfile# grep '3\+' testfile        #这3条命令将会northwest       NW      Charles Main          3.0     .98     3       34western         WE      Sharon Gray           5.3     .97     5       23northeast       NE      AM Main Jr.           5.1     .94     3       13central         CT      Ann Stephens          5.7     .94     5       13
复制代码

 

搜索所有包含0个或1个小数点字符的行。
    

复制代码
# egrep '2\.?[0-9]' testfile # grep -E '2\.?[0-9]' testfile# grep '2\.\?[0-9]' testfile #首先含有2字符,其后紧跟着0个或1个点,后面再是0和9之间的数字。western         WE       Sharon Gray          5.3     .97     5       23southwest       SW      Lewis Dalsass         2.7     .8      2       18eastern         EA       TB Savage             4.4     .84     5       20
复制代码

 

搜索一个或者多个连续的no的行。
    

# egrep '(no)+' testfile# grep -E '(no)+' testfile# grep '\(no\)\+' testfile   #3个命令返回相同结果,northwest       NW      Charles Main        3.0     .98     3       34northeast       NE       AM Main Jr.        5.1     .94     3       13north           NO      Margot Weber        4.5     .89     5       9

 

不使用正则表达式

fgrep 查询速度比grep命令快,但是不够灵活:它只能找固定的文本,而不是规则表达式。

如果你想在一个文件或者输出中找到包含星号字符的行

fgrep  '*' /etc/profilefor i in /etc/profile.d/*.sh ; do或grep -F '*' /etc/profilefor i in /etc/profile.d/*.sh ; do
0 0
原创粉丝点击