LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using awk/sed to convert linefeed to csv, with some formatting (https://www.linuxquestions.org/questions/programming-9/using-awk-sed-to-convert-linefeed-to-csv-with-some-formatting-716658/)

jaykup 04-03-2009 03:44 PM

Using awk/sed to convert linefeed to csv, with some formatting
 
I have a file that looks like the one below, only much larger:


Code:

2009-02-20
this is some text
145 sometimes numbers and text with spaces.


2009-02-07
this is some different text
276 sometimes numbers and text with spaces.


2007-12-18
thisisoneword
13 sometimes numbers and text with spaces.



I would like to change it like this:

Code:

2009-02-20, "this is some text", 145, "sometimes numbers and text with spaces."
2009-02-07, "this is some different text", 276, "sometimes numbers and text with spaces."
2007-12-18, "thisisoneword", 13, "sometimes numbers and text with spaces."


Top line, always a date in that format.
Middle line, text or numbers
bottom line, always an integer, a space, then random text. I want to seperate the first number from the rest with a comma.

The pattern and spacing is exactly like above.

Kenhelm 04-03-2009 05:18 PM

Using GNU sed
Code:

sed  '/./ {
N
N
s/\(.*\)\n\(.*\)\n\([0-9]\+\) \(.*\)/\1, "\2", \3, "\4"/
}
/^$/d' infile > outfile



All times are GMT -5. The time now is 12:06 PM.