-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecute.html
More file actions
665 lines (378 loc) · 26 KB
/
execute.html
File metadata and controls
665 lines (378 loc) · 26 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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
<!DOCTYPE HTML>
<html lang="ko" >
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-161001174-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-161001174-2');
</script>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Execute | Introduction</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.6.7">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
<link rel="stylesheet" href="./styles/website.css">
<link rel="next" href="./command_line_options.html" />
<link rel="prev" href="./multiple_lines.html" />
</head>
<body>
<div class="book"
data-level="8"
data-chapter-title="Execute"
data-filepath="execute.md"
data-basepath="."
data-innerlanguage="">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
Introduction
</a>
</li>
<li class="chapter " data-level="1" data-path="bug_reports.html">
<a href="./bug_reports.html">
<i class="fa fa-check"></i>
<b>1.</b>
Bug reports
</a>
</li>
<li class="chapter " data-level="2" data-path="basics.html">
<a href="./basics.html">
<i class="fa fa-check"></i>
<b>2.</b>
Basics
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="regex.html">
<a href="./regex.html">
<i class="fa fa-check"></i>
<b>2.1.</b>
Regex
</a>
</li>
<li class="chapter " data-level="2.2" data-path="escape_sequences.html">
<a href="./escape_sequences.html">
<i class="fa fa-check"></i>
<b>2.2.</b>
Escape sequences
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="addresses.html">
<a href="./addresses.html">
<i class="fa fa-check"></i>
<b>3.</b>
Addresses
</a>
</li>
<li class="chapter " data-level="4" data-path="and_or_not.html">
<a href="./and_or_not.html">
<i class="fa fa-check"></i>
<b>4.</b>
AND, OR, NOT
</a>
</li>
<li class="chapter " data-level="5" data-path="commands.html">
<a href="./commands.html">
<i class="fa fa-check"></i>
<b>5.</b>
Commands
</a>
<ul class="articles">
<li class="chapter " data-level="5.1" data-path="print.html">
<a href="./print.html">
<i class="fa fa-check"></i>
<b>5.1.</b>
print
</a>
</li>
<li class="chapter " data-level="5.2" data-path="delete.html">
<a href="./delete.html">
<i class="fa fa-check"></i>
<b>5.2.</b>
delete
</a>
</li>
<li class="chapter " data-level="5.3" data-path="quit.html">
<a href="./quit.html">
<i class="fa fa-check"></i>
<b>5.3.</b>
quit
</a>
</li>
<li class="chapter " data-level="5.4" data-path="substitute.html">
<a href="./substitute.html">
<i class="fa fa-check"></i>
<b>5.4.</b>
substitute
</a>
</li>
<li class="chapter " data-level="5.5" data-path="next.html">
<a href="./next.html">
<i class="fa fa-check"></i>
<b>5.5.</b>
next
</a>
</li>
<li class="chapter " data-level="5.6" data-path="move.html">
<a href="./move.html">
<i class="fa fa-check"></i>
<b>5.6.</b>
move between spaces
</a>
</li>
<li class="chapter " data-level="5.7" data-path="insert_append_change.html">
<a href="./insert_append_change.html">
<i class="fa fa-check"></i>
<b>5.7.</b>
insert, append, change
</a>
</li>
<li class="chapter " data-level="5.8" data-path="read_write.html">
<a href="./read_write.html">
<i class="fa fa-check"></i>
<b>5.8.</b>
read, write
</a>
</li>
<li class="chapter " data-level="5.9" data-path="etc.html">
<a href="./etc.html">
<i class="fa fa-check"></i>
<b>5.9.</b>
etc
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="6" data-path="branch.html">
<a href="./branch.html">
<i class="fa fa-check"></i>
<b>6.</b>
Branch
</a>
</li>
<li class="chapter " data-level="7" data-path="multiple_lines.html">
<a href="./multiple_lines.html">
<i class="fa fa-check"></i>
<b>7.</b>
Multiple lines
</a>
</li>
<li class="chapter active" data-level="8" data-path="execute.html">
<a href="./execute.html">
<i class="fa fa-check"></i>
<b>8.</b>
Execute
</a>
</li>
<li class="chapter " data-level="9" data-path="command_line_options.html">
<a href="./command_line_options.html">
<i class="fa fa-check"></i>
<b>9.</b>
Command-line options
</a>
</li>
<li class="chapter " data-level="10" data-path="debugging.html">
<a href="./debugging.html">
<i class="fa fa-check"></i>
<b>10.</b>
Debugging
</a>
</li>
<li class="chapter " data-level="11" data-path="tips.html">
<a href="./tips.html">
<i class="fa fa-check"></i>
<b>11.</b>
Tips
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Actions Left -->
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >Introduction</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<h1 id="execute"><img src="images/buttons/e.png" \=""> Execute</h1>
<p>이것은 sed 가지는 재미있는 기능 중 하나인데요.
pattern space 에 있는 내용을 명령문으로 해서 <code>sh -c</code> 형태로 실행을 시킬 수가 있습니다.
그리고 실행 결과가 다시 pattern space 로 저장됩니다.
그러므로 원하는 대로 또 결과를 수정해서 사용할 수 있겠죠?</p>
<p>다음은 명령을 실행했을 때 프로세스 상태입니다.</p>
<pre><code class="lang-bash">$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"2:3"</span> | sed -E <span class="hljs-string">'s#([0-9]+):([0-9]+)#AA=$((\1+\2)); sleep $AA; echo $AA#e'</span>
<span class="hljs-number">5</span>
</code></pre>
<p><img src="images/execute_1.png" alt=""></p>
<blockquote>
<p>프로세스가 새로 생성되어 실행되므로 반복해서 여러번 실행할 경우 속도가 느려질 수 있습니다.</p>
</blockquote>
<p><code>e</code> 명령이 실행되는 형태를 2가지로 분류해 볼수 있습니다.</p>
<h4 id="1-se-를-이용하는-방법-단독으로-e-명령을-실행하는-방법">1. s/../../e 를 이용하는 방법, 단독으로 e 명령을 실행하는 방법</h4>
<p>이 방법들은 현재 pattern space 에 있는 내용을 명령문으로 하여 <code>sh -c</code> 형태로 실행하고
stdout 값이 다시 pattern space 로 저장됩니다.
단독으로 <code>e</code> 명령을 실행할땐 이후에 다른 문자가 오지 못하므로
<code>-e</code> 옵션을 사용해 분리해야 합니다.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># 'echo hello' 가 pattern space 로 들어가고 'e' 명령에 의해 실행된다.</span>
<span class="hljs-comment"># 실행 결과 stdout 값이 다시 pattern space 로 저장된다.</span>
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"echo hello"</span> | sed <span class="hljs-operator">-e</span> <span class="hljs-string">'e'</span> <span class="hljs-operator">-e</span> <span class="hljs-string">'s/.*/& execute/'</span>
hello execute
</code></pre>
<h4 id="2-pattern-space-와-관계없이-e-command-형식으로-직접-실행하는-방법">2. pattern space 와 관계없이 <code>e command</code> 형식으로 직접 실행하는 방법</h4>
<p>이 실행 방식은 <code>i</code> 명령과 동일합니다.
실행 결과가 patten space 에 영향을 주지 않고, <code>-n</code> 옵션에 상관없이 결과가 프린트됩니다.
명령문으로 사용되는 스트링은 어떤 값도 올수 있기 때문에 <code>-e</code> 옵션을 사용해 분리해야 합니다.</p>
<pre><code class="lang-bash">$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"hello"</span> |
sed -n <span class="hljs-operator">-e</span> <span class="hljs-string">'e echo ------------'</span> <span class="hljs-operator">-e</span> <span class="hljs-string">'s/.*/& execute/p'</span> <span class="hljs-operator">-e</span> <span class="hljs-string">'e echo ============'</span>
------------
hello execute
============
</code></pre>
<hr>
<h3 id="예제-1">예제 1</h3>
<p>다음 예제는 숫자가 포함된 데이터 파일을 읽어들여 max 값을 구한 후 출력합니다.<br>특별히 의미 있는 예제는 아니지만 여러 가지로 활용하는 방법을 보여줍니다.</p>
<blockquote>
<p>sed 에서 사용되는 <code>&</code> , <code>\</code> 문자를 shell 에서 사용하려면 escape 해야 합니다.</p>
</blockquote>
<pre><code class="lang-bash"><span class="hljs-comment"># 데이터 파일</span>
$ cat data
<span class="hljs-number">43</span>
<span class="hljs-number">2</span>
<span class="hljs-number">85</span>
<span class="hljs-number">12</span>
<span class="hljs-number">5</span>
<span class="hljs-number">999</span>
<span class="hljs-number">7</span>
<span class="hljs-number">26</span>
------------------------
$ sed -En <span class="hljs-string">'
1{h;b} # 처음 숫자일 경우 hold 하고 END 로 분기합니다.
G # 현재 pattern space 에 있는 숫자에 hold space 에 있는 숫자를 append 합니다.
s/\n/ / # newline 을 space 로 변경하여 shell 에서 사용될 수 있게 합니다.
s#.*#set &; if [ $1 -gt $2 ]; then echo $1; else echo $2; fi #e
h # 연산 결과를 hold 합니다.
$p # 마지막 라인일 경우 값을 표시합니다.
'</span> data
<span class="hljs-number">999</span>
------------------------
$ sed -En <span class="hljs-string">'
1{h;b}
G
s/\n/ / # f() 쉘 함수를 만들어 실행
s#.*#f() { if [ $1 -gt $2 ]; then echo $1; else echo $2; fi ;}; f & #e
h
$p
'</span> data
<span class="hljs-number">999</span>
------------------------
<span class="hljs-comment"># comp.sh script 를 만들어 실행</span>
$ cat comp.sh
<span class="hljs-shebang">#!/bin/sh
</span>
<span class="hljs-keyword">if</span> [ <span class="hljs-string">"<span class="hljs-variable">$1</span>"</span> <span class="hljs-operator">-gt</span> <span class="hljs-string">"<span class="hljs-variable">$2</span>"</span> ]; <span class="hljs-keyword">then</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>
<span class="hljs-keyword">else</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$2</span>"</span>
<span class="hljs-keyword">fi</span>
..........................
$ sed -En <span class="hljs-string">'
1{h;b}
G
s/\n/ /
s#.*#./comp.sh &#e # comp.sh 스크립트를 실행
h
$p
'</span> data
<span class="hljs-number">999</span>
</code></pre>
<h3 id="예제-2">예제 2</h3>
<p>이번에는 각 라인의 합계를 구합니다.</p>
<pre><code class="lang-bash">$ shuf -i <span class="hljs-number">1</span>-<span class="hljs-number">20</span> | paste - - - - -
<span class="hljs-number">16</span> <span class="hljs-number">7</span> <span class="hljs-number">12</span> <span class="hljs-number">20</span> <span class="hljs-number">17</span>
<span class="hljs-number">2</span> <span class="hljs-number">4</span> <span class="hljs-number">9</span> <span class="hljs-number">3</span> <span class="hljs-number">6</span>
<span class="hljs-number">19</span> <span class="hljs-number">1</span> <span class="hljs-number">11</span> <span class="hljs-number">8</span> <span class="hljs-number">13</span>
<span class="hljs-number">15</span> <span class="hljs-number">14</span> <span class="hljs-number">10</span> <span class="hljs-number">18</span> <span class="hljs-number">5</span>
$ shuf -i <span class="hljs-number">1</span>-<span class="hljs-number">20</span> | paste - - - - - |
sed -E <span class="hljs-string">'
p
# `s` 명령에 의해 다음과 같은 스트링이 `sh -c` 에 의해 실행됩니다.
# f() { for n; do r=$((r+n)); done; echo "sum : $r" ;}; f 7 12 1 10 16
s/^/f() { for n; do r=$((r+n)); done; echo "sum : $r" ;}; f /e
'</span>
<span class="hljs-number">7</span> <span class="hljs-number">12</span> <span class="hljs-number">1</span> <span class="hljs-number">10</span> <span class="hljs-number">16</span>
sum : <span class="hljs-number">46</span>
<span class="hljs-number">11</span> <span class="hljs-number">2</span> <span class="hljs-number">18</span> <span class="hljs-number">17</span> <span class="hljs-number">8</span>
sum : <span class="hljs-number">56</span>
<span class="hljs-number">13</span> <span class="hljs-number">5</span> <span class="hljs-number">4</span> <span class="hljs-number">3</span> <span class="hljs-number">15</span>
sum : <span class="hljs-number">40</span>
<span class="hljs-number">19</span> <span class="hljs-number">6</span> <span class="hljs-number">9</span> <span class="hljs-number">20</span> <span class="hljs-number">14</span>
sum : <span class="hljs-number">68</span>
</code></pre>
<h3 id="예제-3">예제 3</h3>
<p>다음은 데이터 파일에 있는 도메인의 ip 주소를 구하여 추가합니다.</p>
<pre><code class="lang-bash">$ cat host
<span class="hljs-number">001</span> naver.com KR
<span class="hljs-number">002</span> daum.net KR
<span class="hljs-number">003</span> google.com US
$ sed -En <span class="hljs-string">'
h
s#.* (.*) .*#dig +short \1 | head -n1#e
G
s#(.*)\n(.*) (.*) (.*)#\2 \3 : \1 \4 #
p
'</span> host
<span class="hljs-number">001</span> naver.com : <span class="hljs-number">202.179</span>.<span class="hljs-number">177.22</span> KR
<span class="hljs-number">002</span> daum.net : <span class="hljs-number">61.111</span>.<span class="hljs-number">62.173</span> KR
<span class="hljs-number">003</span> google.com : <span class="hljs-number">216.58</span>.<span class="hljs-number">197.142</span> US
</code></pre>
</section>
</div>
</div>
</div>
<a href="./multiple_lines.html" class="navigation navigation-prev " aria-label="Previous page: Multiple lines"><i class="fa fa-angle-left"></i></a>
<a href="./command_line_options.html" class="navigation navigation-next " aria-label="Next page: Command-line options"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="gitbook/app.js"></script>
<script src="gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
<script src="gitbook/plugins/gitbook-plugin-search/search.js"></script>
<script src="gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
gitbook.start(config);
});
</script>
</body>
</html>