Ubuntu – How to print multiline output on the same line

command linescriptstext processing

Is there a method to print multi-line output (single output) on the same line?

For example, if the output is:

abc
def
qwerty

Is it possible to print:

abcdefqwerty 

Best Answer

You can remove all occurrences of characters from a given set with tr -d. To remove the newline character use:

tr -d '\n'

As always you can use input and output redirection and pipes to read from or write to files and other processes.

If you want to keep the last newline you can simply add it back with echo or printf '\n', e. g.:

cat file1 file2... | { tr -d '\n'; echo; } > output.txt