Operating System - HP-UX
1834394 Members
1532 Online
110066 Solutions
New Discussion

simple (ish) script, seperate and reparse input string

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

simple (ish) script, seperate and reparse input string


Hi,

I'm calling a script as follows:

script 012345678

and I need to take the 012345678
and make it look like this:
in a variable in the script:

"\x00\x21\x43\x65\x87\xFF", 8

where the last 8 is the number of digits input

shold work if the number input is: 0123456789 as:
"\x10\x32\x54\x76\x98\xFF", 9

thanks,
Later,
Bill
It works for me (tm)
8 REPLIES 8
Robin Wakefield
Honored Contributor

Re: simple (ish) script, seperate and reparse input string

Hi Bill,

Try:

#!/bin/ksh

echo $1 | awk '{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("\xFF\", %d\n",l-1)
}'

The number of digits is 1 less than the *actual* number entered, by the looks of it, i.e. 0123456789 is 10 digits, not 9.

Rgds, Robin
Bill McNAMARA_1
Honored Contributor

Re: simple (ish) script, seperate and reparse input string

Wow great!
yea it shouldn't be the l-1, that was my error..

"\x10\x32\x54\x76\x98??", 9

However, I'm getting a strange character appearing just before the closing "

Finally for the 10,
it prints out fine to screen, but how can I set it to a shell variable to work further on it (for a sed)

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

Re: simple (ish) script, seperate and reparse input string

Bill,

Shouldn't be the length of 012345678 be 9??

awk '
{
stmp=""
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 "len of %s is %d\n",stmp,length(stmp)
}'

Don't try changing the "\x" "FF" to "\xFF", other wise awk will make if an FF.

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

Re: simple (ish) script, seperate and reparse input string

Hi Bill,

a=`script 012345678`

should do it. I don't get any funny characters when I run it.

Can you pipe the output through | od -c and see what they are.

Rgds, Robin.
harry d brown jr
Honored Contributor

Re: simple (ish) script, seperate and reparse input string

read hello
hithere=`echo $hello |
awk '
{
stmp=""
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
}
'`
echo $hithere
Live Free or Die
harry d brown jr
Honored Contributor
Solution

Re: simple (ish) script, seperate and reparse input string

Bill,

the funny character, probably a "y" with a sideways ":" over it is caused by awk interpeting the "\xFF" as a hex FF, that's why I had to seperate the "\x" and the "FF" into two seperate strings.

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

Re: simple (ish) script, seperate and reparse input string

yea that's what I was seeing Harry..
It works for me (tm)
Robin Wakefield
Honored Contributor

Re: simple (ish) script, seperate and reparse input string

Hi Bill,

I was running an old version of awk - the later version was exhibiting your strange chars.

Rgds, Robin.