Using grep to find a binary pattern in a file Using grep to find a binary pattern in a file unix unix

Using grep to find a binary pattern in a file


The results may depend on you current locale. To avoid this, use:

env LANG=LC_ALL grep -P "<binary pattern>" <file>

where env LANG=LC_ALL overrides your current locale to allow byte matching. Otherwise, patterns with non-ASCII "characters" such as \xff will not match.

For example, this fails to match because (at least in my case) the environment has LANG=en_US.UTF-8:

$ printf '\x41\xfe\n' | grep -P '\xfe'

when this succeeds:

$ printf '\x41\xfe\n' | env LANG=LC_ALL grep -P '\xfe'A?