sed Command not working in AIX UNIX

Hi

1st problem
--------------
i have this sed command in my unix script which replaces new line and carriage return in a line with the string "&#xA"
the script works fine in Linux 3.0.101-0.5, but not in AIX 1 7 , the "s/\r/\&#xA/g" replacement, replaces
all the character "r" in the file.

sed "s/^M/\&#xA/g;s/\r/\&#xA\/g;s/\r^M/\&#xA/g"  < filename.txt > filename 2.txt

2nd problem
--------------
and then i have a awk command that works on the file filename2.txt (output of the above command), below is the awk command i'm using..

awk -v W="$1" '$0 ~ W{ORS="";if(NR!=1)print "\n";}1' filename2.txt > filename3.txt

instead of writing the sed output to a file can i send the output to the awk command, so that it doesn't have to take too much time writing and reading to two files.

any help is appreciated

Thanks
Mj

---------- Post updated 04-22-14 at 08:06 AM ---------- Previous update was 04-21-14 at 06:17 PM ----------

Hi All
Any response would be appreciated....

thanks
MJ

Looks like your shell and/or sed version don't take \r to represent the carriage return character. I think you need to resort to other representations, e.g <ctrl>v<ctrl>m or $'\r' , or even a variable assigned from the output of a printf command.
For your second problem, why don't you just pipe sed 's output to awk ?

You could use awk or perl to catch the \r etc. by it's octal value, which is 15 or \015 in this case.

An example:

$ od -c infile
0000000    s   t   a   r   t  \r  \n   s   t   o   p  \n
0000014
$ awk '{gsub(/\015/,"\&\#xA"); print}' infile > outfile
$ od -c outfile
0000000    s   t   a   r   t   &   #   x   A  \n   s   t   o   p  \n
0000017

There should be plenty of ascii tables with the octal values on the net.
Maybe you can do it all together with awk instead of sed. I assume there is a way with sed, but I don't know it.

thanks rudic, i replaced /r with contorl M