I can't be 100% sure since you haven't shown us what you're doing, but I am guessing you're simply not quoting your variables. For example, given this file:
$ cat file
foo|bar|baz|
foo|bar|baz|
foo|bar|baz|
foo|bar|baz|
foo|bar|baz|
foo|bar|baz|
foo|bar|baz|
If you run your command on it and save the output in a variable, the newlines will be saved. However, whether they are printed or not depends on how you print the variable:
$ var=$(sed 's/.$//' file | sort)
$ echo $var ## bad
foo|bar|baz foo|bar|baz foo|bar|baz foo|bar|baz foo|bar|baz foo|bar|baz foo|bar|baz
$ echo "$var" ## good
foo|bar|baz
foo|bar|baz
foo|bar|baz
foo|bar|baz
foo|bar|baz
foo|bar|baz
foo|bar|baz
When you use an unquoted variable, it invokes the split+glob operator which will split the variable on characters in the $IFS
variable. By default, that's space, newline and tab:
$ printf '%s' "$IFS" | od -c
0000000 \t \n
0000003
Quoting the variable protects it from split+glob which is one of the reasons why you should always quote your variables. For more details on why, see Security implications of forgetting to quote a variable in bash/POSIX shells.
v=$(sed | sort)
, we'll need to see it! – Toby Speight Apr 06 '16 at 07:50