Count the number of occurrences of hexadecimal string HEXSTRING in file FILE.
N.B. HEXSTRING must have each byte’s hex separated by one space, e.g. "Hello" would be represented by "48 65 6c 6c 6f" or "48 65 6C 6C 6F".
Pros: Handles both text and binary files. Handles any byte value from 00 to ff. Handles both uppercase and lowercase hexadecimal. Can use regular expression matching instead of a plain hex string.
Cons: Slow. Some systems don’t have the -o option to grep. Doesn’t handle overlapping hex strings or patterns.
od -v -t x1 FILE | sed 's/[^ ]*//' | tr -d '\012' | grep HEXSTRING | sed 's/HEXSTRING/&\n/g' | grep -c HEXSTRING
Count the number of occurrences of lowercase hexadecimal string HEXSTRING in file FILE.
N.B. HEXSTRING must have each byte’s hex separated by one space, e.g. "Hello" would be represented by "48 65 6c 6c 6f".
Pros: Handles both text and binary files. Handles any byte value from 00 to ff. Can use regular expression matching instead of a plain hex string. Doesn’t rely on the -o option to grep.
Cons: Very slow. Doesn’t handle overlapping hex strings or patterns.