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

Example: file1


I want to start

$ cat file1 | util | cat >file2

and get file2 with something like:


(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.

  • Neither of the cat commands in your hypothetical pipeline should be necessary. – Dennis Williamson Dec 28 '10 at 2:04
  • @Dennis Williamson, yes, they are. But I want to show, that this command is used in the pipe/ – osgx Dec 28 '10 at 9:29
up vote 7 down vote accepted

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))


$ echo $((2#1011010))
$ echo $((8#1776))
$ echo $((23#mmmmm))


while read -r val
    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.

  • it is hard to use with 100MB file of hex values - one value per line. – osgx Dec 27 '10 at 21:35

I normally just use printf...

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

  • this will be done with bash builtins, w/o forks? – osgx Dec 27 '10 at 21:34
cat file1 | wcalc -q -d -EE -P0 > file2

wcalc should be installed first

  • but perl is installed on many. – osgx Dec 27 '10 at 18:28
  • $ wcalc bash: wcalc: command not found – osgx Dec 27 '10 at 18:29
  • osgx: if you wanted Perl solutions you should have asked on stackoverflow. This is not a programming website. – John T Dec 27 '10 at 19:05
  • Little bit of UUOC, too :p – John T Dec 27 '10 at 20:31
  • I want a shell solution. AWK, SED can be used. PERL one-liner is in my question already )) – osgx Dec 27 '10 at 21:36

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.