help with organizing some non regular text

hey im trying to get the hex diffrences in two files ones called new and the other is named old i want it to phrase into my script,
heres how i need the info:

input='\x'94 #the new 1 byte hex change
offset=00000000 #the 1st offset of the difference 
patch 
unset input offset
input='\x'34 #the 2nd new 1 byte hex change
offset=00000001#the 2nd offset of the difference 
patch 
unset input offset
input='\x'03 #the 3rd new 1 byte hex change
offset=0000004C #the 3rd offset of the difference 
patch 
unset input offset

and so one till all the diffrences have been put into the script

can anybody help me?

thanks

You can do something like that :

$ cat lewis.ksh
cmp -l lewis_1.txt lewis_2.txt |
awk -v Q="'" '
function oct2dec(o      ,i,d){
  for(i=1;i<=length(o);++i){
    d=(8*d)+substr(o,i,1);
  }
  return d
}
{
    offset = $1;
    old    = $2;
    new    = $3;
    printf "input=" Q "\\x" Q "%x" "\n", oct2dec(new);
    printf "offset=%08X\n", offset;
    print  "patch\nunset input offset"
}
'
$ cat lewis_1.txt
abcdefghij
klmnopqrst
$ cat lewis_2.txt
aBcdEFghij
klMnopqrst
$ cmp -l lewis_1.txt lewis_2.txt
     2 142 102
     5 145 105
     6 146 106
    14 155 115
$ ./lewis.ksh
input='\x'42
offset=00000002
patch
unset input offset
input='\x'45
offset=00000005
patch
unset input offset
input='\x'46
offset=00000006
patch
unset input offset
input='\x'4d
offset=0000000E
patch
unset input offset
$

Note: My version of awk doesn't support octal and hexa specifications for numbers.

Jean-Pierre.

1 Like

Thanks heaps mandose exactly what i needed :slight_smile: your a freaking pro!!!

hey iv noticed a problem, the offset is allways one too many eg if the change is at offset 00000054 it will say 00000055

cmp -l lewis_1.txt lewis_2.txt |
awk -v Q="'" '
function oct2dec(o      ,i,d){
  for(i=1;i<=length(o);++i){
    d=(8*d)+substr(o,i,1);
  }
  return d
}
{
    offset = $1 - 1;
    old    = $2;
    new    = $3;
    printf "input=" Q "\\x" Q "%x" "\n", oct2dec(new);
    printf "offset=%08X\n", offset;
    print  "patch\nunset input offset"
}
'

Jean-Pierre.

1 Like

thanks mate thats great :slight_smile: