(How) can I delete all newlines (\ n) with sed?

I tried to delete all the lines of a new line in a channel like this:

(echo foo; echo bar) | sed -e :a -e N -e '$!ba' -e 's/\n/ /g' | hexdump -C

What results compress debian into:

00000000  66 6f 6f 20 62 61 72 0a                           |foo bar.|
00000008

Do not delete the ending newline.

tr -d '\n'as in How to delete lines from a text file? works just fine, but not sed.

+5
source share
6 answers

Sorry, this cannot be done using sed, see http://sed.sourceforge.net/sedfaq5.html#s5.10 and discussion here: http://objectmix.com/awk/26812-sed-remove-last -new-line-2.html

It looks like sed will add back \ n if it is present as the last character.

+6
source

\n, , , awk.

printf "%s" `(echo foo; echo bar) | sed -e :a -e N -e '$!ba' -e 's/\n/ /g'`

.

+1
{ echo foo; echo bar; } | awk '{printf("%s ", $0)}' 
0

:   (echo foo; -) | sed -e: a -e N -e '$! ba' -e 's/\n//g' | hexdump -C   (echo foo; -) | tr -d '\n' | hexdump -C   echo foo; -) | hexdump -C | sed -e: a -e N -e '$! ba' -e 's/\n//g'

0

foo bar , ,

(echo foo; echo bar)

echo

(echo -n foo; echo -n bar)

. , sed, , .

0

:

(echo foo; echo bar) | sed 's/\n//' | xargs echo -n
-1

All Articles