Replacing hex characters '\x0D' with '\x0D\x0A'

Hello All,

I have a requirement where I need to replaced the hex character - '\x0D' with 2 hex characters - 'x0D' & 'x0A'

I am trying to use SED -

But somehow its not working. Any pointers?

Also the hex character '\x0D' can occur anywhere in the line.

Can this also be accomplished through 'tr'

You can use Perl to fix this:

$
$ # create a file that has \x0D characters at multiple places
$ perl -le 'print "\x0D The quick \x0D brown fox jumps \x0D over the lazy dog.\x0D"' >f0
$
$ # check the file contents
$ cat -vET f0
^M The quick ^M brown fox jumps ^M over the lazy dog.^M$
$
$ # do an inline replace of all \x0D characters to \x0D\x0A
$
$ perl -i.bak -pe 's/(\x0D)/$1\x0A/g' f0
$
$ # now check the file contents again
$ cat -vET f0
^M$
 The quick ^M$
 brown fox jumps ^M$
 over the lazy dog.^M$
$
$

tyler_durden

you forget to escape \ with \.

sed 's/\\x0D/\\x0D \\x0A/g'

:D:D:D:D

Doesn't that remove the special meaning of "\x" which states that what follows is a hex code ?

$
$ printf "abc\x0Ddef" | od -bc
0000000 141 142 143 015 144 145 146
          a   b   c  \r   d   e   f
0000007
$
$ # sed
$ printf "abc\x0Ddef" | sed 's/\\x0D/\\x0D\\x0A/g' | od -bc
0000000 141 142 143 015 144 145 146
          a   b   c  \r   d   e   f
0000007
$
$ # perl
$ printf "abc\x0Ddef" | perl -ne 's/\x0D/\x0D\x0A/g; print' | od -bc
0000000 141 142 143 015 012 144 145 146
          a   b   c  \r  \n   d   e   f
0000010
$

tyler_durden

Thanks 'durden_tyler' your code worked...but just to mention that it didn't work on Windows...

Same was the case with my 'sed' command I had mentioned at the start. It was not giving the desired results on Windoze but when I executed on Linux it worked like a charm...

I think its due to the fact that interpreters like 'sed', 'Perl' ignores 'CR' (chariage return) characters on windoze..........

Don't know about "sed" on Windows, but that would be highly unusual for Perl on Windows:

C:\>
C:\>REM create a file that has \x0D characters at multiple places
C:\>perl -le "print \"\x0D The quick \x0D brown fox jumps \x0D over the lazy dog.\x0D\"" >f0
C:\>
C:\>REM check the file contents
C:\>vis <f0
\015 The quick \015 brown fox jumps \015 over the lazy dog.\015
C:\>
C:\>REM do an inline replace of all \x0D characters to \x0D\x0A
C:\>perl -i.bak -pe "s/(\x0D)/$1\x0A/g" f0
C:\>
C:\>REM now check the file contents again
C:\>vis <f0
\015
 The quick \015
 brown fox jumps \015
 over the lazy dog.\015

C:\>
C:\>REM print file using the DOS "type" command
C:\>type f0
 The quick
 brown fox jumps
 over the lazy dog.

C:\>
C:\>

The perl interpreter used in the example above is from the ActiveState Perl installed using MSI.

"vis" is just a small C program that makes non-printable characters visible in octal code.

tyler_durden

paragkalra,

Without using Perl or C, may be this is what you were looking for,
a pure sed solution, on a typical Unix environment:

  sed 's/\\r/\\r\\n/g' &lt;infile &gt;outfile

At least it works flawlessly on my RH/Fedora... I've tested it using
an input file, as shown by "od -c", like:

0000000 f i r s t l i n e \r s e c o n
0000020 d l i n e \r t h i r d l i n
0000040 e \r
0000042

and having the output file generated like this:

0000000 f i r s t l i n e \r \n s e c o
0000020 n d l i n e \r \n t h i r d l
0000040 i n e \r \n
0000045

Please not that \r and \n are the standard escape sequences
(used by both "od" and "sed") for 0x0D and 0x0A - presuming
we live in an ASCII world, of course. This may not apply if
you are dealing with systems built upon different codings...

Helpful?

Mario.