How can I print multiline output on the same line?

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 

Solution 1:

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

Solution 2:

Many ways. To illustrate, I have saved your example in file:

$ cat file | tr -d '\n'
abcdefqwerty$ 
$ cat file | perl -pe 's/\n//'
abcdefqwerty$ 

That removes all newline characters, including the last one though. You might instead want to do:

$ printf "%s\n" "$(cat file | perl -pe 's/\n//')"
abcdefqwerty
$ printf "%s\n" "$(cat file | tr -d '\n')"
abcdefqwerty