With sed this requires to read the whole input into a buffer and afterwards replace all newlines by |
, like this:
sed ':a;N;$!ba;s/\n/ | /g' input.txt
Part 1 - buffering input
:a
defines a label called 'a'
N
gets the next line from input and appends it to the pattern buffer
$!ba
jumps to a
unless the end of input is reached
Part 2 - replacing newlines by |
s/\n/|/
execute the substitute command on the pattern buffern
As you can see, this is very inefficient since it requires to:
- read the complete input into memory
- operate three times on the input: 1. reading, 2. substituting, 3. printing
Therefore I would suggest to use awk which can do it in one loop:
awk 'NR==1{printf $0;next}{printf " | "$0}END{print ""}' input.txt