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 11-29-2007, 08:03 PM   #1
skuz_ball
LQ Newbie
 
Registered: Nov 2007
Location: Melbourne, Australia
Posts: 14

Rep: Reputation: 0
shell script/command for converting columns/table onto a single line


Hi all,

I'm after a command, or a sequence of commands that can help me convert values in a table (columns and rows) to values on a single line. The number of columns in the input file is always the same but the number of rows can vary.

eg

The table might look like the following:

A B C
D E F
G H I
J K L

I need the information to be output onto a single line that looks like the following.

A B C D E F G H I J K L

Any help would be greatly appreciated.
 
Old 11-29-2007, 08:20 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,482

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
"echo" is doing this job.
Code:
$ a="a b c
d e f
g h i"
$ echo "$a"
a b c
d e f
g h i
$ echo $a
a b c d e f g h i
 
Old 11-29-2007, 08:34 PM   #3
skuz_ball
LQ Newbie
 
Registered: Nov 2007
Location: Melbourne, Australia
Posts: 14

Original Poster
Rep: Reputation: 0
that is fu@King brilliant.

thank you very much
 
Old 11-29-2007, 09:23 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by skuz_ball View Post
I need the information to be output onto a single line that looks like the following.

A B C D E F G H I J K L
Code:
tr -d '\n '  < file_with_table
 
Old 11-29-2007, 10:23 PM   #5
skuz_ball
LQ Newbie
 
Registered: Nov 2007
Location: Melbourne, Australia
Posts: 14

Original Poster
Rep: Reputation: 0
I ended up using the echo command in my script. It took two lines instead of one but I prefer the simplicity. I did the following.

Quote:
file=$(more file_with_table)
echo $file > one-line-file
I'm really only beginning with shell scripts so I reckon I'll be using the more basic commands at first.
 
Old 11-30-2007, 12:54 AM   #6
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,749

Rep: Reputation: 461Reputation: 461Reputation: 461Reputation: 461Reputation: 461
You probably should be using 'cat' instead of 'more'.
 
Old 11-30-2007, 12:55 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

Rep: Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022
file=$(<yourfile)
 
Old 11-30-2007, 01:20 AM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
The "cat file | tr '\n' ' ' >newfile" example will add a space after the last item if the last lines ends with a newline as is apt to be the case.
The 2nd post isn't taking the input from a file.
The "file=$(<yourfile); echo $file < newfile" example works perfectly.

I think the 2nd post showing the difference between $a and "$a" and the later one completed it by entering the file into a variable:
a=$(<temp)
~> echo "$a" >newfile
A B C
D E F
G H I
K L M
N O P
~> echo $a >newfile
A B C D E F G H I K L M N O P
 
Old 11-30-2007, 02:54 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,482

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Using an intermediary variable may be unnecessary:
Code:
$ cat file
A B C
D E F
G H I
K L M
$ echo $(<file)
A B C D E F G H I K L M
 
Old 11-30-2007, 03:02 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,282

Rep: Reputation: 172Reputation: 172
if it's not too large
xargs < file will do it too,

of course you need to be careful if the file has meta-characters in it
 
  


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
how to pass command-line parameter to shell script? Kropotkin Linux - Newbie 12 07-25-2011 09:24 AM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 10:10 AM
How to read a single line from a text file into a shell script. SkipHuffman Linux - Software 2 08-16-2006 02:10 PM
How to execute a command line in a shell script orgazmo Programming 5 05-09-2005 09:16 AM
Shell Script to read 500files from the command line saravanan1979 Programming 1 09-22-2004 09:44 AM


All times are GMT -5. The time now is 06:52 PM.

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