非常に大きなCSVには次のデータがあります。
"sortorder","name","description"
"1","B.1","Boiler room"
"2","1.1","First office"
"3","1.2","Second office"
etc...
私は次のように終わりたいと思います。
{ name => 'B.1', description => 'Boiler room', sortorder => 1 },
{ name => '1.1', description => 'First office', sortorder => 2 },
{ name => '1.2', description => 'Second office', sortorder => 3 },
etc.
PerlやBashを使ってこれを達成するにはどうすればよいですか?
答え1
一般的なsedソリューション(フィールドを使用しない)
sed '
1{
h
d
}
G
:a
s/\(^\|,\)\([^=]\+\n\)"\?\([^,]\+\)"\?,\?/\1 \3 => \2/
ta
s/^/{/
s/\n/ },/
' file.csv
しかし、Riot Realmを最後のゾーンにしたい場合は追加してください。
s/\([^,]*\), \([^\n]*\)/\2, \1/
後ろにta
答え2
少しルビーワンライナー
ruby -rcsv -ne '
row = CSV.parse_line($_)
if $. == 1 then
keys = row
else
h = Hash[keys.zip(row)]
puts h.to_s + ","
end
'
出力
{"sortorder"=>"1", "name"=>"B.1", "description"=>"Boiler room"},
{"sortorder"=>"2", "name"=>"1.1", "description"=>"First office"},
{"sortorder"=>"3", "name"=>"1.2", "description"=>"Second office"},
またはPerl、完全なデータ構造(ハッシュ配列)を生成する
use strict;
use warnings;
use v5.10;
use Text::CSV;
use autodie;
use Data::Dump qw/ dump /;
my @rows;
open my $fh, "<", shift @ARGV;
my $csv = Text::CSV->new( {binary => 1} );
my $keys = $csv->getline($fh);
while (my $row = $csv->getline($fh) ) {
my %h;
@h{ @$keys } = @$row;
push @rows, \%h;
}
# print it
say dump \@rows;
出力
[
{ description => "Boiler room", name => "B.1", sortorder => 1 },
{ description => "First office", name => 1.1, sortorder => 2 },
{ description => "Second office", name => 1.2, sortorder => 3 },
]
答え3
Perlスクリプトの使用
#!/usr/bin/perl
open INFILE, "<", "inputfile";
open OUTFILE, ">", "outputfile";
$header_line = <INFILE>;
chop($header_line);
@headers = split /,/, $header_line;
foreach $header_column (@headers) {
$header_column =~ s/^"(.*)"$/$1/;
}
while(<INFILE>) {
chop($_); # clear the newline at the end of string
@fields = split /,/; # split string by ,
foreach $field (@fields) {
$field =~ s/^"(.*)"$/$1/;
}
for($i = 0; $i <= $#headers; $i++) {
$fields[$i] = $headers[$i] . " => " . $fields[$i];
}
print OUTFILE "{ " . join(', ', @fields) . "},\n";
}
そしてあなたのものinputfile
"sortorder","name","description"
"1","B.1","Boiler room"
"2","1.1","First office"
"3","1.2","Second office"
出力を生成する
{ name => 'name', description => 'description', sortorder => 'sortorder' },
{ name => 'B.1', description => 'Boiler room', sortorder => '1' },
{ name => '1.1', description => 'First office', sortorder => '2' },
{ name => '1.2', description => 'Second office', sortorder => '3' },
スクリプトは最初の行から始まり、列の順序を維持します。