1 | egrep
|
2 | ===
|
3 |
|
4 | 在文件内查找指定的字符串
|
5 |
|
6 | ## 补充说明
|
7 |
|
8 | **egrep命令** 用于在文件内查找指定的字符串。egrep执行效果与`grep -E`相似,使用的语法及参数可参照grep指令,与grep的不同点在于解读字符串的方法。egrep是用extended regular expression语法来解读的,而grep则用basic regular expression 语法解读,extended regular expression比basic regular expression的表达更规范。
|
9 |
|
10 | ### 语法
|
11 |
|
12 | ```shell
|
13 | egrep(选项)(查找模式)(文件名1,文件名2,……)
|
14 | ```
|
15 |
|
16 | ### 实例
|
17 |
|
18 | 显示文件中符合条件的字符。例如,查找当前目录下所有文件中包含字符串"Linux"的文件,可以使用如下命令:
|
19 |
|
20 | ```shell
|
21 | egrep Linux *
|
22 | ```
|
23 |
|
24 | 结果如下所示:
|
25 |
|
26 | ```shell
|
27 | # 以下五行为 testfile 中包含Linux字符的行
|
28 | testfile:hello Linux!
|
29 | testfile:Linux is a free Unix-type operating system.
|
30 | testfile:This is a Linux testfile!
|
31 | testfile:Linux
|
32 | testfile:Linux
|
33 |
|
34 | # 以下两行为testfile1中含Linux字符的行
|
35 | testfile1:helLinux!
|
36 | testfile1:This a Linux testfile!
|
37 |
|
38 | # 以下两行为 testfile_2 中包含Linux字符的行
|
39 | testfile_2:Linux is a free unix-type opterating system
|
40 | testfile_2:Linux test
|
41 | ```
|
42 |
|
43 | 过滤注释行和空白行
|
44 |
|
45 | ```shell
|
46 | egrep -v '^\s*(#|$)' filename
|
47 | ```
|