Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

2 Answers

up vote 5 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:

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

awk:

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. – Bean May 30 '12 at 1:14

Your Answer

 
discard

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.