Find Positions of a Hexadecimal Sequence in a File
This page answers questions like these:
- How to find the locations that a hexadecimal sequence appears in a file?
- How to find the locations that a hexadecimal pattern appears in a binary file?
- How to find the locations in a file containing a given hexadecimal sequence?
- How to find the locations in a file containing a given hexadecimal pattern?
- How to find where a hexadecimal string appears in a binary file?
- How to find where a hexadecimal pattern appears in a binary file?
- How to find a hexadecimal sequence in a file?
- How to find a binary sequence in a file?
- Does a hex string appear within a file?
- Does a hex pattern appear within a file?
- How to grep hex?
Related Links:
Count Occurrences 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
Find and/or Replace a Tilde in Excel
Find: Path Must Precede Expression
Detect Whether A File Contains A Hexadecimal String:
grep -P '\xNN\xNN\xNN...' FILE
- In some versions of Unix/Linux, you can use Perl regular expressions to grep hex strings.
- e.g. “grep -P '\x48\x65\x6c\x6c\x6f' FILE” will grep for "Hello".
- Pros: Faster than the methods presented below.
- Cons: For binary files, it only tells you whether the file contains the string or not. (For text files, it shows you the matching lines just like normal grep.)
Find Positions of a Hexadecimal Sequence in a File:
od -v -t x1 FILE |
sed 's/[^ ]* *//' |
tr '\012' ' ' |
grep -b -i -o HEXSTRING |
sed 's/:.*/\/3/' |
bc
- Find the positions of hexadecimal string HEXSTRING in file FILE. Positions are listed in order and on separate lines. Position zero is the first character of the 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: Very 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 '\012' ' ' |
sed 's/HEXSTRING/FOUND\n&/g' |
tr ' ' '\012' |
grep -n FOUND |
sed 's/\([^:]*\):.*/v=\1;d=d+1;v-d;/' |
bc
- Find the positions of lowercase hexadecimal string HEXSTRING in file FILE. Positions are listed in order and on separate lines. Position zero is the first character of the 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: Extremely slow. Doesn’t handle overlapping hex strings or patterns.
Related Links:
Count Occurrences 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
Find and/or Replace a Tilde in Excel
Find: Path Must Precede Expression
Home > Linux / Unix > Find Positions of a Hexadecimal Sequence in a File
Tags: grep hex, hex grep, find hex in file, byte sequence, hexadecimal sequence, hex sequence, hexadecimal string, hex string, hexadecimal pattern, hex pattern, binary pattern, file, text file, binary file, linux, unix, solaris, bsd, aix
Copyright © HelpDoco.com
file-find-hexadecimal.txt
Linux-Unix/find-positions-of-hexadecimal-sequence-in-file.htm
2