-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
406 lines (288 loc) · 15.6 KB
/
index.html
File metadata and controls
406 lines (288 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<!doctype html>
<html lang="en-us">
<head>
<title>Linux文本处理命令总结 // sin-coder</title>
<meta charset="utf-8" />
<meta name="generator" content="Hugo 0.59.1" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="csuyzz" />
<meta name="description" content="" />
<link rel="stylesheet" href="https://sin-coder.github.io/css/main.min.f90f5edd436ec7b74ad05479a05705770306911f721193e7845948fb07fe1335.css" />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Linux文本处理命令总结"/>
<meta name="twitter:description" content="一、概述 Linux中的有关文本处理和分析的命令有很多,本节主要介绍几个使用比较频繁的命令:
grep、sed、find、awk。其中每个命令的使用方式都是比较灵活的,命令的参数很多,选
项也很多,但我们只需记住些常用的即可
二、grep 1、用途简介 grep用于查找文件中符合条件的字符串,比较多地用于日志行为分析
2、命令格式 grep match_pattern file
3、常用参数 -o:只输出匹配的文本行
-v:只输出没有匹配的文本行
-c:统计文件中包含文本的行数
-n:打印匹配的行号
-i:搜索时忽略大小写
-l:只打印文件名
-e:指定字符串作为查找文件内容的样式
-r:递归的搜索
4、使用示例 (1)匹配多个字符串模式
grep -e "mode1" -e "mode2" filename
(2)在多级目录中对文本递归搜索
grep "mode" . -r -n
(3)在当前目录下查找以file为后缀的文件名并且包含test字符串的文件
grep test file
(4)在日志文件夹中查找包含docker字符串的日志
grep -n docker /var/log/log
三、sed 1、用途简介 sed命令主要是利用脚本来处理文本文件,主要用来自动编辑一个或多个文件、简化"/>
<meta property="og:title" content="Linux文本处理命令总结" />
<meta property="og:description" content="一、概述 Linux中的有关文本处理和分析的命令有很多,本节主要介绍几个使用比较频繁的命令:
grep、sed、find、awk。其中每个命令的使用方式都是比较灵活的,命令的参数很多,选
项也很多,但我们只需记住些常用的即可
二、grep 1、用途简介 grep用于查找文件中符合条件的字符串,比较多地用于日志行为分析
2、命令格式 grep match_pattern file
3、常用参数 -o:只输出匹配的文本行
-v:只输出没有匹配的文本行
-c:统计文件中包含文本的行数
-n:打印匹配的行号
-i:搜索时忽略大小写
-l:只打印文件名
-e:指定字符串作为查找文件内容的样式
-r:递归的搜索
4、使用示例 (1)匹配多个字符串模式
grep -e "mode1" -e "mode2" filename
(2)在多级目录中对文本递归搜索
grep "mode" . -r -n
(3)在当前目录下查找以file为后缀的文件名并且包含test字符串的文件
grep test file
(4)在日志文件夹中查找包含docker字符串的日志
grep -n docker /var/log/log
三、sed 1、用途简介 sed命令主要是利用脚本来处理文本文件,主要用来自动编辑一个或多个文件、简化" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://sin-coder.github.io/linux/textcommand/" />
<meta property="article:published_time" content="2020-04-24T00:08:20+08:00" />
<meta property="article:modified_time" content="2020-04-24T00:08:20+08:00" />
</head>
<body>
<header class="app-header">
<a href="https://sin-coder.github.io"><img class="app-header-avatar" src="/cat.jpg" alt="csuyzz" /></a>
<h1>sin-coder</h1>
<p>I always remember that 'Talk is cheap,show me the code'</p>
<div class="app-header-social">
<a target="_blank" href="https://github.com/sin-coder" rel="noreferrer noopener"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-github">
<title>github</title>
<path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22"></path>
</svg></a>
<a target="_blank" href="mailto:csuyzz@foxmail.com" rel="noreferrer noopener"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-mail">
<title>mail</title>
<path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"></path><polyline points="22,6 12,13 2,6"></polyline>
</svg></a>
<a target="_blank" href="https://cn.linkedin.com/in/csuyzz" rel="noreferrer noopener"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-linkedin">
<title>linkedin</title>
<path d="M16 8a6 6 0 0 1 6 6v7h-4v-7a2 2 0 0 0-2-2 2 2 0 0 0-2 2v7h-4v-7a6 6 0 0 1 6-6z"></path><rect x="2" y="9" width="4" height="12"></rect><circle cx="4" cy="4" r="2"></circle>
</svg></a>
<a target="_blank" href="https://twitter.com/csuyzz1" rel="noreferrer noopener"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-twitter">
<title>twitter</title>
<path d="M23 3a10.9 10.9 0 0 1-3.14 1.53 4.48 4.48 0 0 0-7.86 3v1A10.66 10.66 0 0 1 3 4s-4 9 5 13a11.64 11.64 0 0 1-7 2c9 5 20 0 20-11.5a4.5 4.5 0 0 0-.08-.83A7.72 7.72 0 0 0 23 3z"></path>
</svg></a>
</div>
<h3><a href="/" title="首页">首页</a></h3>
<h3><a href="/category/content/" title="首页">分类目录</a></h3>
<h3><a href="/personal/introduce/" title="首页">个人简介</a></h3>
</header>
<main class="app-container">
<article class="post">
<header class="post-header">
<h1 class ="post-title">Linux文本处理命令总结</h1>
<div class="post-meta">
<div>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-calendar">
<title>calendar</title>
<rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect><line x1="16" y1="2" x2="16" y2="6"></line><line x1="8" y1="2" x2="8" y2="6"></line><line x1="3" y1="10" x2="21" y2="10"></line>
</svg>
Apr 24, 2020
</div>
<div>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-clock">
<title>clock</title>
<circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline>
</svg>
2 min read
</div><div>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tag">
<title>tag</title>
<path d="M20.59 13.41l-7.17 7.17a2 2 0 0 1-2.83 0L2 12V2h10l8.59 8.59a2 2 0 0 1 0 2.82z"></path><line x1="7" y1="7" x2="7" y2="7"></line>
</svg>
<a class="tag" href="https://sin-coder.github.io/tags/linux/">Linux</a></div></div>
</header>
<div class="post-content">
<hr />
<h3 id="一-概述">一、概述</h3>
<blockquote>
<p>Linux中的有关文本处理和分析的命令有很多,本节主要介绍几个使用比较频繁的命令:</p>
</blockquote>
<p>grep、sed、find、awk。其中每个命令的使用方式都是比较灵活的,命令的参数很多,选</p>
<p>项也很多,但我们只需记住些常用的即可</p>
<h3 id="二-grep">二、grep</h3>
<h4 id="1-用途简介">1、用途简介</h4>
<blockquote>
<p>grep用于查找文件中符合条件的字符串,比较多地用于日志行为分析</p>
</blockquote>
<h4 id="2-命令格式">2、命令格式</h4>
<blockquote>
<p>grep match_pattern file</p>
</blockquote>
<h4 id="3-常用参数">3、常用参数</h4>
<ul>
<li><p>-o:只输出匹配的文本行</p></li>
<li><p>-v:只输出没有匹配的文本行</p></li>
<li><p>-c:统计文件中包含文本的行数</p></li>
<li><p>-n:打印匹配的行号</p></li>
<li><p>-i:搜索时忽略大小写</p></li>
<li><p>-l:只打印文件名</p></li>
<li><p>-e:指定字符串作为查找文件内容的样式</p></li>
<li><p>-r:递归的搜索</p></li>
</ul>
<h4 id="4-使用示例">4、使用示例</h4>
<blockquote>
<p>(1)匹配多个字符串模式</p>
<blockquote>
<p>grep -e "mode1" -e "mode2" filename</p>
</blockquote>
<p>(2)在多级目录中对文本递归搜索</p>
<blockquote>
<p>grep "mode" . -r -n</p>
</blockquote>
<p>(3)在当前目录下查找以file为后缀的文件名并且包含test字符串的文件</p>
<blockquote>
<p>grep test <em>file</em></p>
</blockquote>
<p>(4)在日志文件夹中查找包含docker字符串的日志</p>
<blockquote>
<p>grep -n docker /var/log/<em>log</em></p>
</blockquote>
</blockquote>
<h3 id="三-sed">三、sed</h3>
<h4 id="1-用途简介-1">1、用途简介</h4>
<blockquote>
<p>sed命令主要是利用脚本来处理文本文件,主要用来自动编辑一个或多个文件、简化</p>
</blockquote>
<p>对文件的反复操作、编写转换程序</p>
<blockquote>
<p>sed命令在处理文本时把当前行存储在临时缓冲区,称为模式空间,然后使用sed命令</p>
</blockquote>
<p>处理缓冲区中的内容;处理之后把缓冲区中的内容送往屏幕显示,再去处理下一行,不断</p>
<p>重复,直到文件末。如果不使用写入的命令,文件内容是不会被改变的</p>
<blockquote>
<p>一般在使用命令时都会对行进行定址,如果不指定地址,sed默认会处理文件的所有行</p>
</blockquote>
<h4 id="2-命令格式-1">2、命令格式</h4>
<blockquote>
<p>sed [-hnV] [-e<script>] [-f] [文本文件]</p>
</blockquote>
<h4 id="3-常用参数-1">3、常用参数</h4>
<ul>
<li><p>-e<script>:以选项中指定的script来处理输入的文件</p></li>
<li><p>-f<script>:以选项中指定的script文件来处理输入的文本文件</p></li>
<li><p>-n:取消默认的完整输出,只要需要的行</p></li>
</ul>
<h4 id="4-动作说明">4、动作说明</h4>
<ul>
<li><p>a: a的后面可以接字符串,而这些字符串会在目前的下一行出现</p></li>
<li><p>c: 后面可以接字符串,这些字符串可以取代n1,n2之间的行</p></li>
<li><p>d: d后通常不用接字符串</p></li>
<li><p>i: i的后面可以接字符串,而且这些字符串会在新的一行出现</p></li>
<li><p>p: print行信息</p></li>
</ul>
<h4 id="5-使用示例">5、使用示例</h4>
<h5 id="1-sed命令经常用于行新增-删除-替换和显示">(1)sed命令经常用于行新增、删除、替换和显示</h5>
<ul>
<li><p>打印出日志文件的第三行 : sed -n '3p' boot.log</p></li>
<li><p>打印出日志文件的第3行到第9行 : sed -n '3,9p' boot.log</p></li>
<li><p>打印出日志文件的最后一行 : sed -n '$p' boot.log</p></li>
<li><p>打印1-5行并且显示行号 : sed -n -e '1,5p' -e '=' boot.log</p></li>
<li><p>将文件的2-5行内容置换成test : nl test.file | sed '2,5c test'</p></li>
<li><p>删除2-5行的文件内容 : nl test.file | sed '2,5d'</p></li>
<li><p>在第2行后增加一行数据 : nl test.file | sed '2a, this is a new line'</p></li>
</ul>
<h5 id="2-sed命令用于数据的搜寻并处理">(2)sed命令用于数据的搜寻并处理</h5>
<ul>
<li><p>搜索文件中包含root关键字的行 : nl boot.log | sed -n '/root/p'</p></li>
<li><p>数据的搜寻并删除 nl boot.log | sed -n '/root/d'</p></li>
<li><p>数据的搜寻并替换 sed 's/要被取代的字符串/新的字符串/g'</p></li>
</ul>
<h3 id="四-find">四、find</h3>
<h4 id="1-用途简介-2">1、用途简介</h4>
<blockquote>
<p>find命令用来在指定的目录下查找文件、任何位于参数之前的字符串都将被视为待</p>
</blockquote>
<p>查找的目录名;如果不使用任何参数,则将查找到的子目录和文件进行全部显示将在</p>
<p>当前目录下查找子目录和文件,并将查询结果全部显示</p>
<h4 id="2-命令格式-2">2、命令格式</h4>
<blockquote>
<p>find path -option [ -print] [-exec -ok command] {} \;</p>
<p>find命令判断 “-”之前的部分为path,之后的是expression option; 如果Path是空字符</p>
</blockquote>
<p>串则使用当前路径</p>
<h4 id="3-常用参数-2">3、常用参数</h4>
<ul>
<li><p>-mount:只检查和在同一个文件系统下的部分</p></li>
<li><p>-amin n : 在过去n分钟被读取过</p></li>
<li><p>-atime n:在过去n天被读取过的文件</p></li>
<li><p>-ctime n : 在过去n分钟被修改过的文件</p></li>
<li><p>-cmin:在过去n分钟内被修改过</p></li>
<li><p>-name:文件名称符合name的文件</p></li>
<li><p>-pid n : process id是n的文件</p></li>
<li><p>-ipath p:路径符合p的文件,ipath一般会忽略路径的大小写</p></li>
<li><p>-empty:空的文件</p></li>
</ul>
<h4 id="4-使用示例-1">4、使用示例</h4>
<h5 id="1-find-name-log">(1) find . -name "*.log"</h5>
<blockquote>
<p>将当前目录及其子目录下所有延伸名是c的文件列出来</p>
</blockquote>
<h5 id="2-find-name-test-file">(2) find . -name "test.file"</h5>
<blockquote>
<p>直接查找当前目录下的几个文件</p>
</blockquote>
<h5 id="3-find-empty">(3) find ~ -empty</h5>
<blockquote>
<p>查找home目录下的所有空文件</p>
</blockquote>
<h5 id="4-find-name-test-file-exec-md5sum">(4) find -name "test.file" -exec md5sum {} \;</h5>
<blockquote>
<p>对查找到的文件执行命令,当然也可以执行其他命令</p>
</blockquote>
<h5 id="5-find-type-f-size-0-exec-ls-l">(5) find / -type f -size 0 -exec ls -l {} \;</h5>
<blockquote>
<p>查找系统中所有文件长度为0的普通文件,并列出它们的完整路径</p>
</blockquote>
<h3 id="五-awk">五、awk</h3>
<h4 id="1-用途简介-3">1、用途简介</h4>
<blockquote>
<p>awk是一个很强大的文本分析工具,简单来说就是awk就是把文件逐行的进行读入,</p>
</blockquote>
<p>以空格为默认的文件分隔符,切开的部分再做各种文件分析和处理</p>
<h4 id="2-使用语法">2、使用语法</h4>
<blockquote>
<p>awk [-F field-separator] 'commands' input-files</p>
<p>其中,commands是真正的awk命令,-F域分隔符是可选的,input-files是待处理的文</p>
</blockquote>
<p>件在awk中,文件中的每一行,由域分隔符分开的每一项称为一个域,在不指定-F域分隔符</p>
<p>的情况下,默认是空格</p>
<blockquote>
<p>awk本来就可以看做一门编程语言了,使用也是极其的灵活,等到之后学习的更深入</p>
</blockquote>
<p>的时候再来补充吧</p>
<hr />
</div>
<div class="post-footer">
</div>
</article>
<script src="https://utteranc.es/client.js"
repo="sin-coder/hugocomments"
issue-term="pathname"
theme="github-dark"
crossorigin="anonymous"
async>
</script>
</main>
</body>
</html>