Re: search and replace the last character but counrting in hex (214 Views)
Reply
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 1 of 7 (214 Views)
Accepted Solution

search and replace the last character but counrting in hex

I have an interesting problem where I need to change a block of numbers in the last field and replace however the numbers are in hex ie:

my file looks like this:

map dev 16D3 to dir 10B:0 target=0 lun=267;
map dev 16D3 to dir 07B:1 target=0 lun=267;
map dev 16D3 to dir 04A:0 target=0 lun=267;
map dev 16D3 to dir 13A:1 target=0 lun=267;

map dev 16CB to dir 10B:0 target=0 lun=268;
map dev 16CB to dir 07B:1 target=0 lun=268;
map dev 16CB to dir 04A:0 target=0 lun=268;
map dev 16CB to dir 13A:1 target=0 lun=268;

map dev 16C3 to dir 10B:0 target=0 lun=269;
map dev 16C3 to dir 07B:1 target=0 lun=269;
map dev 16C3 to dir 04A:0 target=0 lun=269;
map dev 16C3 to dir 13A:1 target=0 lun=269;

map dev 0572 to dir 10B:0 target=0 lun=26A;
map dev 0572 to dir 07B:1 target=0 lun=26A;
map dev 0572 to dir 04A:0 target=0 lun=26A;
map dev 0572 to dir 13A:1 target=0 lun=26A;

with many more entries and I need to replace the lun= with a different hex entry.

ie starting with the stanza "map dev 16D3" I'd like to replace 267 with 2D3 then the next stanza entries 268 with 2D4

is it possible to count and report in hex?

any ideas are greatly appreciated :)

Thanks

Chris
hello
Please use plain text.
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 2 of 7 (214 Views)

Re: search and replace the last character but counrting in hex

printf will convert dec to hex - here's a blurb from the printf man page:

"..unsigned hexadecimal notation using (x and X)..."

for example:
printf "%5x" $DEC_NUM
##############

There is 'dc' - do a man dc
h1 = "echo 16i " toupper($1) " pq | dc"


###############

There is awk

awk '{print ("0x"$1)
Support Fatherhood - Stop Family Law
Please use plain text.
Frequent Advisor
N,Vipin
Posts: 39
Registered: ‎06-07-2010
Message 3 of 7 (214 Views)

Re: search and replace the last character but counrting in hex

I can not see any relation which can impliment through script. If entire file is same format, i mean block of four line separated by blank line, then there is a possibility of writing code. Again we should think of the logic to generate the numbers to replace.
Please use plain text.
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 4 of 7 (214 Views)

Re: search and replace the last character but counrting in hex

Thanks Michael,

I am also sure there is a pearl version but I also found an awk and can incorporate this with the "sub" routine within awk

Chris
hello
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 7 (214 Views)

Re: search and replace the last character but counrting in hex

Hi Chris:

Yes, you can do arithmetic in hexadecimal :-)

If I understand correctly, this is what you want:

# cat ./myconverter
#!/usr/bin/perl
use strict;
use warnings;
my ($old, $new) = ( "267", "2D3" );
my $diff = hex($new) - hex($old);
while (<>) {
s/(lun=)([0-9a-fA-F]+);$/sprintf("%s%X;",$1,hex($2)+$diff)/xe and print;
}
1;

...run as:

# ./myconverter file

Regards!

...JRF...
Please use plain text.
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 6 of 7 (214 Views)

Re: search and replace the last character but counrting in hex

thanks again and thanks James !

:)
hello
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,877
Registered: ‎03-06-2006
Message 7 of 7 (214 Views)

Re: search and replace the last character but counrting in hex

Or assuming the lun numbers are sequential after the first block:
typeset -i10 lun=16#2d3
awk -F= -v start=$lun <BEGIN { replace = 0; last="" }
/map dev 16D3/ { replace = 1 }
length($0) == 0 && replace { ++start } # gap
{
if ($0 ~ /map dev/ && replace) {
printf "%s=%s=%X;\n",$1,$2, start
} else
print $0
} ' file
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation