From 2459a14ea3d6cbcdb88f5529d798b55054620136 Mon Sep 17 00:00:00 2001 From: Ray Miller Date: Fri, 12 Aug 2011 15:06:00 +0100 Subject: [PATCH] Initial check-in of yaml2csv and collapse scripts --- collapse | 23 +++++++++++++++++++++++ yaml2csv | 25 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100755 collapse create mode 100755 yaml2csv 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 ); +}