Reputation: 1314
I am trying to write a perl one-liner which takes two hex-like strings (string that are look like hexadecimal, but without the leading 0x) and performs the arithmetic expression on the two hexadecimal strings.
For example. Lets say I am reading the data from /proc/PID/maps. The first column containing an address to memory in hexadecimal(without the leading 0x)
bff45000-bff66000
Is an example of the address.
The perl one-liner that I am using is:
perl -le 'print" "x5,"Hex\t\tDecimal\n","-"x15," "x5,"-"x15;foreach $a(@ARGV){$b=eval($a);printf"0x%-10x %17d\n",$b,$b}'
The problem I am having is that perl throws an error like this
Bareword found where operator expected at (eval 1) line 1, near...
This is because Perl can perform arithmetic operations on hexadecimal strings in the form of "0x... + 0x..." and not ".... + ....."
May someone please point me in the right direction as to how I may solve my problem please?
Upvotes: 2
Views: 374
Reputation: 118595
Do you want the output in hex, too?
echo deadbeef-c00cc00c | perl -lne 's/[[:xdigit:]]+/hex($&)/g;printf "%x",eval'
Upvotes: 0
Reputation: 35198
Using a perl oneliner, give the numbers the 0x
prefix and then just use eval
:
echo "bff45000-bff66000" | perl -lne 's/([[:xdigit:]]+)/0x$1/g; print eval'
Outputs:
-135168
Upvotes: 2