how to extract version number form binary file using awk
I had tried to extract version number from a binary file.
The version number is after this string 'VeRsIoN_StRiNg'. But how to find it using awk and print the next character I can't find out.Someone the开发者_如何学Cr can help?
/Lasse
Do you strictly need to use awk? This seems like a better usecase for grep --binary-files=text -o 'VeRsIoN_StRiNg.' file | grep -o '.$'
.
I'm not entirely sure how well a stream editor like awk will actually work with a binary file. If this is part of a larger awk script, you probably want to call the above grep formula from awk.
You can use strings command to find the printable strings in a object, or other binary, file
strings /path/to/binary | grep -o 'VeRsIoN_StRiNg.' | grep -o '.$'
why not awk ?
gawk -b/mawk/mawk2 'BEGIN { RS = "^$"; FS = "^.*VeRsIoN_StRiNg"
} END { print substr($2,1,1)' # mawk/mawk2 or gawk in byte mode.
# LC_ALL=C gawk -e will be here too
even in gawk unicode mode, this workaround will do
gawk -e 'BEGIN { RS = "^$"; FS = "^.*VeRsIoN_StRiNg"
} END { printf("%.1s\n", $2) }' # gawk in unicode mode
This is to take advantage of the fact a "precision" N specified (e.g. %.ns) for %s means
at most N items printed
But since, by definition of FS
, we know the first byte of $2
is already your version number, a single character integer, then this printf will circumvent any gawk error message of complaining trying to do sub-strings on UTF8 non-compliant data.
精彩评论