Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Search this Thread
Old 04-03-2009, 03:44 PM   #1
Registered: Jan 2006
Location: Mukwonago, WI
Distribution: Slackware 12
Posts: 77

Rep: Reputation: 16
Using awk/sed to convert linefeed to csv, with some formatting

I have a file that looks like the one below, only much larger:

this is some text
145 sometimes numbers and text with spaces.

this is some different text
276 sometimes numbers and text with spaces.

13 sometimes numbers and text with spaces.

I would like to change it like this:

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.
Old 04-03-2009, 05:18 PM   #2
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
Using GNU sed
sed  '/./ {
s/\(.*\)\n\(.*\)\n\([0-9]\+\) \(.*\)/\1, "\2", \3, "\4"/
/^$/d' infile > outfile


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
awk and csv file Firebar Programming 9 11-18-2010 05:00 AM
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
AWK: change a particular field in a csv file help help help!!!! haydar68 Programming 20 08-03-2008 01:10 AM
Convert into CSV file say_hi_ravi Programming 4 07-17-2008 03:25 AM

All times are GMT -5. The time now is 03:42 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration