Initial check-in of yaml2csv and collapse scripts
This commit is contained in:
parent
a40cdd4259
commit
2459a14ea3
2 changed files with 48 additions and 0 deletions
23
collapse
Executable file
23
collapse
Executable file
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/env perl
|
||||
|
||||
use strict;
|
||||
use warnings FATAL => 'all';
|
||||
|
||||
my ( $key, @values ) = next_record();
|
||||
|
||||
while ( defined $key ) {
|
||||
my ( $next_key, @next_values ) = next_record();
|
||||
if ( defined $next_key and $next_key eq $key ) {
|
||||
push @values, @next_values;
|
||||
}
|
||||
else {
|
||||
print join( "\t", $key, @values ) . "\n";
|
||||
$key = $next_key;
|
||||
@values = @next_values;
|
||||
}
|
||||
}
|
||||
|
||||
sub next_record {
|
||||
defined( local $_ = <> ) or return;
|
||||
chomp; split;
|
||||
}
|
25
yaml2csv
Executable file
25
yaml2csv
Executable file
|
@ -0,0 +1,25 @@
|
|||
#!/usr/bin/env perl
|
||||
|
||||
use strict;
|
||||
use warnings FATAL => 'all';
|
||||
|
||||
use YAML::Any;
|
||||
use CSV::Writer;
|
||||
use Getopt::Long;
|
||||
|
||||
my @columns;
|
||||
|
||||
GetOptions(
|
||||
'columns=s@' => sub {
|
||||
my ( $opt, $value ) = @_;
|
||||
push @columns, split qr{\s*,\s*}, $value;
|
||||
}
|
||||
) and @columns > 0
|
||||
or die "Usage: $0 [--columns=...]\n";
|
||||
|
||||
my $csv = CSV::Writer->new( columns => \@columns );
|
||||
$csv->write( $csv->columns );
|
||||
|
||||
for my $d ( YAML::Any::LoadFile( \*STDIN ) ) {
|
||||
$csv->write( $d );
|
||||
}
|
Loading…
Reference in a new issue