diff --git a/collapse b/collapse new file mode 100755 index 0000000..c053df6 --- /dev/null +++ b/collapse @@ -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; +} diff --git a/yaml2csv b/yaml2csv new file mode 100755 index 0000000..c9706c7 --- /dev/null +++ b/yaml2csv @@ -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 ); +}