Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a file containing a a large number of lines, each of which contains a bunch of numbers which are separated by spaces. I process this data in a pipe in some way, and then I want to collapse the multiple lines into a single line of all the numbers separated by spaces.

Is there a standard command-line utility I can use to do this? It seems like most line-by-line utilities won't mess with the newlines...

share|improve this question
up vote 10 down vote accepted

That's why you don't use line-by-line utilities for this.

$ tr '\n' ' ' < input.txt > output.txt
share|improve this answer

Even line-by-line utilities can remove all newlines.


sed ':a;N;$!ba;s/\n/ /g' file


awk '{printf $0" "}' file

But it's much better to use tr, like @Ignacio Vazquez-Abrams wrote.

share|improve this answer
I knew it could be done with sed, but you're right, tr is better. Yet another two-letter unix utility to add to my belt. – JeremyKun May 30 '12 at 1:14

pipe the output to xargs

#!/bin/sh -x
date > temp
ping cp1za >> temp
cat temp | xargs echo
Tue Dec 17 18:23:31 UTC 2013 CS V1R12: Pinging host CP1ZA.TMMNA.TMM.TOYOTA.C ( Ping #1 response took 0.001 seconds.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.