Linux shell utils: convert a list of hexadecimals to a list of decimals

How can I convert a file with a lot of hex numbers into decimal?

Example: file1

 0x59999
 0x5acdc
 0xffeff

I want to start

$ cat file1 | util | cat >file2

and get file2 with something like:

 1021489
 1249230
 3458080

(The numbers in example output are random, as I can't convert such long hex numbers to decimal)

UPDATE: I solved this with the following Perl command:

perl -pe '$_=hex;$_.="\n"'

Is there a better solution? The real task is needing to be able to sort hex numbers.


Solution 1:

John T's answer is the way to go for hex conversions, but you can also do them this way (which can be used for other bases as well):

$ hexval=0x59999
$ hexval=${hexval#*x}
$ echo $((16#$hexval))
367001

Demonstration:

$ echo $((2#1011010))
90
$ echo $((8#1776))
1022
$ echo $((23#mmmmm))
6436342

Edit:

#!/bin/bash
base=16
while read -r val
do
    val=${val#*x}
    echo $(($base#$val))
done < inputfile > outputfile

The only advantage over John T's answer is that this one can be easily adapted to convert other bases to decimal. His is quite a bit faster.

This gawk command seems to be a little faster than John's shell version:

gawk --non-decimal-data '{printf "%d\n",$1}' inputfile > outputfile

It's about the same speed as your Perl command. Why not just use it?

By the way, the last part of your Perl one-liner can be replaced by the -l option:

perl -lpe '$_=hex'

Another note: Typically the pipeline you show in your question would be written as:

util < file1 > file2

or, if cat represents placeholders, then dummy names should be used:

prog1 < file1 | util | prog2 > file2

then you won't have people complaining about useless uses of cat.

Solution 2:

I normally just use printf...

while read x;do printf '%d\n' $x;done <file