0

数十万行、単一列、スペース、引用符、カンマのない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の方法を本当に好みます。それは間違いなく私の頭の上にあるので、助けてください、ありがとう。

4

3 に答える 3

0

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);

標準出力に出力するように実行することもperl convert.pl file、シェルでファイルにリダイレクトすることもできます。

于 2013-02-12T18:35:53.377 に答える
0

したがって、ファイルから50行を読み取り、コンマを使用してそれらを連結する必要がありますか?これが私が(Pythonを使って)思いついたものです:

import sys;

fd = open("foo.txt");
for i in range(3):
    for j in range(50):
        line = fd.readline().rstrip()
        if (j != 0):
            sys.stdout.write(",")
        sys.stdout.write(line)
    sys.stdout.write("\n")
fd.close()

350行のブロック数と"foo.txt"実際のファイル名に変更します。これはstdoutに書き込みます。それが問題である場合は、書き込み用に別のファイルを開くことができます。

于 2013-02-12T18:36:35.557 に答える
0

bashで:

#!/bin/bash

out_file=output.csv
line_width=50

count=0

while read line
do
  echo -n $line >> $out_file
  count=$(($count+1))

  if [ $count -lt $line_width ]
  then
    echo -n "," >> $out_file
  else
    echo "" >> $out_file
    count=0
  fi
done

# strip trailing commas
sed 's/,$//g' < $out_file > "$out_file.tmp" && mv "$out_file.tmp" $out_file

このスクリプトが含まれているwrap.shとすると、コマンドラインから実行します。

$ ./wrap.sh < file.txt

出力はになりますoutput.csv

于 2013-02-12T19:32:32.720 に答える