LinuxQuestions.org
Visit Jeremy's Blog.
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: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,788

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
"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,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
You probably should be using 'cat' instead of 'more'.
 
Old 11-30-2007, 12:55 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,342

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

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,788

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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

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

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