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"
OR
sed -r "s/[\r\n]+/\x20/g" test.txt
The test.txt file is returned unchanged.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
example:
tr -s [\r\n] "@" | sed "s/^[[:space:]@]+//g" | sed "s/[[:space:]]@[[:space:]]/@/g" | sed "s/@@+/@/g" | ../bin/sed "s/@$//g" | sed "s/@/\"\x20\"/g"
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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"
OR
sed -r "s/[\r\n]+/\x20/g" test.txt
The test.txt file is returned unchanged.
Thanks GnuWin32,
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.
example:
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