Count Occurrences of a Hexadecimal Sequence in a File
This page answers questions like these:
- How to count the number of locations in a file containing a hexadecimal sequence?
- How to count the number of locations in a file containing a binary sequence?
- How to count the number of locations in a file containing a hexadecimal pattern?
- How to count the number of locations in a file containing a binary pattern?
- How to count the number of times a hexadecimal sequence appears in a file?
- How to count the number of times a binary sequence appears in a file?
- How to count the number of times a hexadecimal pattern appears in a file?
- How to count the number of times a binary pattern appears in a file?
- How to count the number of times a hexadecimal sequence appears in a binary file?
- How to count the number of times a hexadecimal pattern appears in a binary file?
- Does a hex string appear within a file?
- Does a hex pattern appear within a file?
Related Links:
Find Positions of a Hexadecimal Sequence in a File
Count Occurrences of a String in a File
Output the Lines Between Two Matching Lines
Output Lines of a File in Reverse Order
Replace Text in a File using the Command Line
Join Lines of Text File Together
Count Occurrences of a Hexadecimal Sequence in a File:
od -v -t x1 FILE | sed 's/[^ ]*//' | tr -d '\012' | grep -i -o HEXSTRING | wc -l
- 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.
Related Links:
Find Positions of a Hexadecimal Sequence in a File
Count Occurrences of a String in a File
Output the Lines Between Two Matching Lines
Output Lines of a File in Reverse Order
Replace Text in a File using the Command Line
Join Lines of Text File Together
Home > Linux / Unix > Count Occurrences of a Hexadecimal Sequence in a File
Tags: count, byte sequence, hexadecimal sequence, hex sequence, hexadecimal string, hex string, hexadecimal pattern, hex pattern, file, text file, binary file, linux, unix, solaris, bsd, aix
Copyright © HelpDoco.com
file-count-hexadecimal.txt
Linux-Unix/count-occurrences-of-hexadecimal-sequence-in-file.htm
1