LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-16-2004, 01:10 AM   #1
markraem
Member
 
Registered: Nov 2003
Posts: 82

Rep: Reputation: 15
How to write the contents of a column as a row in linux


Hi,

suppose I have an ACII file called data.txt that looks like this :

%cat data.txt
item1
item2
item3

I am looking for a command (or script) that converts the column to a row,
meaning :

%command data.txt will result in :
item1, item2, item3

or

%command data.txt > row.txt will yield :
%cat row.txt
item1,item2,item3

(The comma in between may also be a space or TAB )

Does a command like this exist in linux ?
 
Old 07-16-2004, 05:17 AM   #2
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
awk '{printf "%s, ",$0} END {printf "\n"}' data.txt

Or, if you care about a trailing comma...

awk '{printf "%s, ",$0} END {printf "\n"}' data.txt | sed 's/, $//'

John
 
Old 07-16-2004, 11:56 AM   #3
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
Check out the Advanced Bash Scripting Guide for more info on arrays.

ITEMS=(`cat data.txt`);echo ${ITEMS[@]}




FYI: Change one character and you have a crude "word count" program:

ITEMS=(`cat /path/to/anyfile`);echo ${#ITEMS[@]}
 
Old 07-16-2004, 02:29 PM   #4
wipe
Member
 
Registered: Jun 2004
Location: High Green
Distribution: Fedora Core 4
Posts: 180

Rep: Reputation: 30
cat data.txt|tr '\n' ,

Simple enough? Great challenge, BTW. If you want a trailing newline,

echo `cat data.txt|tr '\n' ,`

Count words with

cat data.txt|wc -w

Regards
Simon
 
Old 07-17-2004, 07:35 PM   #5
markraem
Member
 
Registered: Nov 2003
Posts: 82

Original Poster
Rep: Reputation: 15
Tnx alot guys,

I am using the solution of wipe, because it looks the most simple to me

It works like a champ.
 
  


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 1 column into several rows in Linux? markraem Linux - Software 9 03-30-2010 11:24 AM
Transposing a column into a row mayyash Linux - General 1 09-30-2005 02:23 AM
Cd has different contents in Linux then in Windows sephiro499 Linux - Newbie 10 10-29-2004 08:39 AM
Linux, Threatened... news column in rediff.com ashesh General 4 09-13-2003 10:01 AM
can i copy windows contents into linux gurpreetpall Linux - General 5 03-20-2002 03:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:30 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