juanito23 has asked for the wisdom of the Perl Monks concerning the following question:
Hello team, I have a very mis-formatted string exported to csv file from oracle db table (around 60 columns table) Seems each line is very badly formatted so I got some perl code to format the same. As example a single original line from csv file when I print in a Linux terminal:
""6357445" + "1349947" + + "0" + + "1" + + "3" + + "2" + + "1" + "1" + + "1" + + "1" + + "-2" + + "1394531830" + + "1394531830" + "14 +15599200" + "0" + + "0" + + "0" + + "0" + + "196" + +"29240" + "378" + + "1394531846" + + "0" + + "0" + + "0" + + "8201" + "0" + + "64" + + "0" + + "2" + + "89799" + + "8201" + "8980 +5" + "-1" + + "-1" + + "Local Cell id=2, Cell Name=21842C11" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + "0" + +"1394531057" + "1394531057 +" + "1415599200" + + "1394531092" + + "" + + + + + + + + + + + "0" + + "RAT_INFO=GL, AFFECTED_RAT=L" + + + + + + + + + + + + + + + + "" + + "" + + + + + + "10.35.201.146" + + + + + + + + + + + "195;" + + + + + + + + + + + "0" + " +5705354" + "0" + + "" + + "" + + + "0" + + "0" + "Cell""
This looks odd, but that's only 1 single line, splitted in some weird characters. After some perl code I got this:
Result of this ismy $file = `filename`; chomp($file); open(my $data, '<', $file) or die "Couldn't open '$file'\n "; while (my $line = <$data>){ $line =~ s/\s+/ /g ; $line =~ s/\" \"/\"\"/g ; $line =~ s/\n+//g ; $line =~ s/\s+$//; if ($line !~ /select/ && $line !~ /spool/ && $line !~ /\|\|/ + ){ my @array = $line; my $result; for (@array){ chomp; $result .= $_ . " "; } $result =~ s/\s+//g; print "$result\n";
Ok, so I have one line still splited for some reason. Here each field is separated by double quote "" character, so we can expect empty fields as well. My target is to have this in a single line in order to access to each element, even the NULL ones!""6357445""1349947""0""1""3""2""1""1""1""1""-2""1394531830""1394531830 +""1415599200""0""0""0""0""196""29240""378""1394531846""0""0""0""8201" +"0""64""0""2""89799""8201""89805""-1""-1""LocalCellid=2,CellName=2184 +2C11""0""1394531057""1394531057""1415599200""1394531092""""0""RAT_INF +O=GL,AFFECTED_RAT=L""""" "10.35.201.146""195;""0""5705354""0""""""0""0""Cell""
Hope this is clear, any thoughts?
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: extract string columns with perl
by LanX (Saint) on Apr 15, 2014 at 11:12 UTC | |
by juanito23 (Novice) on Apr 16, 2014 at 08:06 UTC | |
|
Re: extract string columns with perl
by Laurent_R (Canon) on Apr 15, 2014 at 17:04 UTC | |
by juanito23 (Novice) on Apr 16, 2014 at 08:10 UTC | |
by Laurent_R (Canon) on Apr 16, 2014 at 08:41 UTC | |
by juanito23 (Novice) on Apr 16, 2014 at 08:46 UTC |