Operating System - Linux
1828537 Members
2579 Online
109980 Solutions
New Discussion

Re: Multi-line output to single line

 
SOLVED
Go to solution
Patrick Ware_1
Super Advisor

Multi-line output to single line

Hello,

How can I take the following multi-lined output:

outputa
outputb
outputc


and turn it into single line ouput, with a single space between each field like below:

outputa outputb outputc
9 REPLIES 9
Khairy
Esteemed Contributor
Solution

Re: Multi-line output to single line

hi patrick,

try this. This format the output to a 10000 columns width line.

# | fmt -10000
OR
# command > file.txt
# cat file.txt | fmt -10000
outputa outputb outputc outputd

Please assign some points if this answer yr questions...

Thanx!
Peter Nikitka
Honored Contributor

Re: Multi-line output to single line

Hi,

if there is no need for the special handling, that fmt does, use
tr '\012' ' '
- the last entry is single_quote-space-single-quote
- read form stdin, writes to stdout

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Patrick Ware_1
Super Advisor

Re: Multi-line output to single line

Ok, so how does this work on a file?
Patrick Ware_1
Super Advisor

Re: Multi-line output to single line

I also ended up getting the following solution:

All on one line:

awk -v x="" '{ s=s sprintf(x "%s" x " ", $0) } END { sub(",$", "", s); print(s) }' file.txt
Patrick Ware_1
Super Advisor

Re: Multi-line output to single line

I tried the tr command, and got the following:

tr '\012' ' ' < sdisks1.txt
datadg01 datadg02 datadg03 datadg04 datadg05 root@server:>
Peter Nikitka
Honored Contributor

Re: Multi-line output to single line

Hi,

when you need the data in another file:
tr '\012' ' ' < sdisks1.txt >sdisk1.out

If you need a trailing newline:
(tr '\012' ' ' < sdisks1.txt; print) >sdisk1.out

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: Multi-line output to single line

Hi Patrick:

Be aware that the 'fmt' command counts not only the string lengths but also the column separators in limiting the line width it composes. That may not be a problem in your case.

The use of 'cat' to read the input file and pipe it to 'fmt' runs a superflous process. Simply do:

# fmt -10000 file.txt

Peter's use of 'tr' shows the same economy of resources,

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Multi-line output to single line

Did you only have 3 lines? If so, you use vi and join them. You could also use sed(1).

If you have sets of 3 lines and you want to combine all 3, then you can use awk:
awk '
{
getline l2
getline l3
print $0, l2, l3 }' file

If you have N lines and you want to to put them all together, you can use tr (and Peter's echo).

James R. Ferguson
Acclaimed Contributor

Re: Multi-line output to single line

Hi (again):

When Dennis wrote:

-> print $0, $l2, $l3 }

...he meant:

-> print $0, l2, l3 }

...without the sigil.

I also suspect that for Peter's solution will be the fastest (and most generalized) with the smallest footprint, although there are (usually) many ways to do something :-)

Regards!

...JRF...