Convert text to CSV

Hi Gurus

I need urgent help to convert a flat log file into csv format to load into database.

Log looks like:

a=1
b=2
c=3
a=4
b=5
c=6
Only the values at right side of = will come into csv and it should create a new line once it receives "a" field.

What have you tried to solve this..

Hi Shamrock

I have tried to filter out the data only on the right side of = and use a perl script for the same..

But unfortunately the newline clause is not working. it prints all in same line. I am a very newbie to the same... help is appreciated:

 
perl -ne'print/=\s*(.*)/,(eof)?$/:","' <file_name>
$ cat sample7.txt | sed 's/a=/~a=/g' | tr "\n" " " | tr "~" "\n"

a=1 b=2 c=3
a=4 b=5 c=6

which puts all related data on one line.

I need the same in below format:

1,2,3
4,5,6

$ cat sample7.txt | sed 's/a=/~a=/g' | tr "\n" "=" | tr "~" "\n" | awk -F"=" 'OFS="," {print $2,$4,$6}' | grep '[0-9]'
1,2,3
4,5,6

How about perl ?

#!/usr/bin/perl

while (<DATA>) {
        chomp;
        ($k,$v)=split(/\=/);
        push(@{$k},$v);
        $highIndex=$#{$k} if $highIndex < $#{$k};
        $hash{$k} unless $hash{$k}++;
}

for($i=0;$i<=$highIndex;$i++) {
print ${$_}[$i],"," foreach sort(keys %hash);
print "\n";
}

__DATA__
a=1
b=2
c=3
a=4
b=5
c=6

Assuming there are no missing columns, i.e. each group always has three lines:

sed 's/.*=//g' file |paste -d, - - -

Hi

The scripts change the file to csv but the problem is in my file the left columns are not always same... :frowning:

Any chance I can process set of data from the file? e.g:

process first a= field to just before second a=field and then so on? :confused: