-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathzero_one.html
975 lines (570 loc) · 48.5 KB
/
zero_one.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
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
<!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-3"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-161001174-3');
</script>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>$0=$0, $1=$1 | 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="next" href="./redirection.html" />
<link rel="prev" href="./field_sep.html" />
</head>
<body>
<div class="book"
data-level="6"
data-chapter-title="$0=$0, $1=$1"
data-filepath="zero_one.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="data_types.html">
<a href="./data_types.html">
<i class="fa fa-check"></i>
<b>2.1.</b>
Data Types
</a>
</li>
<li class="chapter " data-level="2.2" data-path="variables.html">
<a href="./variables.html">
<i class="fa fa-check"></i>
<b>2.2.</b>
Variables
</a>
</li>
<li class="chapter " data-level="2.3" data-path="regex.html">
<a href="./regex.html">
<i class="fa fa-check"></i>
<b>2.3.</b>
Regex
</a>
</li>
<li class="chapter " data-level="2.4" data-path="functions.html">
<a href="./functions.html">
<i class="fa fa-check"></i>
<b>2.4.</b>
Functions
</a>
</li>
<li class="chapter " data-level="2.5" data-path="escape_sequences.html">
<a href="./escape_sequences.html">
<i class="fa fa-check"></i>
<b>2.5.</b>
Escape sequences
</a>
</li>
<li class="chapter " data-level="2.6" data-path="begin_end.html">
<a href="./begin_end.html">
<i class="fa fa-check"></i>
<b>2.6.</b>
BEGIN, END
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="builtin_variables.html">
<a href="./builtin_variables.html">
<i class="fa fa-check"></i>
<b>3.</b>
Built-in Variables
</a>
</li>
<li class="chapter " data-level="4" data-path="record_sep.html">
<a href="./record_sep.html">
<i class="fa fa-check"></i>
<b>4.</b>
Record 분리
</a>
</li>
<li class="chapter " data-level="5" data-path="field_sep.html">
<a href="./field_sep.html">
<i class="fa fa-check"></i>
<b>5.</b>
Field 분리
</a>
</li>
<li class="chapter active" data-level="6" data-path="zero_one.html">
<a href="./zero_one.html">
<i class="fa fa-check"></i>
<b>6.</b>
$0=$0, $1=$1
</a>
</li>
<li class="chapter " data-level="7" data-path="redirection.html">
<a href="./redirection.html">
<i class="fa fa-check"></i>
<b>7.</b>
Redirection
</a>
</li>
<li class="chapter " data-level="8" data-path="getline.html">
<a href="./getline.html">
<i class="fa fa-check"></i>
<b>8.</b>
getline
</a>
</li>
<li class="chapter " data-level="9" data-path="arrays.html">
<a href="./arrays.html">
<i class="fa fa-check"></i>
<b>9.</b>
Arrays
</a>
<ul class="articles">
<li class="chapter " data-level="9.1" data-path="sort.html">
<a href="./sort.html">
<i class="fa fa-check"></i>
<b>9.1.</b>
Sort
</a>
</li>
<li class="chapter " data-level="9.2" data-path="GROUP_BY.html">
<a href="./GROUP_BY.html">
<i class="fa fa-check"></i>
<b>9.2.</b>
GROUP BY
</a>
</li>
<li class="chapter " data-level="9.3" data-path="JOIN.html">
<a href="./JOIN.html">
<i class="fa fa-check"></i>
<b>9.3.</b>
JOIN
</a>
</li>
<li class="chapter " data-level="9.4" data-path="subquery.html">
<a href="./subquery.html">
<i class="fa fa-check"></i>
<b>9.4.</b>
Subquery
</a>
</li>
<li class="chapter " data-level="9.5" data-path="index2.html">
<a href="./index2.html">
<i class="fa fa-check"></i>
<b>9.5.</b>
Index
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="10" data-path="control_statements.html">
<a href="./control_statements.html">
<i class="fa fa-check"></i>
<b>10.</b>
Control Statements
</a>
</li>
<li class="chapter " data-level="11" data-path="operators.html">
<a href="./operators.html">
<i class="fa fa-check"></i>
<b>11.</b>
Operators
</a>
</li>
<li class="chapter " data-level="12" data-path="builtin_functions.html">
<a href="./builtin_functions.html">
<i class="fa fa-check"></i>
<b>12.</b>
Built-in Functions
</a>
<ul class="articles">
<li class="chapter " data-level="12.1" data-path="builtin/numeric.html">
<a href="./builtin/numeric.html">
<i class="fa fa-check"></i>
<b>12.1.</b>
Numeric Functions
</a>
</li>
<li class="chapter " data-level="12.2" data-path="builtin/string.html">
<a href="./builtin/string.html">
<i class="fa fa-check"></i>
<b>12.2.</b>
String Functions
</a>
</li>
<li class="chapter " data-level="12.3" data-path="builtin/io.html">
<a href="./builtin/io.html">
<i class="fa fa-check"></i>
<b>12.3.</b>
I/O Functions
</a>
</li>
<li class="chapter " data-level="12.4" data-path="builtin/time.html">
<a href="./builtin/time.html">
<i class="fa fa-check"></i>
<b>12.4.</b>
Time Functions
</a>
</li>
<li class="chapter " data-level="12.5" data-path="builtin/bitwise.html">
<a href="./builtin/bitwise.html">
<i class="fa fa-check"></i>
<b>12.5.</b>
Bitwise Functions
</a>
</li>
<li class="chapter " data-level="12.6" data-path="builtin/type.html">
<a href="./builtin/type.html">
<i class="fa fa-check"></i>
<b>12.6.</b>
Type Functions
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="13" data-path="extensions.html">
<a href="./extensions.html">
<i class="fa fa-check"></i>
<b>13.</b>
Dynamic Extensions
</a>
<ul class="articles">
<li class="chapter " data-level="13.1" data-path="example.html">
<a href="./example.html">
<i class="fa fa-check"></i>
<b>13.1.</b>
예제
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="14" data-path="tcp_ip.html">
<a href="./tcp_ip.html">
<i class="fa fa-check"></i>
<b>14.</b>
TCP/IP Internetworking
</a>
</li>
<li class="chapter " data-level="15" data-path="command_line_options.html">
<a href="./command_line_options.html">
<i class="fa fa-check"></i>
<b>15.</b>
Command-line options
</a>
</li>
<li class="chapter " data-level="16" data-path="debugging.html">
<a href="./debugging.html">
<i class="fa fa-check"></i>
<b>16.</b>
Debugging
</a>
</li>
<li class="chapter " data-level="17" data-path="tips.html">
<a href="./tips.html">
<i class="fa fa-check"></i>
<b>17.</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-">
<h2 id="0--0-1--1">$0 = $0, $1 = $1</h2>
<p>레코드 값을 다시 레코드 변수에 대입하고, 필드 값을 다시 필드 변수에 대입하는 것은
아무 의미가 없어 보이는데요.
이것은 awk 에서 필드가 분리되어 필드값이 설정되고 레코드 값이 설정되는 내부 작동 방식에 관계된 내용입니다.
스크립트를 오류 없이 작성하기 위해서 알고 있어야 할 필요가 있습니다.
타이틀은 $0=$0, $1=$1 로 작성하였지만 꼭 $0, $1 값을 대입에 사용해야 되는 것은 아니고
어떤 값이든 $0 나 $1, $2, $3 ... 변수에 대입 연산을 할 때 적용되는 개념입니다.
( <code>++</code> <code>--</code> 연산도 포함 )</p>
<blockquote>
<p>$0 = $0 는 FS 에따라 FIELDS ( $1, $2, $3 ... NF ) 재설정 !<br>$1 = $1 는 OFS 에따라 RECORD ( $0 ) 재설정 !</p>
</blockquote>
<p>먼저 스크립트 내에서 FS 값을 변경했을 경우 적용되는 시점에 대해 알아보겠습니다.</p>
<pre><code class="lang-bash">$ cat file
aa=<span class="hljs-number">11</span>,bb=<span class="hljs-number">22</span>,cc=<span class="hljs-number">33</span>
dd=<span class="hljs-number">44</span>,ee=<span class="hljs-number">55</span>,ff=<span class="hljs-number">66</span>
gg=<span class="hljs-number">77</span>,hh=<span class="hljs-number">88</span>,ii=<span class="hljs-number">99</span>
-----------------
$ awk <span class="hljs-string">'{ printf "(%s) (%s) (%s)\n", $1,$2,$3 }'</span> FS=, file
(aa=<span class="hljs-number">11</span>) (bb=<span class="hljs-number">22</span>) (cc=<span class="hljs-number">33</span>)
(dd=<span class="hljs-number">44</span>) (ee=<span class="hljs-number">55</span>) (ff=<span class="hljs-number">66</span>)
(gg=<span class="hljs-number">77</span>) (hh=<span class="hljs-number">88</span>) (ii=<span class="hljs-number">99</span>)
<span class="hljs-comment"># 코드 실행 중에 FS="=" 로 변경</span>
$ awk <span class="hljs-string">'{ printf "(%s) (%s) (%s)\n", $1,$2,$3 }
NR==1 { FS="="
printf "(%s) (%s) (%s)\n", $1,$2,$3 }
'</span> FS=, file
(aa=<span class="hljs-number">11</span>) (bb=<span class="hljs-number">22</span>) (cc=<span class="hljs-number">33</span>)
(aa=<span class="hljs-number">11</span>) (bb=<span class="hljs-number">22</span>) (cc=<span class="hljs-number">33</span>) <span class="hljs-comment"># FS="=" 로 변경했지만 필드값이 변경되지 않는다.</span>
(dd) (<span class="hljs-number">44</span>,ee) (<span class="hljs-number">55</span>,ff) <span class="hljs-comment"># 다음 레코드 입력부터 FS="=" 값이 적용된다.</span>
(gg) (<span class="hljs-number">77</span>,hh) (<span class="hljs-number">88</span>,ii)
</code></pre>
<h3 id="0-변수에-대입-연산">$0 변수에 대입 연산</h3>
<p>위 예제에서 코드 실행 중에 <code>FS="="</code> 로 변경하였지만 이어지는 프린트문에서는
필드값이 변경되지 않고 이전과 동일한 값이 표시되는 것을 볼 수 있습니다.</p>
<p>레코드에서 필드가 분리되어 필드 변수가 ( $1, $2, $3 ... NF ) 설정되는 시점은
레코드 값이 <code>$0</code> 변수에 대입될 때입니다.
그러므로 명령 사이클을 시작하기 위해 입력 스트림으로부터 레코드를 읽어들여 <code>$0</code> 변수에 대입할 때나,
또는 <code>gsub(/foo/,"bar")</code> 같은 스트링 함수를 실행하여 결과가 <code>$0</code> 에 대입되는 경우 모두에 해당됩니다.
따라서 다음과 같이 코드 실행 중에 FS 값을 변경한 후에 <code>$0=$0</code> 를 해주게 되면
새로운 FS 값에 따라 다시 필드가 분리되어 변수값이 설정됩니다.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># 처음 FS 값은 ','</span>
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">'aa=11,bb=22,cc=33'</span> |
awk <span class="hljs-string">'{ printf "(%s) (%s) (%s)\n", $1,$2,$3
FS="="; $0=$0
printf "(%s) (%s) (%s) (%s)\n", $1,$2,$3,$4 }
'</span> FS=,
(aa=<span class="hljs-number">11</span>) (bb=<span class="hljs-number">22</span>) (cc=<span class="hljs-number">33</span>)
(aa) (<span class="hljs-number">11</span>,bb) (<span class="hljs-number">22</span>,cc) (<span class="hljs-number">33</span>) <span class="hljs-comment"># 필드값이 FS="=" 에 따라 재설정 되었다.</span>
</code></pre>
<h3 id="필드-변수에-대입-연산">필드 변수에 대입 연산</h3>
<p>FS 문자가 <code>;</code> 인 레코드를 출력할 때 <code>@</code> 문자로 바꾸어 출력하려면 어떻게 할까요?
아래 첫 번째 경우처럼 하게 되면 정상적으로 실행되지 않는 것을 볼 수 있습니다.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># 정상적으로 실행되지 않는다.</span>
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"11;22;33"</span> | awk -vFS=<span class="hljs-string">';'</span> -vOFS=<span class="hljs-string">'@'</span> <span class="hljs-number">1</span>
<span class="hljs-number">11</span>;<span class="hljs-number">22</span>;<span class="hljs-number">33</span>
<span class="hljs-comment"># 다음과 같이 해야 합니다.</span>
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"11;22;33"</span> | awk -vFS=<span class="hljs-string">';'</span> -vOFS=<span class="hljs-string">'@'</span> <span class="hljs-string">'{ print $1,$2,$3 }'</span>
<span class="hljs-number">11</span>@<span class="hljs-number">22</span>@<span class="hljs-number">33</span>
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"11;22;33"</span> | awk -vFS=<span class="hljs-string">';'</span> -vOFS=<span class="hljs-string">'@'</span> <span class="hljs-string">'{$1=$1}1'</span>
<span class="hljs-number">11</span>@<span class="hljs-number">22</span>@<span class="hljs-number">33</span>
</code></pre>
<p><code>$1=$1</code>, <code>$2+=5</code>, <code>$3=$7=""</code> 와 같이 필드 변수에 대입 연산을 하게 되면 레코드 값에 해당하는 <code>$0</code> 값이 OFS 에 따라 재설정됩니다.
이때는 <code>$0</code> 변수에 대입 연산을 하는 것이 아니므로 필드 변수들이 FS 에의해 재설정되지는 않습니다.
다시 말해서 <code>$0</code> 값은 변경되지만 기존의 필드 변수값들은 그대로 남아있습니다.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># 필드 변수에 대입 연산이 하는 역할은 실질적으로 다음과 같습니다.</span>
<span class="hljs-variable">$0</span> = <span class="hljs-variable">$1</span> OFS <span class="hljs-variable">$2</span> OFS <span class="hljs-variable">$3</span> OFS ...
</code></pre>
<pre><code class="lang-bash">$ <span class="hljs-built_in">echo</span> <span class="hljs-string">" 111 222 333"</span> |
awk <span class="hljs-string">'{ printf "(%s) (%s) (%s)\n",$1,$2,$3
printf "(%s)\n", $0 }'</span>
(<span class="hljs-number">111</span>) (<span class="hljs-number">222</span>) (<span class="hljs-number">333</span>) <span class="hljs-comment"># 필드값은 제대로 분리되지만</span>
( <span class="hljs-number">111</span> <span class="hljs-number">222</span> <span class="hljs-number">333</span>) <span class="hljs-comment"># $0 값은 원본 그대로 공백이 남아 있다.</span>
..................................................................
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">" 111 222 333"</span> |
awk <span class="hljs-string">'{ printf "(%s) (%s) (%s)\n",$1,$2,$3
$1=$1
printf "(%s)\n", $0 }'</span>
(<span class="hljs-number">111</span>) (<span class="hljs-number">222</span>) (<span class="hljs-number">333</span>)
(<span class="hljs-number">111</span> <span class="hljs-number">222</span> <span class="hljs-number">333</span>) <span class="hljs-comment"># $1=$1 를 실행하면 $0 값이 재설정 된다.</span>
......................................................................
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">" 111 222 333"</span> |
awk <span class="hljs-string">'{ printf "(%s) (%s) (%s)\n",$1,$2,$3
$1=$1
printf "(%s)\n", $0
printf "(%s) (%s) (%s)\n",$1,$2,$3
}'</span> OFS=<span class="hljs-string">':'</span> <span class="hljs-comment"># OFS=':' 값 설정</span>
(<span class="hljs-number">111</span>) (<span class="hljs-number">222</span>) (<span class="hljs-number">333</span>)
(<span class="hljs-number">111</span>:<span class="hljs-number">222</span>:<span class="hljs-number">333</span>) <span class="hljs-comment"># $0 값이 재설정 될 때 OFS 값이 사용되는 것을 볼 수 있습니다.</span>
(<span class="hljs-number">111</span>) (<span class="hljs-number">222</span>) (<span class="hljs-number">333</span>) <span class="hljs-comment"># $0 값이 변경되었지만 기존의 필드값들은 그대로 남아있습니다.</span>
</code></pre>
<p>필드에 대입 연산을 하게 되면 한가지 불편한 점은 $0 값이 재설정됨에 따라
기존의 프린트 포멧이 망가진다는 점입니다.</p>
<pre><code class="lang-bash">$ ls <span class="hljs-operator">-l</span> | awk <span class="hljs-number">1</span>
total <span class="hljs-number">88</span>
-rw-r--r-- <span class="hljs-number">1</span> jack jack <span class="hljs-number">8</span> Jun <span class="hljs-number">19</span> <span class="hljs-number">2013</span> qunit-<span class="hljs-number">1.11</span>.<span class="hljs-number">0</span>.css
-rw-r--r-- <span class="hljs-number">1</span> jack jack <span class="hljs-number">56908</span> Jun <span class="hljs-number">19</span> <span class="hljs-number">2013</span> qunit-<span class="hljs-number">1.11</span>.<span class="hljs-number">0</span>.js
-rw-r--r-- <span class="hljs-number">1</span> jack jack <span class="hljs-number">4306</span> Dec <span class="hljs-number">29</span> <span class="hljs-number">09</span>:<span class="hljs-number">16</span> <span class="hljs-built_in">test</span>1.html
-rw-r--r-- <span class="hljs-number">1</span> jack jack <span class="hljs-number">5476</span> Dec <span class="hljs-number">7</span> <span class="hljs-number">08</span>:<span class="hljs-number">09</span> <span class="hljs-built_in">test</span>1.js
<span class="hljs-comment"># 필드에 대입 연산으로 인해 기존의 프린트 포멧이 망가진다.</span>
$ ls <span class="hljs-operator">-l</span> | awk <span class="hljs-string">'NR != 1 { $5+=100; print }'</span>
-rw-r--r-- <span class="hljs-number">1</span> jack jack <span class="hljs-number">108</span> Jun <span class="hljs-number">19</span> <span class="hljs-number">2013</span> qunit-<span class="hljs-number">1.11</span>.<span class="hljs-number">0</span>.css
-rw-r--r-- <span class="hljs-number">1</span> jack jack <span class="hljs-number">57008</span> Jun <span class="hljs-number">19</span> <span class="hljs-number">2013</span> qunit-<span class="hljs-number">1.11</span>.<span class="hljs-number">0</span>.js
-rw-r--r-- <span class="hljs-number">1</span> jack jack <span class="hljs-number">4406</span> Dec <span class="hljs-number">29</span> <span class="hljs-number">09</span>:<span class="hljs-number">16</span> <span class="hljs-built_in">test</span>1.html
-rw-r--r-- <span class="hljs-number">1</span> jack jack <span class="hljs-number">5576</span> Dec <span class="hljs-number">7</span> <span class="hljs-number">08</span>:<span class="hljs-number">09</span> <span class="hljs-built_in">test</span>1.js
</code></pre>
<p>보통 printf 문을 이용해 출력을 포멧하지만 포멧이 고정적이지 않고
출력되는 값에 따라서 달라질 경우 다음과 같은 방법을 사용할 수 있습니다.</p>
<pre><code class="lang-bash">$ ls <span class="hljs-operator">-l</span> | awk <span class="hljs-string">'NR != 1 { $5 = sprintf( "%*s", length($5), $5+100); print}'</span> \
FPAT=<span class="hljs-string">' *[[:graph:]]+'</span> OFS=<span class="hljs-string">""</span>
-rw-r--r-- <span class="hljs-number">1</span> jack jack <span class="hljs-number">108</span> Jun <span class="hljs-number">19</span> <span class="hljs-number">2013</span> qunit-<span class="hljs-number">1.11</span>.<span class="hljs-number">0</span>.css
-rw-r--r-- <span class="hljs-number">1</span> jack jack <span class="hljs-number">57008</span> Jun <span class="hljs-number">19</span> <span class="hljs-number">2013</span> qunit-<span class="hljs-number">1.11</span>.<span class="hljs-number">0</span>.js
-rw-r--r-- <span class="hljs-number">1</span> jack jack <span class="hljs-number">4406</span> Dec <span class="hljs-number">29</span> <span class="hljs-number">09</span>:<span class="hljs-number">16</span> <span class="hljs-built_in">test</span>1.html
-rw-r--r-- <span class="hljs-number">1</span> jack jack <span class="hljs-number">5576</span> Dec <span class="hljs-number">7</span> <span class="hljs-number">08</span>:<span class="hljs-number">09</span> <span class="hljs-built_in">test</span>1.js
</code></pre>
<h3 id="예제-">예제 )</h3>
<p>FS 가 <code>/</code> 문자인 레코드에서 5 번째 필드값에 +5 하기</p>
<pre><code class="lang-bash"><span class="hljs-comment"># 필드 변수에 대입 연산을 하게 되면 $0 값이 재설정 되므로 OFS=/ 도 설정해야 합니다.</span>
$ awk -F/ <span class="hljs-string">'{ $5+=5 }1'</span> OFS=/ file
<span class="hljs-number">03</span>/Oct/<span class="hljs-number">2016</span>:<span class="hljs-number">06</span>:<span class="hljs-number">39</span>:<span class="hljs-number">50</span>-<span class="hljs-number">0500</span>,<span class="hljs-built_in">cd</span>/base/<span class="hljs-number">5</span>/<span class="hljs-number">48</span>/<span class="hljs-number">2</span>.png,<span class="hljs-number">206</span>,<span class="hljs-number">1514</span>
<span class="hljs-number">03</span>/Oct/<span class="hljs-number">2016</span>:<span class="hljs-number">06</span>:<span class="hljs-number">39</span>:<span class="hljs-number">50</span>-<span class="hljs-number">0500</span>,<span class="hljs-built_in">cd</span>/base/<span class="hljs-number">6</span>/<span class="hljs-number">46</span>/<span class="hljs-number">3</span>.png,<span class="hljs-number">206</span>,<span class="hljs-number">5796</span>
<span class="hljs-number">03</span>/Oct/<span class="hljs-number">2016</span>:<span class="hljs-number">06</span>:<span class="hljs-number">39</span>:<span class="hljs-number">50</span>-<span class="hljs-number">0500</span>,<span class="hljs-built_in">cd</span>/base/<span class="hljs-number">7</span>/<span class="hljs-number">45</span>/<span class="hljs-number">4</span>.png,<span class="hljs-number">206</span>,<span class="hljs-number">2252</span>
.....
</code></pre>
<p>FS 문자를 변경하여 출력하기</p>
<pre><code class="lang-bash"><span class="hljs-comment"># FS 가 ',' 문자일 경우 '-' 로 변경하여 출력</span>
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"111,222,333"</span> | awk <span class="hljs-string">'$1=$1'</span> FS=, OFS=-
<span class="hljs-number">111</span>-<span class="hljs-number">222</span>-<span class="hljs-number">333</span>
---------------------------------------------
<span class="hljs-comment"># FS 가 NUL 문자일 경우 space 로 변경하여 출력</span>
$ cat /proc/`pidof pulseaudio`/cmdline
/usr/bin/pulseaudio--start--log-target=syslog
$ awk <span class="hljs-string">'{$1=$1}1'</span> FS=<span class="hljs-string">'\0'</span> OFS=<span class="hljs-string">' '</span> /proc/`pidof pulseaudio`/cmdline
/usr/bin/pulseaudio --start --log-target=syslog
</code></pre>
<p>FS, OFS 가 기본값인 공백일 경우 특정 필드 삭제하고 재정렬 하기</p>
<pre><code class="lang-bash"><span class="hljs-comment"># $0=$0 는 필드 재설정!, $1=$1 는 레코드 재설정!</span>
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">'111 222 333 444 555 666'</span> |
awk <span class="hljs-string">'{$1=$3=$6=""; $0=$0; print $1,$2,$3; print $0; $1=$1; print $0}'</span>
<span class="hljs-number">222</span> <span class="hljs-number">444</span> <span class="hljs-number">555</span>
<span class="hljs-number">222</span> <span class="hljs-number">444</span> <span class="hljs-number">555</span>
<span class="hljs-number">222</span> <span class="hljs-number">444</span> <span class="hljs-number">555</span>
</code></pre>
<p>FS, OFS 가 공백이 아닐 경우는 다음과 같이 해야 합니다.</p>
<pre><code class="lang-bash">$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"111,222,333,444,555,666"</span> |
awk <span class="hljs-string">'{ $1=$3=$6="\a" # 1,3,6 번 필드 삭제를 위해 마크
gsub(OFS "(\a" OFS ")+", OFS) # gsub 함수를 이용해 마크한 필드 삭제
gsub("^\a" OFS "|" OFS "\a$", "") # gsub 함수는 결과를 $0 에 대입하므로
# 필드가 자동으로 재정렬 됩니다.
printf "(%s) (%s) (%s)\n", $1,$2,$3
print $0
}'</span> FS=, OFS=,
(<span class="hljs-number">222</span>) (<span class="hljs-number">444</span>) (<span class="hljs-number">555</span>) <span class="hljs-comment"># 필드값이 3 개로 재정렬</span>
<span class="hljs-number">222</span>,<span class="hljs-number">444</span>,<span class="hljs-number">555</span> <span class="hljs-comment"># 레코드 값도 재정렬됨</span>
</code></pre>
<h4 id="firefox-실행파일의-cpu-instructions-사용빈도-통계">firefox 실행파일의 cpu instructions 사용빈도 통계</h4>
<pre><code class="lang-bash"><span class="hljs-comment"># objdump -d 출력을 `cat -t` 명령을 이용해 분석해 보면 다음과 같이</span>
<span class="hljs-comment"># ^I (tab) 문자로 구분되어 있는 것을 알 수 있습니다.</span>
$ objdump <span class="hljs-operator">-d</span> /usr/lib/firefox/firefox | cat -t
<span class="hljs-number">3658</span>:^I48 <span class="hljs-number">83</span> ec <span class="hljs-number">08</span> ^Isub <span class="hljs-variable">$0x8</span>,%rsp
<span class="hljs-number">365</span>c:^I48 <span class="hljs-number">8</span>b <span class="hljs-number">05</span> <span class="hljs-number">7</span>d b9 <span class="hljs-number">21</span> <span class="hljs-number">00</span> ^Imov <span class="hljs-number">0</span>x21b97d(%rip),%rax
<span class="hljs-number">3663</span>:^I48 <span class="hljs-number">85</span> c0 ^I<span class="hljs-built_in">test</span> %rax,%rax
$ objdump <span class="hljs-operator">-d</span> /usr/lib/firefox/firefox |
awk -F<span class="hljs-string">'\t'</span> <span class="hljs-string">'$3 { FS=" "; $0=$3; a[$1]++; FS="\t" } \
END { for (i in a) print a[i], i}'</span> | sort -rn
<span class="hljs-number">36719</span> mov
<span class="hljs-number">9057</span> lea
<span class="hljs-number">8570</span> add
<span class="hljs-number">8130</span> cmp
<span class="hljs-number">5730</span> callq
<span class="hljs-number">5350</span> je
<span class="hljs-number">4381</span> <span class="hljs-built_in">test</span>
<span class="hljs-number">4365</span> xor
<span class="hljs-number">3448</span> jne
<span class="hljs-number">3111</span> pop
. . .
. . .
<span class="hljs-comment"># /bin 디렉토리에 있는 모든 실행파일 통계</span>
$ find /bin -type f -exec <span class="hljs-built_in">stat</span> -c <span class="hljs-string">'%i %n'</span> {} + |
awk <span class="hljs-string">'!($1 in a) { a[$1]; print $2 }'</span> |
xargs objdump <span class="hljs-operator">-d</span> <span class="hljs-number">2</span>> /dev/null |
awk -F<span class="hljs-string">'\t'</span> <span class="hljs-string">'$3 { FS=" "; $0=$3; a[$1]++; FS="\t" } \
END { for (i in a) print a[i], i}'</span> | sort -rn
<span class="hljs-number">702355</span> mov
<span class="hljs-number">187434</span> callq
<span class="hljs-number">166436</span> lea
<span class="hljs-number">115877</span> <span class="hljs-built_in">test</span>
<span class="hljs-number">109900</span> je
<span class="hljs-number">106160</span> xor
<span class="hljs-number">84808</span> pop
<span class="hljs-number">83070</span> cmp
<span class="hljs-number">76116</span> push
<span class="hljs-number">75223</span> jne
. . .
. . .
</code></pre>
<h4 id="필드-range-추출과-삭제">필드 range 추출과 삭제</h4>
<p><a href="field_sep.html#range">Field 분리</a> 메뉴에서 작성한 함수의 경우는 레코드($0), 필드 변수값 ($1,$2,$3 ... NF)
들이 변경되지 않고 그대로 남아있었는데요.
이번에는 레코드, 필드 값들을 모두 재설정합니다.</p>
<pre><code class="lang-bash">$ cat file
<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>,<span class="hljs-number">9</span>
a,b,c,d,e,f,g,h,i
.................................................
<span class="hljs-comment"># 레코드, 필드 재설정 하면서 3 ~ 6 range 추출하기</span>
$ awk <span class="hljs-string">'
{ print subflds(3,6) }
function subflds(s,e, i) {
for (i=1; i<=NF; i++)
if (i < s || i > e) $i="\a"
gsub(OFS "(\a" OFS ")+", OFS)
gsub("^\a" OFS "|" OFS "\a$", "")
return $0
}
'</span> FS=, OFS=, file
<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>
c,d,e,f
<span class="hljs-comment"># 레코드, 필드 재설정 하면서 3 ~ 6 range 삭제하기</span>
$ awk <span class="hljs-string">'
{ print delflds(3,6) }
function delflds(s,e) {
while (s <= e) $(s++) = "\a"
gsub(OFS "(\a" OFS ")+", OFS)
gsub("^\a" OFS "|" OFS "\a$", "")
return $0
}
'</span> FS=, OFS=, file
<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>,<span class="hljs-number">9</span>
a,b,g,h,i
</code></pre>
</section>
</div>
</div>
</div>
<a href="./field_sep.html" class="navigation navigation-prev " aria-label="Previous page: Field 분리"><i class="fa fa-angle-left"></i></a>
<a href="./redirection.html" class="navigation navigation-next " aria-label="Next page: Redirection"><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>