I want to strip carriage-returns and line-feeds from a text file and replace them with a space using sed.
Neither of the following sed scripts appear to work...
cat test.txt | sed -r "s/[\r\n]+/\x20/g"
sed -r "s/[\r\n]+/\x20/g" test.txt
The test.txt file is returned unchanged.
Your suggestion to use "tr" worked and eventually allowed me to find a solution for my underlying problem.
What was the solution?
I used "tr" to replace all CR LF's with a neutral character, (eg "@"). I could then pipe the result into sed and manipulate it further.
tr -s [\r\n] "@" | sed "s/^[[:space:]@]+//g" | sed "s/[[:space:]]@[[:space:]]/@/g" | sed "s/@@+/@/g" | ../bin/sed "s/@$//g" | sed "s/@/\"\x20\"/g"
This cannot be done with sed, not even with the -B option for binary reading and writing. Instead, use tr (from coreutils):
tr -s [\r\n] " " <test.txt