shell - Convert a file into CSV with newline as the delimiter -
i've got lot of data new line delimited, copied raw server. however, cannot convert csv replacing \n
comma since need have fields. there 9 fields in occasion. how can convert data?
keeping in mind last element doesn't need comma since need retain newline character.
i horrible @ regular expression foo, since have loop being needed, assume need make shell script?
any appreciated.
example data:
name logon user ip address version last login restart required foo1 foo2 foo3 jon weinraub jweinraub 10.18.66.10 3.1.1.1 2013-08-19 14:33:11 no bar1 bar2 bar3 homer simpson ....
so should be
name,logon user, ip,...foo3 jon weinraub,jweinraub,10.18.66.10,...bar3 homer simpson,....
a elaborate way (but it's easy understand , modify) using awk
:
create file makecsv.awk
following script:
begin { count = 0; } { count++; if (count == 9) { count = 0; printf "%s\n", $0; } else { printf "%s, ", $0; } }
then can execute command line with
awk -f makecsv.awk myinputfile > myoutputfile.csv
Comments
Post a Comment