-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuiltin_variables.html
More file actions
1245 lines (832 loc) · 59.4 KB
/
builtin_variables.html
File metadata and controls
1245 lines (832 loc) · 59.4 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
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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!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>Built-in Variables | 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="./record_sep.html" />
<link rel="prev" href="./begin_end.html" />
</head>
<body>
<div class="book"
data-level="3"
data-chapter-title="Built-in Variables"
data-filepath="builtin_variables.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 active" 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 " 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="builtin-variables">Builtin Variables</h1>
<p>awk 에서 builin 변수는 스크립트를 작성할 때 자주 활용됩니다.
특히 RECORD 와 FIELD 관련 변수에 대해서는 잘 알고 있어야겠습니다.
이 변수들은 함수를 정의할 때 매개변수로 사용할 수 없습니다.
따라서 함수 내에서 변경해 사용하려면 직접 백업과 복구 과정을 거쳐야 합니다.</p>
<h3 id="record-와-관련된-변수-">[ RECORD 와 관련된 변수 ]</h3>
<hr>
<h3 id="nr">NR</h3>
<p>awk 명령의 인수로 여러 개의 입력 파일이 사용될 경우 계속해서 누적되어 증가하는
total number of input records 를 말합니다.
메인 입력 스트림에서 읽어들이는 경우에만 적용됩니다.</p>
<h3 id="fnr">FNR</h3>
<p>현재 입력 스트림으로 사용 중인 파일의 record number 를 말합니다.
NR 와 같이 계속 누적되는 것이 아니고 파일별 record number 이므로
입력 스트림이 다음 파일로 변경될 때마다 0 부터 다시 시작합니다.
마찬가지로 메인 입력 스트림에서 읽어들이는 경우에만 적용됩니다.</p>
<pre><code class="lang-sh">$ cat file1 $ cat file2
aaaaa ddddd
bbbbb eeeee
ccccc fffff
$ awk <span class="hljs-string">'{ printf "NR: %d, FNR: %d\n", NR, FNR}'</span> file1 file2
NR: <span class="hljs-number">1</span>, FNR: <span class="hljs-number">1</span>
NR: <span class="hljs-number">2</span>, FNR: <span class="hljs-number">2</span>
NR: <span class="hljs-number">3</span>, FNR: <span class="hljs-number">3</span>
NR: <span class="hljs-number">4</span>, FNR: <span class="hljs-number">1</span> <---- FNR 은 <span class="hljs-number">1</span> 부터 다시시작
NR: <span class="hljs-number">5</span>, FNR: <span class="hljs-number">2</span>
NR: <span class="hljs-number">6</span>, FNR: <span class="hljs-number">3</span>
</code></pre>
<h3 id="rs">RS</h3>
<p>input Record Separator 입니다.
awk 는 데이터를 읽어 들일 때 항상 레코드 단위로 읽어 들입니다.
따라서 명령 사이클에 의해 메인 입력 스트림으로부터 읽어 들이던
redirection 을 이용해 직접 읽어들이던 상관없이 항상 RS 가 적용됩니다.
기본값은 newline 이고 문자가 두 개 이상이면 regex 로 해석됩니다.
실행 중 변경하게 되면 기본적으로 다음 레코드를 읽어들일 때부터 적용됩니다.</p>
<h3 id="ors">ORS</h3>
<p>Output Record Separator 입니다.
<code>print</code> 문을 이용해 레코드를 출력할 때 자동으로 끝에 붙는 값입니다 ( '1' 을 사용하여 출력하는 경우도 포함).
기본값은 newline 이고 output 에 사용되므로 regex 으로 해석되지 않습니다.
변경 즉시 이어지는 print 문부터 바로 적용됩니다.
레코드 끝에 자동으로 붙는 값이라 다음과 같은 특징이 있습니다.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># '111.222' 를 '111X222' 로 만들려고 하지만 자동으로 끝에 X 가 붙는다.</span>
$ <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"111.222"</span> | awk <span class="hljs-string">'1'</span> RS=. ORS=X
<span class="hljs-number">111</span>X222X
<span class="hljs-comment"># newline 이 레코드 값이 되어 실제 레코드 개수는 3 개가 된다.</span>
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"111.222."</span> | awk <span class="hljs-string">'1'</span> RS=. ORS=X
<span class="hljs-number">111</span>X222X
X
$ <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"111.222."</span> | awk <span class="hljs-string">'1'</span> RS=. ORS=X
<span class="hljs-number">111</span>X222X
.........................................
$ seq <span class="hljs-number">30</span> | awk <span class="hljs-string">'ORS = NR % 5 ? FS : RS'</span>
<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> <span class="hljs-number">10</span>
<span class="hljs-number">11</span> <span class="hljs-number">12</span> <span class="hljs-number">13</span> <span class="hljs-number">14</span> <span class="hljs-number">15</span>
<span class="hljs-number">16</span> <span class="hljs-number">17</span> <span class="hljs-number">18</span> <span class="hljs-number">19</span> <span class="hljs-number">20</span>
<span class="hljs-number">21</span> <span class="hljs-number">22</span> <span class="hljs-number">23</span> <span class="hljs-number">24</span> <span class="hljs-number">25</span>
<span class="hljs-number">26</span> <span class="hljs-number">27</span> <span class="hljs-number">28</span> <span class="hljs-number">29</span> <span class="hljs-number">30</span>
</code></pre>
<h3 id="rt">RT</h3>
<p>Record Terminator 입니다. 기본적으로 RS 값과 같지만
RS 값으로 regex 이 사용될 경우 실제 매칭 된 값이 RT 에 설정되게 됩니다.</p>
<p>예제: 숫자를 모두 -1 하기 )</p>
<pre><code class="lang-bash">$ cat file
<span class="hljs-number">2</span>,<span class="hljs-number">6</span>*<span class="hljs-number">3</span>,<span class="hljs-number">4</span>-<span class="hljs-number">7</span>
<span class="hljs-number">1</span>,<span class="hljs-number">2</span>*<span class="hljs-number">7</span>,<span class="hljs-number">6</span>-<span class="hljs-number">2</span>
<span class="hljs-number">5</span>,<span class="hljs-number">4</span>**<span class="hljs-number">3</span>,<span class="hljs-number">9</span>-<span class="hljs-number">8</span>
$ awk <span class="hljs-string">'BEGIN { RS="[^0-9]+"; ORS="" }
{ if (NF) print ($0-1) RT }'</span> file
<span class="hljs-number">1</span>,<span class="hljs-number">5</span>*<span class="hljs-number">2</span>,<span class="hljs-number">3</span>-<span class="hljs-number">6</span>
<span class="hljs-number">0</span>,<span class="hljs-number">1</span>*<span class="hljs-number">6</span>,<span class="hljs-number">5</span>-<span class="hljs-number">1</span>
<span class="hljs-number">4</span>,<span class="hljs-number">3</span>**<span class="hljs-number">2</span>,<span class="hljs-number">8</span>-<span class="hljs-number">7</span>
</code></pre>
<h3 id="field-와-관련된-변수-">[ FIELD 와 관련된 변수 ]</h3>
<hr>
<h3 id="nf">NF</h3>
<p>읽어들인 레코드의 Number of Fields 값입니다.
<code>getline var</code> 와 같이 레코드를 읽어들여 변수에 저장하는 경우를 제외하고
레코드를 읽어들이는 모든 경우에 <code>$1</code>, <code>$2</code>, <code>$3</code> ... 필드 변수와 함께 설정됩니다.</p>
<p>실행 중에 새로운 필드값을 추가할 경우 NF 값도 변경됩니다.</p>
<pre><code class="lang-bash">$ <span class="hljs-built_in">echo</span> <span class="hljs-number">11</span> <span class="hljs-number">22</span> <span class="hljs-number">33</span> | awk <span class="hljs-string">'{ print NF; print; $5=55; print NF; print }'</span>
<span class="hljs-number">3</span>
<span class="hljs-number">11</span> <span class="hljs-number">22</span> <span class="hljs-number">33</span>
<span class="hljs-number">5</span>
<span class="hljs-number">11</span> <span class="hljs-number">22</span> <span class="hljs-number">33</span> <span class="hljs-number">55</span>
----------------------------------------------------
$ awk <span class="hljs-string">'{ for (i=5; i<=NF; i++) sum += (2 ^ (i-5)) * 4096 * $i }
END { print sum / 1024 }'</span> /proc/buddyinfo
<span class="hljs-number">4171104</span>
$ free
total used free shared buff/cache available
Mem: <span class="hljs-number">16301392</span> <span class="hljs-number">4491600</span> <span class="hljs-number">4171356</span> <span class="hljs-number">748976</span> <span class="hljs-number">7638436</span> <span class="hljs-number">10723484</span>
Swap: <span class="hljs-number">2097148</span> <span class="hljs-number">513984</span> <span class="hljs-number">1583164</span>
</code></pre>
<h3 id="fs">FS</h3>
<p>input Field Separator 입니다.
기본값은 space 이지만 awk 에서 특별히 처리하므로 실질적으로 <code>FS="[ \t\n]+"</code> 와 같습니다
(<a href="field_sep.html#FS">해당 페이지 참조</a>).
문자가 두 개 이상이면 regex 로 해석됩니다.
실행 중 변경하게 되면 기본적으로 다음 레코드를 읽어들일 때부터 적용됩니다.</p>
<h3 id="ofs">OFS</h3>
<p>Output Field Separator 입니다.
기본값은 space 이고 output 에 사용되므로 regex 으로 해석되지 않습니다.
변경 즉시 이어지는 print 문부터 바로 적용됩니다.</p>
<h3 id="fieldwidths">FIELDWIDTHS</h3>
<p><a href="field_sep.html#FIELDWIDTHS">해당 페이지 참조</a></p>
<h3 id="fpat">FPAT</h3>
<p><a href="field_sep.html#FPAT">해당 페이지 참조</a></p>
<h3 id="포멧과-관련된-변수-">[ 포멧과 관련된 변수 ]</h3>
<hr>
<h3 id="convfmt">CONVFMT</h3>
<p><a href="data_types.html#conversion">해당 페이지 참조</a></p>
<h3 id="ofmt">OFMT</h3>
<p><a href="data_types.html#conversion">해당 페이지 참조</a></p>
<p><a name="PREC"></a></p>
<h3 id="prec">PREC</h3>
<p>global working precision of arbitrary precision floating-point numbers 을 설정합니다.
기본값은 53 bits 인데 더 높은 수준의 정밀도가 필요할 경우
<code>-M</code> 옵션과 함께 PREC 변수를 설정해 사용할 수 있습니다.</p>
<blockquote>
<p>PREC 변수는 <code>-v</code> 옵션으로만 설정할 수 있습니다.</p>
</blockquote>
<pre><code class="lang-bash">$ awk <span class="hljs-string">'BEGIN { printf("%0.25f\n", 0.1 ) }'</span>
<span class="hljs-number">0.1000000000000000055511151</span>
$ awk -M -vPREC=<span class="hljs-number">80</span> <span class="hljs-string">'BEGIN { printf("%0.25f\n", 0.1 ) }'</span>
<span class="hljs-number">0.1000000000000000000000000</span>
...............................................................
$ awk <span class="hljs-string">'BEGIN { OFMT="%.20f"; print 0.123456789 }'</span>
<span class="hljs-number">0.12345678899999999734</span>
$ awk -M -vPREC=<span class="hljs-number">65</span> <span class="hljs-string">'BEGIN { OFMT="%.20f"; print 0.123456789 }'</span>
<span class="hljs-number">0.12345678900000000000</span>
...............................................................
$ awk <span class="hljs-string">'BEGIN { OFMT="%.20f"; print 2.15 - 1.1 }'</span>
<span class="hljs-number">1.04999999999999982236</span>
$ awk -M -vPREC=<span class="hljs-number">70</span> <span class="hljs-string">'BEGIN { OFMT="%.20f"; print 2.15 - 1.1 }'</span>
<span class="hljs-number">1.05000000000000000000</span>
...............................................................
$ awk <span class="hljs-string">'BEGIN { printf("%0.25f\n", 0.7 * 1.05 ) }'</span>
<span class="hljs-number">0.7349999999999999866773237</span>
$ awk -M -vPREC=<span class="hljs-number">70</span> <span class="hljs-string">'BEGIN { printf("%0.25f\n", 0.7 * 1.05 ) }'</span>
<span class="hljs-number">0.7350000000000000000003049</span>
$ awk -M -vPREC=<span class="hljs-number">80</span> <span class="hljs-string">'BEGIN { printf("%0.25f\n", 0.7 * 1.05 ) }'</span>
<span class="hljs-number">0.7349999999999999999999997</span>
$ awk -M -vPREC=<span class="hljs-number">90</span> <span class="hljs-string">'BEGIN { printf("%0.25f\n", 0.7 * 1.05 ) }'</span>
<span class="hljs-number">0.7350000000000000000000000</span>
</code></pre>
<p><code>bc</code> 명령과 <code>e^12345</code> 계산 속도 비교</p>
<pre><code class="lang-bash">$ time BC_LINE_LENGTH=<span class="hljs-number">0</span> bc <span class="hljs-operator">-l</span> <<< <span class="hljs-string">'e(12345)'</span>
..........<span class="hljs-number">833186320363613720444367.94022392548520604058</span>
real <span class="hljs-number">0</span>m53.<span class="hljs-number">836</span>s
user <span class="hljs-number">0</span>m53.<span class="hljs-number">832</span>s
sys <span class="hljs-number">0</span>m0.<span class="hljs-number">004</span>s
$ time awk -M -vPREC=<span class="hljs-number">20000</span> <span class="hljs-string">'BEGIN { printf( "%0.20f\n", exp(12345) ) }'</span>
..........<span class="hljs-number">833186320363613720444367.94022392548520604058</span>
real <span class="hljs-number">0</span>m0.<span class="hljs-number">018</span>s
user <span class="hljs-number">0</span>m0.<span class="hljs-number">013</span>s
sys <span class="hljs-number">0</span>m0.<span class="hljs-number">005</span>s
</code></pre>
<p><a name="ROUNDMODE"></a></p>
<h3 id="roundmode">ROUNDMODE</h3>
<p>디폴트 값은 "N" ( roundTiesToEven ) 입니다.</p>
<blockquote>
<p>ROUNDMODE 를 적용할 땐 <code>-M</code> 옵션을 사용해야 하고 <code>-v</code> 옵션으로만 설정할 수 있습니다.</p>
</blockquote>
<table>
<thead>
<tr>
<th>Rounding Mode</th>
<th>IEEE Name</th>
<th style="text-align:center">ROUNDMODE</th>
</tr>
</thead>
<tbody>
<tr>
<td>Round to nearest, ties to even</td>
<td>roundTiesToEven</td>
<td style="text-align:center">"N" or "n"</td>
</tr>
<tr>
<td>Round toward plus Infinity</td>
<td>roundTowardPositive</td>
<td style="text-align:center">"U" or "u"</td>
</tr>
<tr>
<td>Round toward negative Infinity</td>
<td>roundTowardNegative</td>
<td style="text-align:center">"D" or "d"</td>
</tr>
<tr>
<td>Round toward zero</td>
<td>roundTowardZero</td>
<td style="text-align:center">"Z" or "z"</td>
</tr>
<tr>
<td>Round to nearest, ties away from zero</td>
<td>roundTiesToAway</td>
<td style="text-align:center">"A" or "a"</td>
</tr>
</tbody>
</table>
<pre><code class="lang-bash"><span class="hljs-comment"># 디폴트 값은 "N" ( roundTiesToEven )</span>
$ awk <span class="hljs-string">'
BEGIN {
x = -4.5
for (i = 1; i < 10; i++) {
x += 1.0
printf("%4.1f => %2.0f\n", x, x)
}
}'</span>
-<span class="hljs-number">3.5</span> => -<span class="hljs-number">4</span>
-<span class="hljs-number">2.5</span> => -<span class="hljs-number">2</span>
-<span class="hljs-number">1.5</span> => -<span class="hljs-number">2</span>
-<span class="hljs-number">0.5</span> => -<span class="hljs-number">0</span>
<span class="hljs-number">0.5</span> => <span class="hljs-number">0</span>
<span class="hljs-number">1.5</span> => <span class="hljs-number">2</span>
<span class="hljs-number">2.5</span> => <span class="hljs-number">2</span>
<span class="hljs-number">3.5</span> => <span class="hljs-number">4</span>
<span class="hljs-number">4.5</span> => <span class="hljs-number">4</span>
-----------------------------------------
<span class="hljs-comment"># "U" ( roundTowardPositive )</span>
$ awk -M -v ROUNDMODE=U <span class="hljs-string">'
BEGIN {
x = -4.5
for (i = 1; i < 10; i++) {
x += 1.0
printf("%4.1f => %2.0f\n", x, x)
}
}'</span>
-<span class="hljs-number">3.5</span> => -<span class="hljs-number">3</span>
-<span class="hljs-number">2.5</span> => -<span class="hljs-number">2</span>
-<span class="hljs-number">1.5</span> => -<span class="hljs-number">1</span>
-<span class="hljs-number">0.5</span> => -<span class="hljs-number">0</span>
<span class="hljs-number">0.5</span> => <span class="hljs-number">1</span>
<span class="hljs-number">1.5</span> => <span class="hljs-number">2</span>
<span class="hljs-number">2.5</span> => <span class="hljs-number">3</span>
<span class="hljs-number">3.5</span> => <span class="hljs-number">4</span>
<span class="hljs-number">4.5</span> => <span class="hljs-number">5</span>
-----------------------------------------
<span class="hljs-comment"># "D" ( roundTowardNegative )</span>
$ awk -M -v ROUNDMODE=D <span class="hljs-string">'
BEGIN {
x = -4.5
for (i = 1; i < 10; i++) {
x += 1.0
printf("%4.1f => %2.0f\n", x, x)
}
}'</span>
-<span class="hljs-number">3.5</span> => -<span class="hljs-number">4</span>
-<span class="hljs-number">2.5</span> => -<span class="hljs-number">3</span>
-<span class="hljs-number">1.5</span> => -<span class="hljs-number">2</span>
-<span class="hljs-number">0.5</span> => -<span class="hljs-number">1</span>
<span class="hljs-number">0.5</span> => <span class="hljs-number">0</span>
<span class="hljs-number">1.5</span> => <span class="hljs-number">1</span>
<span class="hljs-number">2.5</span> => <span class="hljs-number">2</span>
<span class="hljs-number">3.5</span> => <span class="hljs-number">3</span>
<span class="hljs-number">4.5</span> => <span class="hljs-number">4</span>
----------------------------------------
<span class="hljs-comment"># "Z" ( roundTowardZero )</span>
$ awk -M -v ROUNDMODE=Z <span class="hljs-string">'
BEGIN {
x = -4.5
for (i = 1; i < 10; i++) {
x += 1.0
printf("%4.1f => %2.0f\n", x, x)
}
}'</span>
-<span class="hljs-number">3.5</span> => -<span class="hljs-number">3</span> <span class="hljs-comment"># 결과적으로 정수 부분만 출력됩니다.</span>
-<span class="hljs-number">2.5</span> => -<span class="hljs-number">2</span>
-<span class="hljs-number">1.5</span> => -<span class="hljs-number">1</span>
-<span class="hljs-number">0.5</span> => -<span class="hljs-number">0</span>
<span class="hljs-number">0.5</span> => <span class="hljs-number">0</span>
<span class="hljs-number">1.5</span> => <span class="hljs-number">1</span>
<span class="hljs-number">2.5</span> => <span class="hljs-number">2</span>
<span class="hljs-number">3.5</span> => <span class="hljs-number">3</span>
<span class="hljs-number">4.5</span> => <span class="hljs-number">4</span>
-----------------------------------------
<span class="hljs-comment"># "A" ( roundTiesToAway )</span>
$ awk -M -v ROUNDMODE=A <span class="hljs-string">'
BEGIN {
x = -4.5
for (i = 1; i < 10; i++) {
x += 1.0
printf("%4.1f => %2.0f\n", x, x)
}
}'</span>
-<span class="hljs-number">3.5</span> => -<span class="hljs-number">4</span>
-<span class="hljs-number">2.5</span> => -<span class="hljs-number">3</span>
-<span class="hljs-number">1.5</span> => -<span class="hljs-number">2</span>
-<span class="hljs-number">0.5</span> => -<span class="hljs-number">1</span>
<span class="hljs-number">0.5</span> => <span class="hljs-number">1</span>
<span class="hljs-number">1.5</span> => <span class="hljs-number">2</span>
<span class="hljs-number">2.5</span> => <span class="hljs-number">3</span>
<span class="hljs-number">3.5</span> => <span class="hljs-number">4</span>
<span class="hljs-number">4.5</span> => <span class="hljs-number">5</span>
</code></pre>
<h3 id="매칭과-관련된-변수-">[ 매칭과 관련된 변수 ]</h3>
<hr>
<h3 id="ignorecase">IGNORECASE</h3>
<p>변수에 어떤 값이든 설정이 되어 non-null 값을 가지게 되면 (true 가 되면)
모든 스트링 비교와 regex 매칭에 대,소 문자를 구분하지 않습니다.
여기에는 <code>~</code> 연산자를 이용한 regex 매칭,