-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtips.html
936 lines (538 loc) · 37.8 KB
/
tips.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
<!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>Tips | 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="prev" href="./debugging.html" />
</head>
<body>
<div class="book"
data-level="17"
data-chapter-title="Tips"
data-filepath="tips.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 " 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 active" 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-">
<h1 id="tips">Tips</h1>
<h4 id="shell-변수값-설정-방법">shell 변수값 설정 방법</h4>
<p>awk 는 child process 로 실행되므로 실행 중에 현재 shell 의 변수값을 설정할 수 없습니다.
하지만 shell eval 명령을 활용하면 shell 변수값을 설정할 수 있습니다.</p>
<pre><code class="lang-bash"><span class="hljs-keyword">for</span> tuple <span class="hljs-keyword">in</span> \
INTEL_FAM6_KABYLAKE_DESKTOP,<span class="hljs-number">0</span>x0B,<span class="hljs-number">0</span>x80 \
INTEL_FAM6_SKYLAKE_X,<span class="hljs-number">0</span>x03,<span class="hljs-number">0</span>x0100013e \
INTEL_FAM6_BROADWELL_CORE,<span class="hljs-number">0</span>x04,<span class="hljs-number">0</span>x28 \
INTEL_FAM6_HASWELL_ULT,<span class="hljs-number">0</span>x01,<span class="hljs-number">0</span>x21 \
INTEL_FAM6_IVYBRIDGE_X,<span class="hljs-number">0</span>x04,<span class="hljs-number">0</span>x42a \
INTEL_FAM6_SANDYBRIDGE_X,<span class="hljs-number">0</span>x07,<span class="hljs-number">0</span>x712
<span class="hljs-keyword">do</span> <span class="hljs-comment"># 명령치환을 이용한 model, stepping, ucode 변수값 설정</span>
model=$(<span class="hljs-built_in">echo</span> <span class="hljs-variable">$tuple</span> | cut <span class="hljs-operator">-d</span>, <span class="hljs-operator">-f</span>1)
stepping=$(( $(<span class="hljs-built_in">echo</span> <span class="hljs-variable">$tuple</span> | cut <span class="hljs-operator">-d</span>, <span class="hljs-operator">-f</span>2) ))
ucode=$(<span class="hljs-built_in">echo</span> <span class="hljs-variable">$tuple</span> | cut <span class="hljs-operator">-d</span>, <span class="hljs-operator">-f</span>3)
<span class="hljs-keyword">if</span> [ <span class="hljs-string">"<span class="hljs-variable">$cpu_model</span>"</span> = <span class="hljs-string">"<span class="hljs-variable">$model</span>"</span> ] && [ <span class="hljs-string">"<span class="hljs-variable">$cpu_stepping</span>"</span> = <span class="hljs-string">"<span class="hljs-variable">$stepping</span>"</span> ]
<span class="hljs-keyword">then</span>
. . . .
. . . .
<span class="hljs-keyword">done</span>
-------------------------------------------------------------------------
<span class="hljs-keyword">for</span> tuple <span class="hljs-keyword">in</span> \
. . .
<span class="hljs-keyword">do</span> <span class="hljs-comment"># eval 과 awk 를 활용하여 간단히 작성</span>
<span class="hljs-built_in">eval</span> <span class="hljs-string">"<span class="hljs-variable">$( echo "$tuple" |
awk -F, '{ printf "model=%s; stepping=%s; ucode=%s;",$1,strtonum($2)</span>,<span class="hljs-variable">$3</span> }' )"</span>
<span class="hljs-keyword">if</span> [ <span class="hljs-string">"<span class="hljs-variable">$cpu_model</span>"</span> = <span class="hljs-string">"<span class="hljs-variable">$model</span>"</span> ] && [ <span class="hljs-string">"<span class="hljs-variable">$cpu_stepping</span>"</span> = <span class="hljs-string">"<span class="hljs-variable">$stepping</span>"</span> ]
<span class="hljs-keyword">then</span>
. . . .
. . . .
<span class="hljs-keyword">done</span>
</code></pre>
<p>shell 의 declare, local, export, eval 를 이용하여도 변수값을 설정할 수 있습니다.</p>
<pre><code class="lang-bash">$ <span class="hljs-function"><span class="hljs-title">f1</span></span>() {
<span class="hljs-built_in">local</span> $( awk <span class="hljs-string">'BEGIN{ print "AA=100 BB=*" }'</span> )
<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$AA</span> : <span class="hljs-variable">$BB</span>"</span>
}
$ f1
<span class="hljs-number">100</span> : *
</code></pre>
<p>shell 의 read 명령과 here document 를 이용한 방법<br>명령치환이 일어나야 하므로 END 구분자를 escape 하면 안됩니다.</p>
<pre><code class="lang-bash">$ IFS=, <span class="hljs-built_in">read</span> AA BB CC <<END
$( awk <span class="hljs-string">'BEGIN{ print "100,,*" }'</span> ) <span class="hljs-comment"># 두 번째는 empty 값</span>
END
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$AA</span> : <span class="hljs-variable">$BB</span> : <span class="hljs-variable">$CC</span>"</span>
<span class="hljs-number">100</span> : : *
</code></pre>
<p>bash 의 경우 다음과 같은 방법도 가능합니다.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># here string 을 이용</span>
$ IFS=, <span class="hljs-built_in">read</span> AA BB CC <<< $( awk <span class="hljs-string">'BEGIN{ print "100,,*" }'</span> )
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$AA</span> : <span class="hljs-variable">$BB</span> : <span class="hljs-variable">$CC</span>"</span>
<span class="hljs-number">100</span> : : *
<span class="hljs-comment"># source 명령과 프로세스 치환을 이용</span>
$ <span class="hljs-built_in">source</span> <( awk <span class="hljs-string">'BEGIN{ print "AA=100 BB=*" }'</span> )
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$AA</span> : <span class="hljs-variable">$BB</span>"</span>
<span class="hljs-number">100</span> : *
</code></pre>
<h4 id="here-document-를-이용한-코드-작성방법">here document 를 이용한 코드 작성방법</h4>
<p>shell 에서 제공되는 <code><<WORD</code> 형식을 이용하는 here document 기능은
문서를 작성할 때 single, double quotes 을 escape 할 필요 없이 자유롭게 사용할 수 있는 장점이 있습니다.
그런데 본문 내에서 기본적으로 <code>$</code> 문자로 시작하는 shell 변수가 확장되기 때문에
awk 스크립트를 작성할 때는 다음과 같이 구분자로 사용되는 단어를 escape 하여
shell 변수 확장을 disable 해줘야 합니다.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># heredoc 은 stdin 에 연결되므로 '-f -' 옵션을 사용 ('-' 는 stdin 을 나타냄)</span>
$ awk <span class="hljs-operator">-f</span> - <<\EOF <span class="hljs-comment"># <<\EOF</span>
BEGIN { ... }
EOF
$ awk <span class="hljs-operator">-f</span> - <<<span class="hljs-string">'ABC'</span> <span class="hljs-comment"># <<'ABC'</span>
BEGIN { ... }
ABC
<span class="hljs-comment"># 파이프 로인해 stdin 이 사용 중이므로 -f /dev/fd/3 를 사용</span>
$ <span class="hljs-built_in">command</span> ... | awk <span class="hljs-operator">-f</span> /dev/fd/<span class="hljs-number">3</span> <span class="hljs-number">3</span><<\EOF
BEGIN { ... }
EOF
</code></pre>
<h4 id="awk-내에서-here-document-작성법">awk 내에서 here document 작성법</h4>
<p>awk 에서 shell 명령문을 작성할 때는 기본적으로 스트링을 이용하므로
heredoc 과같이 개행이 필요한 명령문에서는 직접 newline 문자를 사용해야 합니다.</p>
<pre><code class="lang-bash">$ awk <span class="hljs-string">'BEGIN { # 오류
cmd = "cat <<END
111
222
333
END"
system(cmd)
}'</span>
awk: cmd. line:<span class="hljs-number">2</span>: cmd=<span class="hljs-string">"cat <<END
awk: cmd. line:2: ^ unterminated string
awk: cmd. line:2: cmd="</span>cat <<END
awk: cmd. line:<span class="hljs-number">2</span>: ^ syntax error
---------------------------------------------------
$ awk <span class="hljs-string">'BEGIN {
cmd = "cat <<END\n111\n222\n333\nEND" # 직접 newline 문자를 사용
system(cmd)
}'</span>
<span class="hljs-number">111</span>
<span class="hljs-number">222</span>
<span class="hljs-number">333</span>
</code></pre>
<p>만약에 shell 에서처럼 heredoc 을 작성하려면 다음과 같이하면 됩니다.</p>
<pre><code class="lang-bash">$ awk <span class="hljs-string">'BEGIN {
cmd = "cat <<END\n\
${SHELL}\n\
${PWD}\n\
END"
system(cmd)
}'</span>
/bin/bash <span class="hljs-comment"># export 된 값으로 실질적으로는 sh 에의해 실행됩니다.</span>
/home/mug896/tmp
$ awk <span class="hljs-string">'BEGIN {
cmd = "cat <<\\END\n\
${SHELL}\n\
${PWD}\n\
END"
system(cmd)
}'</span>
<span class="hljs-variable">${SHELL}</span>
<span class="hljs-variable">${PWD}</span>
</code></pre>
<h4 id="printf-문에서-천-단위-콤마-넣기">printf 문에서 천 단위 콤마 넣기</h4>
<p>awk 는 스크립트 내에서 필드 변수에 <code>$</code> 문자를 사용하기 때문에 shell 변수로 해석되는 것을
방지하기 위해 single quotes 을 이용해 작성합니다.
그런데 printf 문에서 천 단위 콤마를 넣을 때 사용하는 <code>%'d</code>
포멧 스트링의 경우 <code>'</code> 가 사용되어 문제가 되는데요.
이때는 <code>\047</code> escape 문자를 사용하거나 스크립트 작성에 사용된 single quotes 을 분리한 후에
<code>\'</code> 나 <code>"'"</code> 를 이용해 연결하면 됩니다.</p>
<pre><code class="lang-bash"><span class="hljs-built_in">echo</span> $(<span class="hljs-built_in">echo</span> <span class="hljs-number">2</span>^<span class="hljs-number">64</span> | bc) | LC_ALL=en_US.UTF-<span class="hljs-number">8</span> awk <span class="hljs-string">'{ printf "%'</span>d\n<span class="hljs-string">", <span class="hljs-variable">$1</span> }' # Error
> ^C
$ echo <span class="hljs-variable">$(echo 2^64 | bc)</span> | LC_ALL=en_US.UTF-8 awk '{ printf "</span>%\<span class="hljs-number">047</span>d\n<span class="hljs-string">", <span class="hljs-variable">$1</span> }' # OK
18,446,744,073,709,551,616
$ echo <span class="hljs-variable">$(echo 2^64 | bc)</span> | LC_ALL=ko_KR.UTF-8 awk '{ printf "</span>%<span class="hljs-string">'\'</span><span class="hljs-string">'d\n", $1 }'</span> <span class="hljs-comment"># OK</span>
<span class="hljs-number">18</span>,<span class="hljs-number">446</span>,<span class="hljs-number">744</span>,<span class="hljs-number">073</span>,<span class="hljs-number">709</span>,<span class="hljs-number">551</span>,<span class="hljs-number">616</span>
$ <span class="hljs-built_in">echo</span> $(<span class="hljs-built_in">echo</span> <span class="hljs-number">2</span>^<span class="hljs-number">64</span> | bc) | LC_ALL=ko_KR.UTF-<span class="hljs-number">8</span> awk <span class="hljs-string">'{ printf "%'</span><span class="hljs-string">"'"</span><span class="hljs-string">'d\n", $1 }'</span> <span class="hljs-comment"># OK</span>
<span class="hljs-number">18</span>,<span class="hljs-number">446</span>,<span class="hljs-number">744</span>,<span class="hljs-number">073</span>,<span class="hljs-number">709</span>,<span class="hljs-number">551</span>,<span class="hljs-number">616</span>
.....................................................................
$ awk <span class="hljs-string">'
BEGIN {
if ( "pidof chrome | awk \047{ print NF }\047" | getline count )
print "total chrome pids : " count
}'</span>
total chrome pids : <span class="hljs-number">45</span>
$ awk <span class="hljs-string">'
BEGIN {
if ( "pidof chrome | awk '</span>\<span class="hljs-string">''</span>{ <span class="hljs-built_in">print</span> NF }<span class="hljs-string">'\'</span><span class="hljs-string">'" | getline count )
print "total chrome pids : " count
}'</span>
total chrome pids : <span class="hljs-number">45</span>
</code></pre>
<h4 id="include-할-수-있다">include 할 수 있다</h4>
<p><code>@include</code> 키워드 와 <code>-i</code> 명령 옵션을 이용해 스크립트 파일을 include 해 사용할 수 있습니다.
이 기능은 awk 스크립트 파일을 라이브러리 용도로 활용하기 위한 것으로
한번 include 되어 실행되면 이후에 중복 include 가 되어도 다시 실행되지 않습니다.</p>
<p>파일을 찾을 때는 <code>AWKPATH</code> 환경 변수를 이용하는데 다음과 같이 직접 경로를 지정할 수도 있습니다. </p>
<blockquote>
<p>@include "../test1"<br>@include "/home/abc/awklib/test2" </p>
</blockquote>
<pre><code class="lang-bash">$ cat <span class="hljs-built_in">test</span>1.awk
BEGIN {
<span class="hljs-built_in">print</span> <span class="hljs-string">"This is script test1."</span>
}
@include <span class="hljs-string">"test2"</span>
.................................
$ cat <span class="hljs-built_in">test</span>2.awk
BEGIN {
<span class="hljs-built_in">print</span> <span class="hljs-string">"This is script test2."</span>
}
.................................
$ awk -i <span class="hljs-built_in">test</span>1 <span class="hljs-string">'BEGIN{ print "this is main" }'</span>
This is script <span class="hljs-built_in">test</span>1.
This is script <span class="hljs-built_in">test</span>2.
this is main
</code></pre>
<p><a name="timeout"></a></p>
<h4 id="timeout-설정-방법">timeout 설정 방법</h4>
<p>awk 는 blocking read 에 대해서
millisecond 단위로 timeout 을 설정할 수 있습니다.
이것은 PROCINFO 변수에 값을 설정하는 것으로 이루어지며
개별 operation 별로 설정할 수가 있습니다.
input 값이 도착하지 않아 timeout 이 되면 blocking 이 해제되고 <code>-1</code> 이 반환 됩니다.
이때 ERRNO 변수값도 함께 설정됩니다.</p>
<blockquote>
<p>PROCINFO[ "input_name", "READ_TIMEOUT" ] = timeout in milliseconds</p>
</blockquote>
<pre><code class="lang-bash"><span class="hljs-comment"># timeout 이 되면 getline 에서 -1 이 반환됩니다.</span>
$ awk <span class="hljs-string">'BEGIN {
PROCINFO["/dev/stdin", "READ_TIMEOUT"] = 5000 # 5초
printf "enter number : "
while (getline var < "/dev/stdin" > 0) {
print "entered: " var
printf "enter number : "
}
print "ERRNO: " ERRNO
}'</span>
enter number : <span class="hljs-number">111</span>
entered: <span class="hljs-number">111</span>
enter number : <span class="hljs-number">222</span>
entered: <span class="hljs-number">222</span>
enter number : ERRNO: Connection timed out
</code></pre>
</section>
</div>
</div>
</div>
<a href="./debugging.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: Debugging"><i class="fa fa-angle-left"></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>