9
9
10
10
### 选项
11
11
12
- ``` bash
12
+ ``` shell
13
13
-a --text # 不要忽略二进制数据。
14
14
-A < 显示行数> --after-context=< 显示行数> # 除了显示符合范本样式的那一行之外,并显示该行之后的内容。
15
15
-b --byte-offset # 在显示符合范本样式的那一行之外,并显示该行之前的内容。
42
42
43
43
### 规则表达式
44
44
45
- ``` bash
45
+ ``` shell
46
46
^ # 锚定行的开始 如:'^grep'匹配所有以grep开头的行。
47
47
$ # 锚定行的结束 如:'grep$' 匹配所有以grep结尾的行。
48
48
. # 匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。
@@ -65,40 +65,40 @@ x\{m,n\} # 重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配
65
65
66
66
在文件中搜索一个单词,命令会返回一个包含 ** “match_pattern”** 的文本行:
67
67
68
- ``` bash
68
+ ``` shell
69
69
grep match_pattern file_name
70
70
grep " match_pattern" file_name
71
71
```
72
72
73
73
在多个文件中查找:
74
74
75
- ``` bash
75
+ ``` shell
76
76
grep " match_pattern" file_1 file_2 file_3 ...
77
77
```
78
78
79
79
输出除之外的所有行 ** -v** 选项:
80
80
81
- ``` bash
81
+ ``` shell
82
82
grep -v " match_pattern" file_name
83
83
```
84
84
85
85
标记匹配颜色 ** --color=auto** 选项:
86
86
87
- ``` bash
87
+ ``` shell
88
88
grep " match_pattern" file_name --color=auto
89
89
```
90
90
91
91
使用正则表达式 ** -E** 选项:
92
92
93
- ``` bash
93
+ ``` shell
94
94
grep -E " [1-9]+"
95
95
# 或
96
96
egrep " [1-9]+"
97
97
```
98
98
99
99
只输出文件中匹配到的部分 ** -o** 选项:
100
100
101
- ``` bash
101
+ ``` shell
102
102
echo this is a test line. | grep -o -E " [a-z]+\."
103
103
line.
104
104
@@ -108,13 +108,13 @@ line.
108
108
109
109
统计文件或者文本中包含匹配字符串的行数 ** -c** 选项:
110
110
111
- ``` bash
111
+ ``` shell
112
112
grep -c " text" file_name
113
113
```
114
114
115
115
输出包含匹配字符串的行数 ** -n** 选项:
116
116
117
- ``` bash
117
+ ``` shell
118
118
grep " text" -n file_name
119
119
# 或
120
120
cat file_name | grep " text" -n
@@ -125,37 +125,37 @@ grep "text" -n file_1 file_2
125
125
126
126
打印样式匹配所位于的字符或字节偏移:
127
127
128
- ``` bash
128
+ ``` shell
129
129
echo gun is not unix | grep -b -o " not"
130
130
7:not
131
131
# 一行中字符串的字符便宜是从该行的第一个字符开始计算,起始值为0。选项 **-b -o** 一般总是配合使用。
132
132
```
133
133
134
134
搜索多个文件并查找匹配文本在哪些文件中:
135
135
136
- ``` bash
136
+ ``` shell
137
137
grep -l " text" file1 file2 file3...
138
138
```
139
<
8000
/td>139
140
140
### grep递归搜索文件
141
141
142
142
在多级目录中对文本进行递归搜索:
143
143
144
- ``` bash
144
+ ``` shell
145
145
grep " text" . -r -n
146
146
# .表示当前目录。
147
147
```
148
148
149
149
忽略匹配样式中的字符大小写:
150
150
151
- ``` bash
151
+ ``` shell
152
152
echo " hello world" | grep -i " HELLO"
153
153
# hello
154
154
```
155
155
156
156
选项 ** -e** 制动多个匹配样式:
157
157
158
- ``` bash
158
+ ``` shell
159
159
echo this is a text line | grep -e " is" -e " line" -o
160
160
is
161
161
line
@@ -170,7 +170,7 @@ echo aaa bbb ccc ddd eee | grep -f patfile -o
170
170
171
171
在grep搜索结果中包括或者排除指定文件:
172
172
173
- ``` bash
173
+ ``` shell
174
174
# 只在目录中所有的.php和.html文件中递归搜索字符"main()"
175
175
grep " main()" . -r --include * .{php,html}
176
176
@@ -184,7 +184,7 @@ grep "main()" . -r --exclude-from filelist
184
184
185
185
使用0值字节后缀的grep与xargs:
186
186
187
- ``` bash
187
+ ``` shell
188
188
# 测试文件:
189
189
echo " aaa" > file1
190
190
echo " bbb" > file2
@@ -197,14 +197,14 @@ grep "aaa" file* -lZ | xargs -0 rm
197
197
198
198
grep静默输出:
199
199
200
- ``` bash
200
+ ``` shell
201
201
grep -q " test" filename
202
202
# 不会输出任何信息,如果命令运行成功返回0,失败则返回非0值。一般用于条件测试。
203
203
```
204
204
205
205
打印出匹配文本之前或者之后的行:
206
206
207
- ``` bash
207
+ ``` shell
208
208
# 显示匹配某个结果之后的3行,使用 -A 选项:
209
209
seq 10 | grep " 5" -A 3
210
210
5
0 commit comments