I have the file ~/dummy_hex.txt
containing hex and random string:
\x12\xA1\xF1\xE3somegibberigh
I want to count how many hex values (groups of \x^hex_digit^^hex_digit^) the string above has. In the example above I want the commands to run to return the number 4
.
In other words I want to type over my terminal:
command ^file_having hex^
And return the value 4
So far I tried to do that with:
sed 's/[^\x[0-9A-Fa-f][0-9A-Fa-f]]//g' dummy_hex.txt | awk '{ print length }'
But somehow seems to return wrong result because of regex misstype. Can you tell me how to use full PCRE compartible regex with sed in order to do that?
Alternatively I want to count how many hex values my string contains.
Edit 1
An another approach is to count the \x
string occurences but that may count any stray \x
that may not be followed with a value indicating a hexadimal string.
sed 's/[^\x]//g' dummy_hex.txt | awk '{ print length }'
Further more I tried to do that with -r
option that enables PCRE:
sed -r 's/^\\x[0-9A-Fa-f][0-9A-Fa-f]]/g' dummy_hex.txt | awk '{ print length }'
But I get the error:
sed: -e expression #1, char 31: unterminated `s' command
wc -c
says 30 bytes.