linux - 将逐行 CSV 转换为逗号 CSV 的最简单方法

标签 linux csv

我有一个包含数十万行、单列、没有空格、没有引号、没有逗号的 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/

相关文章:

android - c和uinput,如何处理不同的语言

linux - 在 Linux 内核中检测无限循环的简单方法

python - django crontab 作业不工作

java - 如何将单行数据插入到多个表中

shell - 从命令行对 .csv 进行排序

linux - Golang 中的 slice 导致空白终端和困惑的线程

android - 如何禁用安卓的触摸屏设备?

android - java.io.IOException : Un-terminated quoted field at end of CSV line 异常

python - 读取 .csv 文件时在 Python 中解析日期的最快方法?

sql-server - 将数据从 Excel/CSV 导入到 SQL Server