/[virtual-ldap]/bin/csv2yaml.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /bin/csv2yaml.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (hide annotations)
Sun Mar 15 20:39:55 2009 UTC (15 years ago) by dpavlin
File MIME type: text/plain
File size: 1047 byte(s)
strip spaces
1 dpavlin 9 #!/usr/bin/perl
2    
3     use warnings;
4     use strict;
5    
6     # cvs2dbi.pl
7     #
8     # 03/15/2009 05:06:53 PM CET Dobrica Pavlinusic <dpavlin@rot13.org>
9    
10     use Data::Dump qw/dump/;
11     use File::Slurp;
12     use YAML qw/DumpFile/;
13     use Text::CSV;
14     use Encode qw/decode/;
15    
16     my $debug = 0;
17    
18     my $path = shift @ARGV || die "usage: $0 file.csv\n";
19    
20     my $csv = read_file( $path );
21     $csv = decode('utf-16', $csv);
22    
23     my @columns;
24    
25     foreach my $line ( split(/\r?\n/, $csv) ) {
26    
27     warn "## $line\n";
28    
29     my @fields = split(/;/, $line);
30     if ( ! @columns ) {
31     @columns = @fields;
32     warn "# columns = ",dump( @columns ) if $debug;
33     next;
34     }
35    
36     my $hash;
37    
38     warn "# fields = ",dump( @fields ) if $debug;
39    
40 dpavlin 11 foreach ( 0 .. $#fields ) {
41     my $n = $columns[$_];
42     my $v = $fields[$_];
43 dpavlin 9
44 dpavlin 11 $v =~ s{\s*#\s*$}{};
45 dpavlin 12 $v =~ s{^\s+}{};
46     $v =~ s{\s+$}{};
47 dpavlin 11
48     if ( $v =~ m{#} ) {
49     my @v = split(/\s*#\s*/, $v);
50     foreach my $pos ( 0 .. $#v ) {
51     $hash->{ $n . '_' . $pos } = $v[$pos];
52     }
53     }
54     $hash->{ $n } = $v;
55     }
56    
57 dpavlin 9 warn dump( $hash ) if $debug;
58    
59     my $uuid = $fields[0];
60    
61     DumpFile( "yaml/$uuid.yaml", $hash );
62     }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26