Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

search and replace the last character but counrting in hex

SOLVED
Go to Solution
Highlighted
Super Advisor Super Advisor

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
6 REPLIES
Honored Contributor Honored Contributor

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
Frequent Advisor

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.
Super Advisor Super Advisor

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
Acclaimed Contributor Acclaimed Contributor

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...
Super Advisor Super Advisor

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

thanks again and thanks James !

:)
hello
Acclaimed Contributor Acclaimed Contributor

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