LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 04-03-2009, 03:44 PM   #1
jaykup
Member
 
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:


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

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


Reply


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 02:30 AM.

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