我有一个包含数十万行、单列、没有空格、没有引号、没有逗号的 CSV 文件。
line1
line2
line3
line4
我需要将它拆分为 1 列,但每行最多包含 50 行,以逗号分隔。
所以:
line1,line2,line3,line4 all the way to line50
line51,line52,line53, all the way to line100
line101,line102,line103 all the way to line150
直到 CSV 完成。
我有 FFE、CSVTOOLS,我运行的是 Linux,所以更喜欢 Linux 方法。 这绝对超出了我的能力范围,所以请帮忙,谢谢。
最佳答案
我假设您可以运行 Perl
脚本。我无法保证速度,但根据您提供的详细信息,它会完成工作。
#!/usr/bin/perl
use strict;
use warnings;
my $file = $ARGV[0];
open( my $fh, "<", $file ) or die $!;
my $cnt = 0;
while (<$fh>) {
++$cnt;
if ( $cnt < 50 ) {
$_ =~ tr/\n/,/;
print $_;
}
else {
print "$_";
$cnt = 0;
}
}
close($fh);
如果您希望将其打印到标准输出,或者只是在 shell 中将其重定向到文件,则可以将其作为 perl Convert.pl 文件
运行。
关于linux - 将逐行 CSV 转换为逗号 CSV 的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14838971/