-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfield_sep.html
1150 lines (732 loc) · 59.1 KB
/
field_sep.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
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>Field 분리 | 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="./zero_one.html" />
<link rel="prev" href="./record_sep.html" />
</head>
<body>
<div class="book"
data-level="5"
data-chapter-title="Field 분리"
data-filepath="field_sep.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 active" 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="field-분리">Field 분리</h1>
<p>Field 분리에는 FS 를 이용하는 기본 방법과 FPAT, FIELDWIDTHS 를 이용하는 방법 세 가지가 있습니다.<br>FS 에 설정되는 값은 문자가 두 개 이상이면 regex 로 해석됩니다.</p>
<p><a name="FS"></a></p>
<h3 id="디폴트-값은-fs---">디폴트 값은 <code>FS = " "</code></h3>
<p>RS( Record Seperator ) 에서는 <code>RS=""</code> 가 특별히 처리된다면
FS( Field Seperator ) 에서는 <code>FS=" "</code> 가 특별히 처리됩니다.
<code>FS=" "</code> 의 기능은 실제적으로 <code>FS="[ \t\n]+"</code> 와 같습니다.
다시 말해서 space 뿐만 아니라 tab, newline 도 같은 분리자로 취급한다는 뜻입니다.</p>
<p><code>FS="[ \t\n]+"</code> 의 경우 한가지 단점이 있는데 첫 번째 필드 시작 전에 공백이 있으면
null 값 필드가 생긴다는 것입니다.
하지만 <code>FS=" "</code> 의 경우는 먼저 공백을 삭제하기 때문에 null 값 필드가 생기지 않습니다.
이것은 <code>RS=""</code> 와 비슷한 기능입니다.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># 디폴트 FS 값은 space</span>
$ awk <span class="hljs-string">'BEGIN{ printf FS }'</span> | od <span class="hljs-operator">-a</span>
<span class="hljs-number">0000000</span> sp
<span class="hljs-number">0000001</span>
<span class="hljs-comment"># 실제적으로 FS=" " 는 FS='[ \t\n]+' 와 같은 기능을 합니다.</span>
<span class="hljs-comment"># 추가로 FS=" " 는 첫 번째 필드 시작 전에 있는 공백을 먼저 삭제 처리합니다.</span>
$ <span class="hljs-built_in">printf</span> %b <span class="hljs-string">' 11 \t\t22 33\n\t\n \n44'</span> |
awk <span class="hljs-string">'{ printf "(%s) (%s) (%s) (%s) NF: %d\n",$1,$2,$3,$4,NF }'</span> RS=<span class="hljs-string">'^$'</span> FS=<span class="hljs-string">' '</span>
(<span class="hljs-number">11</span>) (<span class="hljs-number">22</span>) (<span class="hljs-number">33</span>) (<span class="hljs-number">44</span>) NF: <span class="hljs-number">4</span>
<span class="hljs-comment"># FS=" " 와 달리 FS='[ \t\n]+' 는 첫 번째 필드 시작 전에 공백이 있을 경우 null 값 필드가 생깁니다.</span>
<span class="hljs-built_in">printf</span> %b <span class="hljs-string">' 11 \t\t22 33\n\t\n \n44'</span> |
awk <span class="hljs-string">'{ printf "(%s) (%s) (%s) (%s) (%s) NF: %d\n",$1,$2,$3,$4,$5,NF }'</span> RS=<span class="hljs-string">'^$'</span> FS=<span class="hljs-string">'[ \t\n]+'</span>
() (<span class="hljs-number">11</span>) (<span class="hljs-number">22</span>) (<span class="hljs-number">33</span>) (<span class="hljs-number">44</span>) NF: <span class="hljs-number">5</span>
</code></pre>
<h3 id="cut-명령과-awk-가-다른점">cut 명령과 awk 가 다른점</h3>
<p>쉘 스크립트에서 필드값을 분리할 때 cut 명령을 사용하는데요.<br>cut 명령과 awk 는 다음과 같은 차이가 있습니다.</p>
<p>awk 의 경우 공백 개수에 상관없이 정확히 필드를 분리합니다.</p>
<pre><code class="lang-bash">$ cat file
AAA BBB CCC DDD
XXX YYY ZZZ KKK
<span class="hljs-number">111</span> <span class="hljs-number">222</span> <span class="hljs-number">333</span> <span class="hljs-number">444</span>
................................
$ awk <span class="hljs-string">'{print $1,$2,$3,$4}'</span> file
AAA BBB CCC DDD
XXX YYY ZZZ KKK
<span class="hljs-number">111</span> <span class="hljs-number">222</span> <span class="hljs-number">333</span> <span class="hljs-number">444</span>
$ awk <span class="hljs-string">'{print $1,$3}'</span> file
AAA CCC
XXX ZZZ
<span class="hljs-number">111</span> <span class="hljs-number">333</span>
</code></pre>
<p>cut 명령은 실제 space 개수에 따라서만 필드가 분리됩니다.</p>
<pre><code class="lang-bash">$ cat file
AAA BBB CCC DDD
XXX YYY ZZZ KKK
<span class="hljs-number">111</span> <span class="hljs-number">222</span> <span class="hljs-number">333</span> <span class="hljs-number">444</span>
......................
$ cut <span class="hljs-operator">-d</span><span class="hljs-string">' '</span> <span class="hljs-operator">-f</span>1 file
AAA
$ cut <span class="hljs-operator">-d</span><span class="hljs-string">' '</span> <span class="hljs-operator">-f</span>2 file
BBB
XXX
$ cut <span class="hljs-operator">-d</span><span class="hljs-string">' '</span> <span class="hljs-operator">-f</span>3 file
CCC
YYY
<span class="hljs-number">111</span>
</code></pre>
<h4 id="cut-명령과-같이-fs-값을-실제-space-로-설정하려면">cut 명령과 같이 FS 값을 실제 space 로 설정하려면</h4>
<p>다음과 같이 regex 표현식 <code>[ ]</code> or <code>( )</code> 을 이용해 설정하면 됩니다.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># space 수에 관계없이 필드 개수가 4 개로 나온다.</span>
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">'11 22 33 44'</span> | awk <span class="hljs-string">'{ print NF }'</span> FS=<span class="hljs-string">' '</span>
<span class="hljs-number">4</span>
<span class="hljs-comment"># FS='[ ]' 로 설정하면 모든 space 에서 필드가 분리되어 필드 개수가 11 개로 나온다</span>
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">'11 22 33 44'</span> | awk <span class="hljs-string">'{ print NF }'</span> FS=<span class="hljs-string">'[ ]'</span>
<span class="hljs-number">11</span>
</code></pre>
<h3 id="sort-명령과-awk-가-다른점">sort 명령과 awk 가 다른점</h3>
<p>다음은 ps 명령의 출력에서 4 번째 TIME 컬럼의 값을 <code>sort -u</code> 명령을 이용해 uniq 한 값을 구하려고 한 것인데요.
결과를 보면 uniq 한값이 아니라 0:00, 0:01 값이 중복되어 나타나고 있습니다.
이렇게 되는 이유는 앞에 흰색으로 표시한 공백이 컬럼 값에 포함되어 다른 값으로 인식되기 때문입니다.</p>
<p><img src="images/sort_example.png" alt=""></p>
<p>따라서 정상적으로 결과가 출력되기 위해서는 sort 명령의 <code>-b</code>( ignore-leading-blanks )
옵션을 함께 사용해야 합니다.
하지만 이것도 separator 값이 공백이 아닐 경우는 사용할 수 없습니다.</p>
<p>awk 의 경우는 FS 값만 변경해주면 되고 정확히 uniq 한 값을 출력합니다.</p>
<pre><code class="lang-bash">$ ps axc --no-headers | awk <span class="hljs-string">'{a[$4]=$0} END {for (i in a) print a[i]}'</span>
<span class="hljs-number">5710</span> ? Sl <span class="hljs-number">11</span>:<span class="hljs-number">35</span> chrome
<span class="hljs-number">1617</span> ? Sl <span class="hljs-number">5</span>:<span class="hljs-number">40</span> konsole
<span class="hljs-number">5804</span> ? Sl <span class="hljs-number">1</span>:<span class="hljs-number">59</span> chrome
<span class="hljs-number">5556</span> ? Sl <span class="hljs-number">59</span>:<span class="hljs-number">40</span> chrome
<span class="hljs-number">24408</span> pts/<span class="hljs-number">10</span> Ss <span class="hljs-number">0</span>:<span class="hljs-number">10</span> bash
<span class="hljs-number">1181</span> ? Ssl <span class="hljs-number">0</span>:<span class="hljs-number">12</span> polkitd
<span class="hljs-number">1378</span> ? Ss <span class="hljs-number">0</span>:<span class="hljs-number">13</span> dbus-daemon
. . . . .
. . . . .
</code></pre>
<h3 id="rs---이고-fs-가-single-character-일-경우"><code>RS = ""</code> 이고 FS 가 single character 일 경우</h3>
<p><code>RS = ""</code> 이면 레코드가 공백 라인에 의해 분리가 되는데요.
이때 FS 값이 single character 이면 자동으로 newline 에서도 필드가 분리됩니다.</p>
<pre><code class="lang-bash">$ cat file
Amelia@<span class="hljs-number">555</span>-<span class="hljs-number">5553</span>
Anthony@<span class="hljs-number">555</span>-<span class="hljs-number">3412</span>
Becky@<span class="hljs-number">555</span>-<span class="hljs-number">7685</span>
.................
<span class="hljs-comment"># FS='@' 이지만 newline 에서도 분리가 되어 NF 값이 6 개로 나온다.</span>
$ awk <span class="hljs-string">'{print NF}'</span> FS=<span class="hljs-string">'@'</span> RS=<span class="hljs-string">''</span> file
<span class="hljs-number">6</span>
<span class="hljs-comment"># 실질적으로 FS='[@\n]' 와 같다.</span>
$ awk <span class="hljs-string">'{print NF}'</span> FS=<span class="hljs-string">'[@\n]'</span> RS=<span class="hljs-string">''</span> file
<span class="hljs-number">6</span>
$ awk <span class="hljs-string">'{printf "(%s) (%s) (%s) (%s)\n",$1,$2,$3,$4}'</span> FS=<span class="hljs-string">'@'</span> RS=<span class="hljs-string">''</span> file
(Amelia) (<span class="hljs-number">555</span>-<span class="hljs-number">5553</span>) (Anthony) (<span class="hljs-number">555</span>-<span class="hljs-number">3412</span>)
$ awk <span class="hljs-string">'{printf "(%s) (%s) (%s) (%s)\n",$1,$2,$3,$4}'</span> FS=<span class="hljs-string">'[@\n]'</span> RS=<span class="hljs-string">''</span> file
(Amelia) (<span class="hljs-number">555</span>-<span class="hljs-number">5553</span>) (Anthony) (<span class="hljs-number">555</span>-<span class="hljs-number">3412</span>)
---------------------------------------------------------------------------
<span class="hljs-comment"># FS='[@]' 로 설정하면 정확히 '@' 문자에서만 분리가 된다.</span>
$ awk <span class="hljs-string">'{print NF}'</span> FS=<span class="hljs-string">'[@]'</span> RS=<span class="hljs-string">''</span> file
<span class="hljs-number">4</span>
$ awk <span class="hljs-string">'{printf "(%s) (%s) (%s) (%s)\n",$1,$2,$3,$4}'</span> FS=<span class="hljs-string">'[@]'</span> RS=<span class="hljs-string">''</span> file
(Amelia) (<span class="hljs-number">555</span>-<span class="hljs-number">5553</span>
Anthony) (<span class="hljs-number">555</span>-<span class="hljs-number">3412</span>
Becky) (<span class="hljs-number">555</span>-<span class="hljs-number">7685</span>)
</code></pre>
<h3 id="fs--">FS = ""</h3>
<p>FS 값을 <code>""</code> 로 설정하면 문자 단위로 필드가 분리됩니다.</p>
<pre><code class="lang-bash">$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"foo bar"</span> | awk <span class="hljs-string">'{for (i=1; i<=NF; i++) print "$" i " : " $i}'</span> FS=<span class="hljs-string">''</span>
<span class="hljs-variable">$1</span> : f
<span class="hljs-variable">$2</span> : o
<span class="hljs-variable">$3</span> : o
<span class="hljs-variable">$4</span> :
<span class="hljs-variable">$5</span> : b
<span class="hljs-variable">$6</span> : a
<span class="hljs-variable">$7</span> : r
</code></pre>
<h3 id="fs-값에-사용되는---regex-문자">FS 값에 사용되는 <code>^</code>, <code>$</code> regex 문자</h3>
<p>FS 에 사용되는 <code>^</code>, <code>$</code> 문자는 레코드의 시작과 끝을 나타냅니다.
따라서 만약에 FS 에 <code>^</code> 를 사용한다면 레코드의 시작 부분에만 매칭이 되고
<code>$</code> 를 사용한다면 레코드의 마지막 부분에만 매칭이 되게 됩니다.</p>
<h3 id="fs--">FS = "^$"</h3>
<p>이것은 레코드 값이 존재하는 한 매칭이 되지 않으므로
레코드 분리가 일어나지 않아 NF 가 1 이 되고 <code>$0 == $1</code> 가 됩니다.</p>
<p><a name="FIELDWIDTHS"></a></p>
<h4 id=""><img src="images/buttons/fieldwidths.png" alt=""></h4>
<p>다음 예는 레코드의 필드값이 경우에 따라서 공백으로 비어 있습니다.
이와 같이 구성되어 있을 경우 각 컬럼에 맞는 필드값을 정확히 추출하기 어려운데요.
이럴 때는 FS 를 이용하는 방법 대신에 FIELDWIDTHS 를 사용할 수 있습니다.
이 방법은 FIELDWIDTHS 에 직접 입력한 컬럼 사이즈에 따라서 필드를 분리합니다.</p>
<p><img src="images/data_example3.png" alt=""></p>
<pre><code class="lang-bash">$ awk -v FIELDWIDTHS=<span class="hljs-string">"6 8 7 6 3 6 4"</span> <span class="hljs-string">'{
printf "(%s) (%s) (%s) (%s) (%s) (%s) (%s)\n",
$1, $2, $3, $4, $5, $6, $7
}'</span> file
(<span class="hljs-number">4051</span> ) (S00001 ) (<span class="hljs-number">31228</span> ) (<span class="hljs-number">3286</span> ) (<span class="hljs-number">0</span> ) ( ) (<span class="hljs-number">23.6</span>)
(<span class="hljs-number">4050</span> ) (S00201 ) ( ) (<span class="hljs-number">4251</span> ) (<span class="hljs-number">0</span> ) (<span class="hljs-number">12.1</span> ) (<span class="hljs-number">23.6</span>)
(<span class="hljs-number">4049</span> ) (S00301 ) (<span class="hljs-number">31221</span> ) (<span class="hljs-number">3021</span> ) (<span class="hljs-number">1</span> ) (<span class="hljs-number">14.4</span> ) ()
(<span class="hljs-number">4048</span> ) (S00213 ) (<span class="hljs-number">46578</span> ) ( ) (<span class="hljs-number">0</span> ) (<span class="hljs-number">23.2</span> ) (<span class="hljs-number">43.9</span>)
(<span class="hljs-number">4047</span> ) (S00113 ) ( ) (<span class="hljs-number">4250</span> ) (<span class="hljs-number">0</span> ) ( ) (<span class="hljs-number">43.9</span>)
(<span class="hljs-number">4046</span> ) (S00122 ) (<span class="hljs-number">31225</span> ) (<span class="hljs-number">4249</span> ) (<span class="hljs-number">0</span> ) (<span class="hljs-number">45.5</span> ) (<span class="hljs-number">21.6</span>)
</code></pre>
<p>그런데 위의 출력 결과를 보면 필드값에 공백이 포함되죠.
gawk 4.2 부터는 각 컬럼 사이즈에 <code>:</code> 를 이용해서 skip width 를 지정할 수 있습니다.
그러니까 사이즈가 <code>6</code> 인 컬럼을 사용하고 싶은데 앞에 공백이 <code>2</code> 만큼 있으면
<code>2:6</code> 와 같이 작성하면 됩니다.</p>
<pre><code class="lang-bash">$ awk -v FIELDWIDTHS=<span class="hljs-string">"4 2:6 2:5 2:4 2:1 2:4 2:2"</span> <span class="hljs-string">'{
printf "(%s) (%s) (%s) (%s) (%s) (%s) (%s)\n",
$1, $2, $3, $4, $5, $6, $7
}'</span> file
(<span class="hljs-number">4051</span>) (S00001) (<span class="hljs-number">31228</span>) (<span class="hljs-number">3286</span>) (<span class="hljs-number">0</span>) ( ) (<span class="hljs-number">23</span>)
(<span class="hljs-number">4050</span>) (S00201) ( ) (<span class="hljs-number">4251</span>) (<span class="hljs-number">0</span>) (<span class="hljs-number">12.1</span>) (<span class="hljs-number">23</span>)
(<span class="hljs-number">4049</span>) (S00301) (<span class="hljs-number">31221</span>) (<span class="hljs-number">3021</span>) (<span class="hljs-number">1</span>) (<span class="hljs-number">14.4</span>) ()
(<span class="hljs-number">4048</span>) (S00213) (<span class="hljs-number">46578</span>) ( ) (<span class="hljs-number">0</span>) (<span class="hljs-number">23.2</span>) (<span class="hljs-number">43</span>)
(<span class="hljs-number">4047</span>) (S00113) ( ) (<span class="hljs-number">4250</span>) (<span class="hljs-number">0</span>) ( ) (<span class="hljs-number">43</span>)
(<span class="hljs-number">4046</span>) (S00122) (<span class="hljs-number">31225</span>) (<span class="hljs-number">4249</span>) (<span class="hljs-number">0</span>) (<span class="hljs-number">45.5</span>) (<span class="hljs-number">21</span>)
</code></pre>
<p>또한 gawk 4.2 부터는 마지막 컬럼 사이즈로 이후 모두를 나타내는 <code>*</code> 도 사용할 수 있습니다.</p>
<pre><code class="lang-bash">$ awk -v FIELDWIDTHS=<span class="hljs-string">"4 2:6 2:*"</span> <span class="hljs-string">'{
printf "(%s) (%s) (%s)\n", $1, $2, $3
}'</span> file
(<span class="hljs-number">4051</span>) (S00001) (<span class="hljs-number">31228</span> <span class="hljs-number">3286</span> <span class="hljs-number">0</span> <span class="hljs-number">23.6</span>)
(<span class="hljs-number">4050</span>) (S00201) ( <span class="hljs-number">4251</span> <span class="hljs-number">0</span> <span class="hljs-number">12.1</span> <span class="hljs-number">23.6</span>)
(<span class="hljs-number">4049</span>) (S00301) (<span class="hljs-number">31221</span> <span class="hljs-number">3021</span> <span class="hljs-number">1</span> <span class="hljs-number">14.4</span>)
(<span class="hljs-number">4048</span>) (S00213) (<span class="hljs-number">46578</span> <span class="hljs-number">0</span> <span class="hljs-number">23.2</span> <span class="hljs-number">43.9</span>)
(<span class="hljs-number">4047</span>) (S00113) ( <span class="hljs-number">4250</span> <span class="hljs-number">0</span> <span class="hljs-number">43.9</span>)
(<span class="hljs-number">4046</span>) (S00122) (<span class="hljs-number">31225</span> <span class="hljs-number">4249</span> <span class="hljs-number">0</span> <span class="hljs-number">45.5</span> <span class="hljs-number">21.6</span>)
</code></pre>
<p>FIELDWIDTHS 방법을 사용한 후에 다시 FS 나 FPAT 방법을 사용하려면 해당 변수값을
다시 설정해 주면 됩니다.</p>
<pre><code class="lang-bash">$ awk -v FIELDWIDTHS=<span class="hljs-string">"6 8 7 6 3 6 4"</span> <span class="hljs-string">'{ print NF } NR==3 { FS=FS }'</span> file
<span class="hljs-number">7</span>
<span class="hljs-number">7</span>
<span class="hljs-number">7</span>
<span class="hljs-number">6</span> <span class="hljs-comment"># NR==3 에서 FS=FS 가 설정되어 4 번째 레코드부터 FS 방법이 사용된다.</span>
<span class="hljs-number">5</span>
<span class="hljs-number">7</span>
</code></pre>
<p><a name="FPAT"></a></p>
<h4 id=""><img src="images/buttons/fpat.png" alt=""></h4>
<p>다음은 CSV( comma-separated values ) 레코드의 한 예입니다.
전체 필드 개수가 7 개인 레코드인데 3 번째 필드값의 double quotes 안에서도 <code>,</code> 가 사용되어
실제 필드 개수가 8 개로 나오고 있습니다.</p>
<pre><code class="lang-bash">Robbins,Arnold,<span class="hljs-string">"1234 A Pretty Street, NE"</span>,MyTown,MyState,<span class="hljs-number">12345</span>-<span class="hljs-number">6789</span>,USA
<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-comment"># 필드 개수가 총 8 개로 나온다.</span>
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">'Robbins,Arnold,"1234 A Pretty Street, NE",MyTown,MyState,12345-6789,USA'</span> |
awk <span class="hljs-string">'{ print NF }'</span> FS=,
<span class="hljs-number">8</span>
</code></pre>
<p>이와 같은 문제를 해결하기 위한 것이 FPAT( Field PATtern ) 인데요.
기존의 FS 를 이용하는 방법이 레코드에서 FS 값을 매칭하여 분리했다면
FPAT 을 이용하는 방법은 실제 필드값 자체를 매칭합니다.</p>
<p>따라서 위 예제와 같은 경우 FPAT 값은 다음 두 가지로 설정할 수 있습니다.</p>
<ul>
<li><p><code>[^,]+</code> ( <code>,</code> 이외의 값 )</p>
</li>
<li><p><code>"[^"]+"</code> ( <code>" "</code> 로 둘러싸인 값 )</p>
</li>
</ul>
<pre><code class="lang-bash"><span class="hljs-comment"># 필드 개수가 정확히 7 개로 나온다</span>
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">'Robbins,Arnold,"1234 A Pretty Street, NE",MyTown,MyState,12345-6789,USA'</span> |
awk -v FPAT=<span class="hljs-string">'[^,]+|"[^"]+"'</span> <span class="hljs-string">'{ print NF }'</span>
<span class="hljs-number">7</span>
$ <span class="hljs-built_in">echo</span> <span class="hljs-string">'Robbins,Arnold,"1234 A Pretty Street, NE",MyTown,MyState,12345-6789,USA'</span> |
awk <span class="hljs-string">'{printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", $1,$2,$3,$4,$5,$6,$7}'</span> FPAT=<span class="hljs-string">'[^,]+|"[^"]+"'</span>
Robbins
Arnold
<span class="hljs-string">"1234 A Pretty Street, NE"</span>
MyTown
MyState
<span class="hljs-number">12345</span>-<span class="hljs-number">6789</span>
USA
</code></pre>
<p>예제2 )</p>
<pre><code class="lang-bash">$ <span class="hljs-built_in">echo</span> <span class="hljs-string">'ip="192.168.1.1" method="http https ftp" period="5"'</span> |
awk <span class="hljs-string">'{ printf "(%s)\n(%s)\n(%s)\n",$1,$2,$3 }'</span> FPAT=<span class="hljs-string">'[^ ]+="[^"]*"'</span>
(ip=<span class="hljs-string">"192.168.1.1"</span>)
(method=<span class="hljs-string">"http https ftp"</span>)
(period=<span class="hljs-string">"5"</span>)
</code></pre>
<h4 id="fpat-을-이용한-필드-추출">FPAT 을 이용한 필드 추출</h4>
<p>FPAT 을 이용하면 특정 스트링 패턴을 필드로 추출하여 사용할 수 있습니다.
다음 예제를 보면 <code>{{AAA}}</code>,<code>{{BBB}}</code> 이 각각 <code>$1</code>, <code>$2</code> 필드 변수에 할당된 것을 볼 수 있습니다.</p>
<pre><code class="lang-bash">$ <span class="hljs-built_in">echo</span> <span class="hljs-string">"{{AAA}} design will serve as the basis for the planned Mars {{BBB}} rover."</span> |
awk <span class="hljs-string">'{ print $1; print $2; print NF }'</span> FPAT=<span class="hljs-string">'{{[A-Z]+}}'</span>
{{AAA}}
{{BBB}}
<span class="hljs-number">2</span>
</code></pre>
<hr>
<p>FPAT 방법을 사용한 후에 다시 FS 나 FIELDWIDTHS 방법을 사용하려면 해당 변수값을 다시 설정해 주면 됩니다.
현재 사용되고 있는 방법은 <code>PROCINFO["FS"]</code> 값으로 조회할 수 있습니다.</p>
<pre><code class="lang-bash">$ seq <span class="hljs-number">3</span> | awk <span class="hljs-string">'{
check()
FIELDWIDTHS = FIELDWIDTHS
check()
FPAT = FPAT
check()
FS = FS
}
function check() {
using_fs = (PROCINFO["FS"] == "FS")
using_fw = (PROCINFO["FS"] == "FIELDWIDTHS")
using_fpat = (PROCINFO["FS"] == "FPAT")
if (using_fs)
print "using FS"
else if (using_fw)
print "using FIELDWIDTHS"
else if (using_fpat)
print "using FPAT"
}'</span>
using FS
using FIELDWIDTHS
using FPAT
using FS
using FIELDWIDTHS
using FPAT
using FS
using FIELDWIDTHS
using FPAT
------------------------------------
$ seq <span class="hljs-number">10</span> | awk <span class="hljs-string">'{
if (NR == 4) FIELDWIDTHS = FIELDWIDTHS
if (NR == 7) FPAT = FPAT
check()
}
function check() {
using_fs = (PROCINFO["FS"] == "FS")
using_fw = (PROCINFO["FS"] == "FIELDWIDTHS")
using_fpat = (PROCINFO["FS"] == "FPAT")
if (using_fs)
print "using FS"
else if (using_fw)
print "using FIELDWIDTHS"
else if (using_fpat)
print "using FPAT"
}'</span>
using FS
using FS
using FS
using FIELDWIDTHS
using FIELDWIDTHS
using FIELDWIDTHS
using FPAT
using FPAT
using FPAT
using FPAT