Operating System - HP-UX
1748089 Members
5011 Online
108758 Solutions
New Discussion юеВ

sed problem with \ ( calling Robin/Harry extra...)

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

sed problem with \ ( calling Robin/Harry extra...)

hi I got this in a shell variable CALLNUM

\x10\x32\x54\xFF,5

and in the following sed the \'s are removed.. do I have to modify the script to add the \\?
sed "s:calledtochange:'$CALLNUM':g" /tmp/4 > /tmp/5

I need the script modified to have

a " placed before and after the \x09\x89\75

ie
"\x10\x32\x54\xFF",5



CALLNUM=`echo $1 | awk '
{
stmp="\\x" "01"
tmp=$1
olenoftmp=length(tmp)
if ( ( olenoftmp % 2 ) != 0 ) {tmp = 0 tmp}
lenoftmp=length(tmp) / 2
for (i = 1; i < lenoftmp+1 ; i++) {
stmp = stmp "\\x" substr(tmp,(i*2),1) substr(tmp,(i*2)-1,1)
}
stmp = stmp "\\x" "FF," olenoftmp
printf stmp
}
'`
It works for me (tm)
7 REPLIES 7
Bill McNAMARA_1
Honored Contributor

Re: sed problem with \ ( calling Robin/Harry extra...)

okay forget the above, the " is working:

CALLNUM=`echo $1 | awk '
{
stmp="\"\x" "01"
tmp=$1
olenoftmp=length(tmp)
if ( ( olenoftmp % 2 ) != 0 ) {tmp = 0 tmp}
lenoftmp=length(tmp) / 2
for (i = 1; i < lenoftmp+1 ; i++) {
stmp = stmp "\x" substr(tmp,(i*2),1) substr(tmp,(i*2)-1,1)
}
stmp = stmp "\x" "FF\"," olenoftmp
printf stmp
}
'`

but the sed is not:
sed "s:calledtochange:$CALLNUM:g" /tmp/4 > /tmp/5

where the final output is
"x01x10x32x54xFF",5

rather than:
"\x01\x10\x32\x54\xFF",5

An echo in the script shows the variable is fine, just a problem with the sed and the \'s

Bill
It works for me (tm)
Robin Wakefield
Honored Contributor

Re: sed problem with \ ( calling Robin/Harry extra...)

Hi Bill,

Going back to my (corrected) script:

#!/bin/ksh

echo $1 | nawk '{n=$0;l=length($0)
if (l%2 == 1) {n=0 $0}
printf("\"")
for (i=1;i<=l;i=i+2)
{
s1=substr(n,i,1)
s2=substr(n,i+1,1)
printf ("\\\\x" "%s%s",s2,s1)
}
printf("\\\\x" "FF\", %d\n",l-1)
}'


CALLNUM=`script 012`
echo $CALLNUM
"\x00\x21\xFF", 2
echo abc | sed "s+abc+$CALLNUM+g"
"\x00\x21\xFF", 2


In other words, 4 instead of 2 backslashes are required. Are you seeing the above behaviour?

Rgds, Robin.
Bill McNAMARA_1
Honored Contributor

Re: sed problem with \ ( calling Robin/Harry extra...)

everything is okay as far as the echo and awk goes..
but the sed is messing up..
no matter how many /'s I put in the awk script the sed doesn't put substitute them at all....

Bill
It works for me (tm)
Bill McNAMARA_1
Honored Contributor

Re: sed problem with \ ( calling Robin/Harry extra...)

just to add, yea, what you've shown there works the same for me.

Bill
It works for me (tm)
harry d brown jr
Honored Contributor
Solution

Re: sed problem with \ ( calling Robin/Harry extra...)

Bill,

try these changes:

hello=012345678

CALLNUM=`echo $hello | awk '
{
stmp=""
stmp=sprintf("%s%s%sx%s",stmp,"\\\\","\\\\","01")
tmp=$1
olenoftmp=length(tmp)
if ( ( olenoftmp % 2 ) != 0 ) {tmp = 0 tmp}
lenoftmp=length(tmp) / 2
for (i = 1; i < lenoftmp+1 ; i++) {
stmp = sprintf("%s%s%sx%s%s",stmp,"\\\\","\\\\",substr(tmp,(i*2),1),substr(tmp,(
i*2)-1,1))
}
stmp = sprintf("%s%s%sxFF,%d",stmp,"\\\\","\\\\",olenoftmp)
printf stmp
}
'`
echo $CALLNUM
sed "s:calledtochange:$CALLNUM:g" /tmp/4


note the quad slashes and a change to sprintf.


live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: sed problem with \ ( calling Robin/Harry extra...)

Bill,

Have you given the changes a try yet?

live free or die
harry
Live Free or Die
Bill McNAMARA_1
Honored Contributor

Re: sed problem with \ ( calling Robin/Harry extra...)

for Robin..

pereal:root> what /usr/bin/awk
/usr/bin/awk:
$Revision: 82.2 $

The / was still missing after the sed..

Harry's script works..

Later,
Bill
It works for me (tm)