From your sample output, it seems that the output doesn't have a pipe at the end; you have ,
marking the end of each field, and |
separating pairs of fields. For that specification, this works with tr
and sed
:
$ x="123,
> 456
> 789,
> 012"
$ echo "$x" | tr -s '\r\n' ',' | sed 's/,\(.\)/,|\1/g'
123,|456,|789,|012,
$
The tr
command replaces newline and carriage return with comma, squeezing (-s
) duplicates. The sed
command looks for a comma followed by another character and replaces it with ,|
.