LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-19-2008, 12:23 PM   #1
OlRoy
Member
 
Registered: Dec 2002
Posts: 306

Rep: Reputation: 86
Convert a number on each line to CSV file


I have a file that contains a lot of lines, and each line has only one number on it. What would be the easiest way to convert it into one line containing CSVs?

Sample data:

Code:
2
4
4
4
8
1
I want it in this format: 2,4,4,4,8,1

If I had a choice, I'd like it to be done in PERL.
 
Old 08-19-2008, 12:33 PM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Code:
tr "\n" "," < input_file > output_file
You have one comma at the end of the last record in output_file, but you should be able to get rid of that easily enough if you really have to.

edit: Sorry, didn't see the requirement to do this in perl. I'm useless at perl, so can't help there.

Last edited by pwc101; 08-19-2008 at 12:37 PM.
 
Old 08-19-2008, 12:56 PM   #3
OlRoy
Member
 
Registered: Dec 2002
Posts: 306

Original Poster
Rep: Reputation: 86
I like your way better than if it wasn't done in PERL. Thanks!
 
Old 08-19-2008, 01:01 PM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
No problem.

There's also the column command, but I couldn't get it to separate the fields with commas; it seems like it's designed with that sort of functionality in mind though. Still, if this works, it'll do!
 
Old 08-19-2008, 03:49 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Great solution! However, if the column file terminates with a newline, you will get an extra comma at the end of the output string. Maybe something like this does the trick:
Code:
echo $(cat file) | tr " " ","
Sorry, I'm not good with Perl, too!
 
Old 08-19-2008, 06:42 PM   #6
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
Assuming data is in abc.txt, perl one liner would be:

Code:
perl -e 'chomp(@data=<STDIN>);print join(",",@data),"\n";' < abc.txt
Hope this helps.
 
Old 08-20-2008, 05:02 AM   #7
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Two more methods:-
Code:
paste -sd, infile > outfile

sed ':a N;s/\n/,/;ta' infile > outfile
 
Old 08-20-2008, 07:43 AM   #8
OlRoy
Member
 
Registered: Dec 2002
Posts: 306

Original Poster
Rep: Reputation: 86
Choices, choices, choices. Now I have a different way to do it for every day of the week. Thanks guys!
 
Old 08-20-2008, 09:06 AM   #9
wget
LQ Newbie
 
Registered: Apr 2008
Distribution: Slackware 12.0 Kernel 2.6.25, LFS 2.6.25.1 Kernel
Posts: 25

Rep: Reputation: 15
And just for fun another one using the vi editor:

With the cursor at the start of the very first line

:.,$j

and then

:s/ /,/g

delete any extra comma's at the end
 
Old 08-20-2008, 11:05 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
since there's no entry for awk yet
Code:
# awk '{$1=$1}1' RS="" OFS="," file
2,4,4,4,8,1
 
  


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
Convert into CSV file say_hi_ravi Programming 4 07-17-2008 03:25 AM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM
how to get the number of line of a file? loplayers Linux - Newbie 2 10-24-2007 04:10 AM
looking for a perl script to convert html table data into a csv file swiftguy121 Linux - Software 2 04-25-2007 07:28 PM
Count number of line in a file. philipina Programming 7 03-18-2004 05:04 PM

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

All times are GMT -5. The time now is 04:40 PM.

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