99
1010### 选项
1111
12- ``` bash
12+ ``` shell
1313-a --text # 不要忽略二进制数据。
1414-A < 显示行数> --after-context=< 显示行数> # 除了显示符合范本样式的那一行之外,并显示该行之后的内容。
1515-b --byte-offset # 在显示符合范本样式的那一行之外,并显示该行之前的内容。
4242
4343### 规则表达式
4444
45- ``` bash
45+ ``` shell
4646^ # 锚定行的开始 如:'^grep'匹配所有以grep开头的行。
4747$ # 锚定行的结束 如:'grep$' 匹配所有以grep结尾的行。
4848. # 匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。
@@ -65,40 +65,40 @@ x\{m,n\} # 重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配
6565
6666在文件中搜索一个单词,命令会返回一个包含 ** “match_pattern”** 的文本行:
6767
68- ``` bash
68+ ``` shell
6969grep match_pattern file_name
7070grep " match_pattern" file_name
7171```
7272
7373在多个文件中查找:
7474
75- ``` bash
75+ ``` shell
7676grep " match_pattern" file_1 file_2 file_3 ...
7777```
7878
7979输出除之外的所有行 ** -v** 选项:
8080
81- ``` bash
81+ ``` shell
8282grep -v " match_pattern" file_name
8383```
8484
8585标记匹配颜色 ** --color=auto** 选项:
8686
87- ``` bash
87+ ``` shell
8888grep " match_pattern" file_name --color=auto
8989```
9090
9191使用正则表达式 ** -E** 选项:
9292
93- ``` bash
93+ ``` shell
9494grep -E " [1-9]+"
9595# 或
9696egrep " [1-9]+"
9797```
9898
9999只输出文件中匹配到的部分 ** -o** 选项:
100100
101- ``` bash
101+ ``` shell
102102echo this is a test line. | grep -o -E " [a-z]+\."
103103line.
104104
@@ -108,13 +108,13 @@ line.
108108
109109统计文件或者文本中包含匹配字符串的行数 ** -c** 选项:
110110
111- ``` bash
111+ ``` shell
112112grep -c " text" file_name
113113```
114114
115115输出包含匹配字符串的行数 ** -n** 选项:
116116
117- ``` bash
117+ ``` shell
118118grep " text" -n file_name
119119# 或
120120cat file_name | grep " text" -n
@@ -125,37 +125,37 @@ grep "text" -n file_1 file_2
125125
126126打印样式匹配所位于的字符或字节偏移:
127127
128- ``` bash
128+ ``` shell
129129echo gun is not unix | grep -b -o " not"
1301307:not
131131# 一行中字符串的字符便宜是从该行的第一个字符开始计算,起始值为0。选项 **-b -o** 一般总是配合使用。
132132```
133133
134134搜索多个文件并查找匹配文本在哪些文件中:
135135
136- ``` bash
136+ ``` shell
137137grep -l " text" file1 file2 file3...
138138```
139139
140140### grep递归搜索文件
141141
142142在多级目录中对文本进行递归搜索:
143143
144- ``` bash
144+ ``` shell
145145grep " text" . -r -n
146146# .表示当前目录。
147147```
148148
149149忽略匹配样式中的字符大小写:
150150
151- ``` bash
151+ ``` shell
152152echo " hello world" | grep -i " HELLO"
153153# hello
154154```
155155
156156选项 ** -e** 制动多个匹配样式:
157157
158- ``` bash
158+ ``` shell
159159echo this is a text line | grep -e " is" -e " line" -o
160160is
161161line
@@ -170,7 +170,7 @@ echo aaa bbb ccc ddd eee | grep -f patfile -o
170170
171171在grep搜索结果中包括或者排除指定文件:
172172
173- ``` bash
173+ ``` shell
174174# 只在目录中所有的.php和.html文件中递归搜索字符"main()"
175175grep " main()" . -r --include * .{php,html}
176176
@@ -184,7 +184,7 @@ grep "main()" . -r --exclude-from filelist
184184
185185使用0值字节后缀的grep与xargs:
186186
187- ``` bash
187+ ``` shell
188188# 测试文件:
189189echo " aaa" > file1
190190echo " bbb" > file2
@@ -197,14 +197,14 @@ grep "aaa" file* -lZ | xargs -0 rm
197197
198198grep静默输出:
199199
200- ``` bash
200+ ``` shell
201201grep -q " test" filename
202202# 不会输出任何信息,如果命令运行成功返回0,失败则返回非0值。一般用于条件测试。
203203```
204204
205205打印出匹配文本之前或者之后的行:
206206
207- ``` bash
207+ ``` shell
208208# 显示匹配某个结果之后的3行,使用 -A 选项:
209209seq 10 | grep " 5" -A 3
2102105
0 commit comments