Operating System - HP-UX
1748111 Members
3686 Online
108758 Solutions
New Discussion юеВ

search and replace the last character but counrting in hex

 
SOLVED
Go to solution
lawrenzo_1
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 6
Michael Steele_2
Honored Contributor
Solution

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
N,Vipin
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.
lawrenzo_1
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
James R. Ferguson
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...
lawrenzo_1
Super Advisor

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

thanks again and thanks James !

:)
hello
Dennis Handly
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