我有一个文件:
$ od -c testData.txt
0000000 6 7 7 7 1 0 \t 0 \t 1 \t L P A Y \t
0000020 F 6 3 5 P 3 B \t L P A Y 0 0 0 0
0000040 1 \t F R M \t H O U S T O N G R
0000060 O U P ( a k a C O R P O R A
0000100 T E A D V O C A T E S I N C
0000120 . ) T H E \t \t \t \t S a c r a m
0000140 e n t o \t C A \t 9 5 8 1 4 - 2 8
0000160 2 5 \t ( 9 1 6 ) 4 4 7 - 9 8 8
0000200 4 \t \t 6 4 9 9 . 9 8 \t 1 7 . 1 9
0000220 \t 0 \t \t 6 5 1 7 . 1 7 \t 3 9 3 0
0000240 9 . 2 3 \t N \t \t \t \r \n
0000253
我有一个脚本可以做一件事:
#!/usr/bin/perl
$line = <STDIN>;
@p = split '\t', $line;
chomp(@p);
for ($idx = 0; $idx < scalar(@p); $idx++) { print $idx.": \"".$p[$idx]."\"\n"; }
exit(0);
我在 Mac OS X 10.8.5 上并使用股票 perl(perl 5,版本 12,为 darwin-thread-multi-2level 构建的 subversion 4 (v5.12.4))。
如果我不通过 col 传输数据,那么我会从行尾看到一个故障。如果我这样做了,那么 split() 函数将忽略一些选项卡。不是全部,只是少数。真的。恼人的。
$ ./testSplit < testData.txt
0: "677710"
1: "0"
2: "1"
3: "LPAY"
4: "F635P3B"
5: "LPAY00001"
6: "FRM"
7: "HOUSTON GROUP (aka CORPORATE ADVOCATES INC.) THE"
8: ""
9: ""
10: ""
11: "Sacramento"
12: "CA"
13: "95814-2825"
14: "(916) 447-9884"
15: ""
16: "6499.98"
17: "17.19"
18: "0"
19: ""
20: "6517.17"
21: "39309.23"
22: "N"
23: ""
24: ""
"5: "
$
请参阅上面最后一行的轻微故障。
$ col < testData.txt | ./testSplit
0: "677710"
1: "0"
2: "1"
3: "LPAY"
4: "F635P3B LPAY00001"
5: "FRM"
6: "HOUSTON GROUP (aka CORPORATE ADVOCATES INC.) THE"
7: ""
8: ""
9: ""
10: "Sacramento"
11: "CA"
12: "95814-2825"
13: "(916) 447-9884"
14: ""
15: "6499.98 17.19"
16: "0"
17: ""
18: "6517.17 39309.23"
19: "N"
$
有没有搞错!