grep

扩展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' testfile
northwest       NW      Charles Main        3.0     .98     3       34
eastern         EA      TB Savage           4.4     .84     5       20

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

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

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

grep
# 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       23
southwest       SW      Lewis Dalsass         2.7     .8      2       18
eastern         EA       TB Savage             4.4     .84     5       20
grep

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

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

不使用正则表达式

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

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

fgrep  '*' /etc/profile
for i in /etc/profile.d/*.sh ; do

或
grep -F '*' /etc/profile
for i in /etc/profile.d/*.sh ; do