LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script/command for converting columns/table onto a single line (https://www.linuxquestions.org/questions/programming-9/shell-script-command-for-converting-columns-table-onto-a-single-line-603373/)

skuz_ball 11-29-2007 08:03 PM

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.

jlliagre 11-29-2007 08:20 PM

"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


skuz_ball 11-29-2007 08:34 PM

that is fu@King brilliant.

thank you very much

ghostdog74 11-29-2007 09:23 PM

Quote:

Originally Posted by skuz_ball (Post 2975071)
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

skuz_ball 11-29-2007 10:23 PM

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.

gnashley 11-30-2007 12:54 AM

You probably should be using 'cat' instead of 'more'.

chrism01 11-30-2007 12:55 AM

file=$(<yourfile)

jschiwal 11-30-2007 01:20 AM

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

jlliagre 11-30-2007 02:54 AM

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


bigearsbilly 11-30-2007 03:02 AM

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


All times are GMT -5. The time now is 05:13 PM.