Operating System - HP-UX
1751963 Members
4963 Online
108783 Solutions
New Discussion юеВ

script help: awk input string parsing

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

script help: awk input string parsing

Hi,

got this script from here:

RTN=`echo $1 | awk '
{
stmp="\""
stmp=sprintf("%s%s%sx%s",stmp,"\\\\","\\\\","81")
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%sxF7\",%d",stmp,"\\\\","\\\\",olenoftmp+1)
printf stmp
}
'`

echo RTN


That produces this o/p:

./file 1234567

"\x81\x10\x32\x54\x76\xF7",8

but I need it in fact to do this:

"\x81\x10\x32\x54\x76\xF7",6

where the last 6 is the number of \x 's

and
the 81 is because there is an odd number of input digits.

If the number of digits is even

"\x10\x21\x43\x65\x87\xFF",6


any help appreciated..
Thanks,
Bill

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

Re: script help: awk input string parsing

Halfway there..

RTN=`echo $1 | awk '
{
stmp="\""
stmp=sprintf("%s%s%sx%s",stmp,"\\\\","\\\\","81")
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))
numb=sprintf("%s",i)
}
stmp = sprintf("%s%s%sxF7\",%d",stmp,"\\\\","\\\\",i+1)
printf stmp
}
'`

will produce (12345)
"\x81\x21\x43\x65\xF7",5

and (123456)
"\x81\x21\x43\x65\x87\xF7",6

however, in the first case,
the F7 should be
"\x81\x21\x43\xF5",4

and in the second
"\x10\x21\x43\x65\xFF",5

ie.. the F7 and first field are wrong.. but at least the last digit is right!

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

Re: script help: awk input string parsing

This looks okay for the moment!:

God, I hates awk!

(Does it really mean awkward?)

RET = `echo $1 | awk '
{
stmp="\""
tmp=$1
olenoftmp=length(tmp)
if ( ( olenoftmp % 2 ) != 0 ) {
stmp = sprintf("%s%s%sx81",stmp,"\\\\","\\\\")
} else {
stmp = sprintf("%s%s%sx10",stmp,"\\\\","\\\\")
}
if ( ( olenoftmp % 2 ) != 0 ) {tmp = tmp F }
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))
numb=sprintf("%s",i)
}
if ( ( olenoftmp % 2 ) != 0 ) {
stmp = sprintf("%sF\", %d",stmp,i)
} else {
stmp = sprintf("%s%sxFF\", %d",stmp,"\\\\",i+1)
}


printf stmp
}
'`
It works for me (tm)
harry d brown jr
Honored Contributor
Solution

Re: script help: awk input string parsing

RTN=`echo $1 | awk '
{
stmp="\""
##stmp=sprintf("%s%s%sx%s",stmp,"\\\\","\\\\","81")
tmp=$1
olenoftmp=length(tmp)
if ( ( olenoftmp % 2 ) != 0 ) {tmp = "18" tmp "F"} else {tmp = "01" 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))
}
if ( ( olenoftmp % 2 ) == 0 ) {stmp = sprintf("%s%s%sxFF",stmp,"\\\\","\\\\")}
stmp = sprintf("%s\",%d",stmp,olenoftmp)
printf stmp
}
'`


live free or die
harry
Live Free or Die
Robin Wakefield
Honored Contributor

Re: script help: awk input string parsing

Hi Bill,

Well, how about this:

RTN=`echo $1 | awk '
{
stmp="\""
numx=0
tmp=$1
olenoftmp=length(tmp)
if ( ( olenoftmp % 2 ) != 0 ) {
tmp = "180" tmp "7F";
} else {
tmp = "01" tmp "FF";
}
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))
numx++
}
stmp = sprintf("%s\",%d",stmp,numx)

printf stmp
}
' `
echo $RTN


Notice, I do all the prep work up front, and let the reversing take care of the rest.

Rgds, Robin
Bill McNAMARA_1
Honored Contributor

Re: script help: awk input string parsing

Thanks for that guys...

Is there any way to add a Shell variable amongst all that awk stuff...

ie, bang in a $HELLO in the middle of the awk .. where the HELLO was defined outside the awk... I feel a lot of `'\"$'s coming along ;)


Later,
Bill

It works for me (tm)
Volker Borowski
Honored Contributor

Re: script help: awk input string parsing

Hi Bill,

sure there is ( from "man awk" ):

ARGC The number of elements in the ARGV array.

ARGV An array of command line arguments, excluding options and the program argument numbered from zero to ARGC-1.

ENVIRON Array of environment variables; subscripts arenames. For example, if environment variable V=thing, ENVIRON["V"] produces thing.

So I guess you are looking for that ENVIRON array.

Hope this helps
Volker
James R. Ferguson
Acclaimed Contributor

Re: script help: awk input string parsing

Hi Bill:

Do you mean this (?):

# echo "hello"|awk -v WHO=bill '{print $0,WHO}'

...or, alternately:

# echo "hello"|awk '{print $0,WHO}' WHO=bill

...of course, the literal string "bill" could be a $VARIABLE from the shell...

Regards!

...JRF...
harry d brown jr
Honored Contributor

Re: script help: awk input string parsing

Bill,

Maybe it's time to cross the road and learn "perl"??

#!/usr/local/bin/perl
#
$stmp = "\"";
$tmp = $ARGV[0];
$olenoftmp = length($tmp);
$Ptmp=$tmp;
if ( ( $olenoftmp % 2) == 0)
{ $tmp = '01' . $Ptmp . 'FF'; }
else
{ $tmp = '18' . $Ptmp . 'F'; }
$lenoftmp = length($tmp) / 2;
for ($i = 0; $i < $lenoftmp ; $i++) {
$Ptmp=$stmp;
$stmp = sprintf('%s\\x%s%s', $Ptmp,
substr($tmp,($i * 2) + 1, 1), substr($tmp, ($i * 2), 1));
}
$stmp .= "\"," . $olenoftmp;
printf("%s\n",$stmp);



live free or die
harry
Live Free or Die