Is there some way to convert strings like 1K to 1000, 1M to 1000000 etc. with a single Bash command? I'd like to avoid being the one million and thirty-first guy to create a >10-line or >100-character one-liner hack for this. Something like iso2int 5MB.
Edit: units -t '5MB' 'bytes works, but that tool is not available. Are there any simple ways to convert either 5M or 5MB and similar to bytes?
max's solution was elegant, but the shortest equivalent which would work in my case is at the very least sed -e 's/b//i;s/k/*1000/i;s/m/*1000000/i;s/g/*1000000000/i;s/t/*1000000000000/i' | bc.
Something like this?
$ echo "1K + 10M" | sed -e "s/K/*1024/g;s/M/*1024*1024/" | bc
10486784
Edit:
sed -e 's/t/kg/i;s/g/km/i;s/m/kk/i;s/k/*1000/ig;s/b//i' | bc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With