-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinuxbashawkzhong-rsorsfsofsqu-bie-yu-lian-xi.html
442 lines (416 loc) · 15.1 KB
/
linuxbashawkzhong-rsorsfsofsqu-bie-yu-lian-xi.html
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>linux/bash/awk中RS,ORS,FS,OFS区别与联系</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="liugehao">
<!-- Le styles -->
<link rel="stylesheet" href="http://liugehao.github.io/theme/css/bootstrap.min.css" type="text/css" />
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
.sidebar-nav {
padding: 9px 0;
}
.tag-1 {
font-size: 13pt;
}
.tag-2 {
font-size: 10pt;
}
.tag-2 {
font-size: 8pt;
}
.tag-4 {
font-size: 6pt;
}
</style>
<link href="http://liugehao.github.io/theme/css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="http://liugehao.github.io/theme/css/font-awesome.css" rel="stylesheet">
<link href="http://liugehao.github.io/theme/css/pygments.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="http://liugehao.github.io/theme/images/favicon.ico">
<link rel="apple-touch-icon" href="http://liugehao.github.io/theme/images/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="http://liugehao.github.io/theme/images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="http://liugehao.github.io/theme/images/apple-touch-icon-114x114.png">
<link href="http://liugehao.github.io/" type="application/atom+xml" rel="alternate" title="My Note Book ATOM Feed" />
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="http://liugehao.github.io/index.html">My Note Book </a>
<div class="nav-collapse">
<ul class="nav">
<li class="divider-vertical"></li>
<li >
<a href="http://liugehao.github.io/category/c.html">
<i class="icon-folder-open icon-large"></i>c
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/db.html">
<i class="icon-folder-open icon-large"></i>db
</a>
</li>
<li class="active">
<a href="http://liugehao.github.io/category/linux.html">
<i class="icon-folder-open icon-large"></i>linux
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/linux2fe697a0e7babfe7bd91e58da1make3a202a2a2a2flib.html">
<i class="icon-folder-open icon-large"></i>linux(2fe697a0e7babfe7bd91e58da1)make(3a202a2a2a2f)lib
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/nginx.html">
<i class="icon-folder-open icon-large"></i>nginx
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/others.html">
<i class="icon-folder-open icon-large"></i>Others
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/php.html">
<i class="icon-folder-open icon-large"></i>php
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/python.html">
<i class="icon-folder-open icon-large"></i>python
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/ruby.html">
<i class="icon-folder-open icon-large"></i>ruby
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/tmp.html">
<i class="icon-folder-open icon-large"></i>tmp
</a>
</li>
<ul class="nav pull-right">
<li><a href="http://liugehao.github.io/archives.html"><i class="icon-th-list"></i>Archives</a></li>
</ul>
</ul>
<!--<p class="navbar-text pull-right">Logged in as <a href="#">username</a></p>-->
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="span9" id="content">
<section id="content">
<article>
<header>
<h1>
<a href=""
rel="bookmark"
title="Permalink to linux/bash/awk中RS,ORS,FS,OFS区别与联系">
linux/bash/awk中RS,ORS,FS,OFS区别与联系
</a>
</h1>
</header>
<div class="entry-content">
<div class="well">
<footer class="post-info">
<span class="label">Date</span>
<abbr class="published" title="2013-08-27T00:00:00">
<i class="icon-calendar"></i>二 27 八月 2013
</abbr>
<span class="label">By</span>
<a href="http://liugehao.github.io/author/liugehao.html"><i class="icon-user"></i>liugehao</a>
<span class="label">Category</span>
<a href="http://liugehao.github.io/category/linux.html"><i class="icon-folder-open"></i>linux</a>.
</footer><!-- /.post-info --> </div>
<p>学习awk时,一定要记得动手去实践,只有在实践中才能发现问题,以下就我在学习中和实践中的经验,总结一下RS,ORS,FS,OFS的区别和联系。</p>
<p>'''一,RS与ORS'''</p>
<p>1,RS是记录分隔符,默认的分隔符是\n,具体用法看下</p>
<ol>
<li>
<p>[root@krlcgcms01 mytest]# cat test1 /<em> //测试文件 </em>/</p>
</li>
<li>
<p>111 222</p>
</li>
<li>333 444</li>
<li>555 666</li>
</ol>
<p>2,RS默认分割符\n</p>
<ol>
<li>
<p>[root@krlcgcms01 mytest]# awk '{print $0}' test1 /<em> //awk 'BEGIN{RS="\n"}{print $0}' test1 这二个是一样的 </em>/</p>
</li>
<li>
<p>111 222</p>
</li>
<li>333 444</li>
<li>555 666</li>
</ol>
<p>其实你可以把'''上面test1文件里的内容理解为,111 222\n333 444\n555 6666,利用\n进行分割'''。看下一个例子</p>
<p>3,自定义RS分割符</p>
<ol>
<li>
<p>[zhangy@localhost test]$ echo "111 222|333 444|555 666"|awk 'BEGIN{RS="|"}{print $0,RT}'</p>
</li>
<li>
<p>111 222 |</p>
</li>
<li>333 444 |</li>
<li>555 666</li>
</ol>
<p>结合上面一个例子,就很容易理解RS的用法了。</p>
<p>4,RS也可能是正则表达式</p>
<ol>
<li>
<p>[zhangy@localhost test]$ echo "111 222a333 444b555 666"|awk 'BEGIN{RS="[a-z]+"}{print $1,RS,RT}'</p>
</li>
<li>
<p>111 [a-z]+ a</p>
</li>
<li>333 [a-z]+ b</li>
<li>555 [a-z]+</li>
</ol>
<p>从例3和例4,我们可以发现一点,'''当RT是利用RS匹配出来的内容。如果RS是某个固定的值时,RT就是RS的内容'''。</p>
<p>5,RS为空时</p>
<ol>
<li>
<p>[zhangy@localhost test]$ cat -n test2</p>
</li>
<li>
<p>1 111 222</p>
</li>
<li>2</li>
<li>3 333 444</li>
<li>4 333 444</li>
<li>5</li>
<li>6</li>
<li>7 555 666</li>
<li>[zhangy@localhost test]$ awk 'BEGIN{RS=""}{print $0}' test2</li>
<li>111 222</li>
<li>333 444</li>
<li>333 444</li>
<li>555 666</li>
<li>[zhangy@localhost test]$ awk 'BEGIN{RS="";}{print "<",$0,">"}' test2 /<em> //这个例子看着比较明显 </em>/</li>
<li>< 111 222 ></li>
<li>< 333 444 /<em> //这一行和下面一行,是一行 </em>/</li>
<li>333 444 ></li>
<li>< 555 666 ></li>
</ol>
<p>从这个例子,'''可以看出当RS为空时,awk会自动以多行来做为分割符'''。</p>
<p>6,ORS记录输出分符符,默认值是\n</p>
<p>'''把ORS理解成RS反过程,这样更容易记忆和理解''',看下面的例子。</p>
<ol>
<li>
<p>[zhangy@localhost test]$ awk 'BEGIN{ORS="\n"}{print $0}' test1 /<em> //awk '{print $0}' test1二者是一样的 </em>/</p>
</li>
<li>
<p>111 222</p>
</li>
<li>333 444</li>
<li>555 666</li>
<li>[zhangy@localhost test]$ awk 'BEGIN{ORS="|"}{print $0}' test1</li>
<li>111 222|333 444|555 666|</li>
</ol>
<p>'''二,FS与OFS'''</p>
<p>1,FS指定列分割符</p>
<ol>
<li>
<p>[zhangy@localhost test]$ echo "111|222|333"|awk '{print $1}'</p>
</li>
<li>
<p>111|222|333</p>
</li>
<li>[zhangy@localhost test]$ echo "111|222|333"|awk 'BEGIN{FS="|"}{print $1}'</li>
<li>111</li>
</ol>
<p>2,FS也可以用正则</p>
<ol>
<li>
<p>[zhangy@localhost test]$ echo "111||222|333"|awk 'BEGIN{FS="[|]+"}{print $1}'</p>
</li>
<li>
<p>111</p>
</li>
</ol>
<p>3,FS为空的时候</p>
<ol>
<li>
<p>[zhangy@localhost test]$ echo "111|222|333"|awk 'BEGIN{FS=""}{NF++;print $0}'</p>
</li>
<li>
<p>1 1 1 | 2 2 2 | 3 3 3</p>
</li>
</ol>
<p>'''当FS为空的时候,awk会把一行中的每个字符,当成一列来处理'''。</p>
<p>4,RS被设定成非\n时,\n会成FS分割符中的一个</p>
<ol>
<li>
<p>[zhangy@localhost test]$ cat test1</p>
</li>
<li>
<p>111 222</p>
</li>
<li>333 444</li>
<li>555 666</li>
<li>[zhangy@localhost test]$ awk 'BEGIN{RS="444";}{print $2,$3}' test1</li>
<li>222 333</li>
<li>666</li>
</ol>
<p>'''222和333之间是有一个\n的,当RS设定成444后,222和333被认定成同一行的二列了,其实按常规思想是二行的一列才对'''。</p>
<p>5,OFS列输出分隔符</p>
<ol>
<li>
<p>[zhangy@localhost test]$ awk 'BEGIN{OFS="|";}{print $1,$2}' test1</p>
</li>
<li>
<p>111|222</p>
</li>
<li>333|444</li>
<li>555|666</li>
<li>[zhangy@localhost test]$ awk 'BEGIN{OFS="|";}{print $1 OFS $2}' test1</li>
<li>111|222</li>
<li>333|444</li>
<li>555|666</li>
</ol>
<p>'''test1只有二列,如果100列,都写出来太麻烦了吧。'''</p>
<ol>
<li>
<p>[zhangy@localhost test]$ awk 'BEGIN{OFS="|";}{print $0}' test1</p>
</li>
<li>
<p>111 222</p>
</li>
<li>333 444</li>
<li>555 666</li>
<li>[zhangy@localhost test]$ awk 'BEGIN{OFS="|";}{NF=NF;print $0}' test1</li>
<li>111|222</li>
<li>333|444</li>
<li>555|666</li>
</ol>
</div><!-- /.entry-content -->
<div class="comments">
<h2>Comments !</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_identifier = "linuxbashawkzhong-rsorsfsofsqu-bie-yu-lian-xi.html";
(function() {
var dsq = document.createElement('script');
dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://liugehaonotebook.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] ||
document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
</div>
</article>
</section>
</div><!--/span-->
<div class="span3 well sidebar-nav" id="sidebar">
<ul class="nav nav-list">
<li class="nav-header"><h4><i class="icon-external-link"></i>blogroll</h4></li>
<li><a href="http://getpelican.com/"><i class="icon-external-link"></i>Pelican</a></li>
<li><a href="http://python.org/"><i class="icon-external-link"></i>Python.org</a></li>
<li><a href="http://jinja.pocoo.org/"><i class="icon-external-link"></i>Jinja2</a></li>
<li><a href="#"><i class="icon-external-link"></i>You can modify those links in your config file</a></li>
<li class="nav-header"><h4><i class="icon-home icon-large"></i> social</h4></li>
<li><a href="http://liugehao.github.io/" rel="alternate"><i class="icon-bookmark icon-large"></i>atom feed</a></li>
<li><a href="#"><i class="icon-You can add links in your config file-sign icon-large"></i>You can add links in your config file</a></li>
<li><a href="#"><i class="icon-Another social link-sign icon-large"></i>Another social link</a></li>
<li class="nav-header"><h4><i class="icon-folder-close icon-large"></i>Categories</h4></li>
<li>
<a href="http://liugehao.github.io/category/c.html">
<i class="icon-folder-open icon-large"></i>c
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/db.html">
<i class="icon-folder-open icon-large"></i>db
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/linux.html">
<i class="icon-folder-open icon-large"></i>linux
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/linux2fe697a0e7babfe7bd91e58da1make3a202a2a2a2flib.html">
<i class="icon-folder-open icon-large"></i>linux(2fe697a0e7babfe7bd91e58da1)make(3a202a2a2a2f)lib
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/nginx.html">
<i class="icon-folder-open icon-large"></i>nginx
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/others.html">
<i class="icon-folder-open icon-large"></i>Others
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/php.html">
<i class="icon-folder-open icon-large"></i>php
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/python.html">
<i class="icon-folder-open icon-large"></i>python
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/ruby.html">
<i class="icon-folder-open icon-large"></i>ruby
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/tmp.html">
<i class="icon-folder-open icon-large"></i>tmp
</a>
</li>
<li class="nav-header"><h4><i class="icon-tags icon-large"></i>Tags</h4></li>
</ul> </div><!--/.well -->
</div><!--/row-->
<hr>
<footer>
<address id="about">
Proudly powered by <a href="http://pelican.notmyidea.org/">Pelican <i class="icon-external-link"></i></a>,
which takes great advantage of <a href="http://python.org">Python <i class="icon-external-link"></i></a>.
</address><!-- /#about -->
<p>The theme is from <a href="http://twitter.github.com/bootstrap/">Bootstrap from Twitter <i class="icon-external-link"></i></a>,
and <a href="http://fortawesome.github.com/Font-Awesome/">Font-Awesome <i class="icon-external-link"></i></a>, thanks!</p>
</footer>
</div><!--/.fluid-container-->
<script type="text/javascript">
var disqus_shortname = 'liugehaonotebook';
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
<!-- Le javascript -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="http://liugehao.github.io/theme/js/jquery-1.7.2.min.js"></script>
<script src="http://liugehao.github.io/theme/js/bootstrap.min.js"></script>
</body>
</html>