sed
only sees one line at a time, so searching for consecutive line-feeds will not work. One way is to push all the data into the hold space, and then do the replace when all input has been gathered, e.g.:
sed -r -n ' $! { # When it is not the last line 1 { h } # Replace hold space with first line 1! { H } # Otherwise append to hold space } $ { # When last line reached H # Append it to hold space g # Replace pattern space with hold space s/\n//g # Remove all occurrences of \n p # Print pattern space } ' input.txt
Here is a simpler option using perl
, but works in the same manner:
perl -e ' $/ = undef; # Slurp mode, read whole file $_ = <>; # Read file to $_ s/\n//g; # Remove all occurrences of \n print; # Output $_ ' input.txt
Edit
A shorter perl
version suggested by Peter O.:
perl -0777 -pe 's/\n//g' input.txt
-0777
enables slurp mode.-p
implicitly prints result.