Hex to decimal - Execute command for the matching part

Alo

I have this input:

0x10=some text
0x20=some text
0x30=some text

and want this output:

16=some text
32=some text
48=some text

I want to use a command to convert from hex to decimal i.e.:

 $ echo $((0x15a))

or

 $ printf '%d\n' 0x15a

I try to use something like this:

sed 's/^.\{4\}/'"`echo $((&))`"'/' inputfile

but get this error:
&: syntax error: operand expected (error token is "&")

How can I execute a command for the matching part?

Thx in advance

sed 's/0x//' input | { IFS="=" ; while read a b
do
      n=`echo "obase=10;ibase=16;$a"|bc`
      echo "$n=$b"
done }
$ cat input
0x10=some text
0x20=some text 
0x30=some text
$ cat mts
sed 's/0x//' input | { IFS="=" ; while read a b
do
      n=`echo "obase=10;ibase=16;$a"|bc`
      echo "$n=$b"
done }
$ ksh mts
16=some text
32=some text
48=some text
$

I don't think you can embed statements inside a regex like that. sed doesn't evaluate shell statements. And you don't need sed at all anyway, you already know the shell can translate...

How about:

while IFS="=" read HEX TEXT
do
        echo "$((${HEX}))=${TEXT}"
done < input > output

Thx for both answers

I can execute this:

 sed 's/^.\{4\}/'"`date`"'/' inputfile

and get this result:

Wed Nov 17 22:21:40 CET 2010=some text
Wed Nov 17 22:21:40 CET 2010=some text
Wed Nov 17 22:21:40 CET 2010=some text

The problem is that I can't pass the matching part to the shell statement

No need to call an external utility such as sed. It can all be very easily done within the shell.

while IFS="=" read hex rest
do
    printf "%d=%s\n" hex rest
done < infile

Corona is correct. sed doesn't evaluate shell statements.

 sed 's/^.\{4\}/'"`date`"'/' inputfile

It is the shell that expands the above to this expression before calling the sed utility

sed 's/^.\{4\}/'"Wed Nov 17 22:21:40 CET 2010"'/' inputfile

Or you could use Perl -

$
$
$ cat f40
0x10=some text
0x20=some text
0x30=some text
$
$
$ perl -ne '/^(.*?)(=.*)$/ && printf("%s%s\n",hex($1),$2)' f40
16=some text
32=some text
48=some text
$
$

tyler_durden