/[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 13 - (show annotations)
Sun Mar 15 20:47:15 2009 UTC (10 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 1128 byte(s)
add field for mobite phone
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 $v =~ s{^\s+}{};
46 $v =~ s{\s+$}{};
47
48 if ( $v =~ m{#} ) {
49 my @v = split(/\s*#\s*/, $v);
50 foreach my $pos ( 0 .. $#v ) {
51 $hash->{ $n . '_' . $pos } = $v[$pos];
52 $hash->{ $n . '_mobitel' } = $v[$pos] if $n =~ m{tel} && $v[$pos] =~ m{^09};
53 }
54 }
55 $hash->{ $n } = $v;
56 }
57
58 warn dump( $hash ) if $debug;
59
60 my $uuid = $fields[0];
61
62 DumpFile( "yaml/$uuid.yaml", $hash );
63 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26