LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 09-13-2011, 05:43 AM   #1
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Rep: Reputation: 15
Convert content of same file into CSV file


I have one file with below content.

Quote:
$cat file1
#
web1
id1
user1
edl1
#
web2
id2
user2
edl2
#
web3
id3
user3
edl3
There are 5 lines after every "#" charecter. I want teh content of every line to be placed horizontally after #.

The file should look like the one below.

Quote:
$cat file2
# web1 id1 user1 edl1
# web2 id2 user2 edl2
# web3 id3 user3 edl3
OR

Quote:
$cat file2
#,web1,id1,user1,edl1
#,web2,id2,user2,edl2
#,web3,id3,user3,edl3
I know if the content would have been in different files, paste command would have made the trick. But I dont know how to achieve it in this scenario. I believe sed can do it. Anyone can please help.

Thanks,
Ravi
 
Old 09-13-2011, 08:23 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
awk 'NR > 1 && /#/{$0 = "\n"$0}1' ORS=" " file
 
Old 09-13-2011, 10:43 PM   #3
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Original Poster
Rep: Reputation: 15
Smile

Thanks a lot Grail. This is exactly what I needed.
 
Old 09-14-2011, 02:33 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please mark as SOLVED once you have a solution.
 
  


Reply



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
how to convert a simplte text file into csv using awk script certteam Linux - General 1 09-15-2010 12:23 AM
looking for software to convert multiple csv files to a single xml file Rocket-boy Linux - Software 6 10-28-2009 10:03 AM
convert columns to rows (tab separated file to csv) doug23 Programming 16 08-16-2009 09:14 PM
Convert a number on each line to CSV file OlRoy Programming 9 08-20-2008 11:05 AM
Convert into CSV file say_hi_ravi Programming 4 07-17-2008 03:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration