/[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

Contents of /bin/csv2yaml.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (show annotations)
Sun Mar 15 20:01:21 2009 UTC (15 years ago) by dpavlin
File MIME type: text/plain
File size: 1009 byte(s)
if files have magic delimiter # then we will split each
occurance into it's own field
1 #!/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 foreach ( 0 .. $#fields ) {
41 my $n = $columns[$_];
42 my $v = $fields[$_];
43
44 $v =~ s{\s*#\s*$}{};
45
46 if ( $v =~ m{#} ) {
47 my @v = split(/\s*#\s*/, $v);
48 foreach my $pos ( 0 .. $#v ) {
49 $hash->{ $n . '_' . $pos } = $v[$pos];
50 }
51 }
52 $hash->{ $n } = $v;
53 }
54
55 warn dump( $hash ) if $debug;
56
57 my $uuid = $fields[0];
58
59 DumpFile( "yaml/$uuid.yaml", $hash );
60 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26