- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Multi-line output to single line
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2008 09:49 PM
02-24-2008 09:49 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2008 11:51 PM
- Tags:
- fmt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2008 12:39 AM
02-25-2008 12:39 AM
Re: Multi-line output to single line
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
- Tags:
- tr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2008 01:41 AM
02-25-2008 01:41 AM
Re: Multi-line output to single line
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2008 01:42 AM
02-25-2008 01:42 AM
Re: Multi-line output to single line
All on one line:
awk -v x="" '{ s=s sprintf(x "%s" x " ", $0) } END { sub(",$", "", s); print(s) }' file.txt
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2008 01:57 AM
02-25-2008 01:57 AM
Re: Multi-line output to single line
tr '\012' ' ' < sdisks1.txt
datadg01 datadg02 datadg03 datadg04 datadg05 root@server:>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2008 02:16 AM
02-25-2008 02:16 AM
Re: Multi-line output to single line
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2008 04:13 AM
02-25-2008 04:13 AM
Re: Multi-line output to single line
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...
- Tags:
- evil cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2008 01:14 PM - edited 09-17-2011 01:13 PM
02-25-2008 01:14 PM - edited 09-17-2011 01:13 PM
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2008 01:30 PM
02-25-2008 01:30 PM
Re: Multi-line output to single line
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...