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

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.


I normally just use printf...

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