SED won't replace my CR LF's with a space :(

Help
2007-01-03
2012-07-26
  • Keith Willis
    Keith Willis
    2007-01-03

    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.

     
    • Keith Willis
      Keith Willis
      2007-01-07

      Thanks GnuWin32,

      Your suggestion to use "tr" worked and eventually allowed me to find a solution for my underlying problem.

       
      • rmccullough
        rmccullough
        2007-01-08

        What was the solution?

         
        • Keith Willis
          Keith Willis
          2007-01-15

          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"

           
    • GnuWin
      GnuWin
      2007-01-07

      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