I want to process with a bash script a text file that looks like this:
ADD $05 $05 $05
SUBI $06 $06 00011
MUL $07 $07 $07
JSR 011101
taking the binary numbers (that are always longer than 4 bits) and converting them into their decimal representation.
For the previous example, this is the file I want to end up with:
ADD $05 $05 $05
SUBI $06 $06 3
MUL $07 $07 $07
JSR 29
I have been exploring tr
and sed
, but I think they don't let me work with the matched pattern (to convert it) before the replacement. What approach can I take?
EDIT: with the suggestion of @DopeGothi, and given that I have at most one binary number per line, I can create a temporary file with all the decimal versions of the binary numbers. The issue is that now I need to intercalate them:
Every time I find a binary number in the first file, I replace with the corresponding number in the file with decimals.
| grep -oE '[01]+' | awk '{print "ibase=2;obase=A;"$1}' | bc
. Bear in mind that this has no way of distinguishing whether10
is decimal, binary, octal, hexadecimal, or any other base.