How to wrap two line into single line with delimiter

Your best bet is to use printf. You have two strings and you want to output them with some additional formatting. That's exactly what printf does.

$ printf "%s @@ %s\n" "$(cat /proc/loadavg)" "$(date)"

Your tr attempt does not work since tr modifies characters, not words. You could use it to replace the newlines with one single character though:

$ ( cat /proc/loadavg; date ) | tr '\n' '@'

... but it doesn't do quite what you need.

Your sed attempt does not work since the newline is stripped from the input to sed (i.e. sed -n '/\n/p' inputfile would never print anything).

You could still do it with sed if you read the second line (from date) with the N editing command while editing the first line (which will place a newline between them):

$ ( cat /proc/loadavg; date ) | sed 'N;s/\n/ @@ /'

... but I would personally prefer the printf solution.


You can do this:

echo `cat /proc/loadavg` @@ `date`

Like this:

( cat /proc/loadavg && date ) | sed 'N; s/\n/ @@ /'

First, your attempts don't work because the pipe | applies only to date, not no both command. To work around that you need to run cat ... && date in a subshell, and then redirect the subshell's stdout.

Then tr '\n' ' @@ ' doesn't work because you can't replace a character with multiple characters.

And sed 's/\n/ @@ /g' doesn't work because sed only gets to see lines one at a time. To get it to see newlines you need to merge both lines of input in the same buffer. Which is what N does above.