grep for ascii nul

Victor Sudakov vas at
Sat Nov 2 06:45:14 UTC 2019

Ronald F. Guilmette wrote:
> In message <20191101092716.GA67658 at>, 
> Victor Sudakov <vas at> wrote:
> >I need to find files containing ascii null inside, and print their names to
> > stdout.
> Unfortunately, you're banging up against a long-standing a rather
> annoying non-feature of fgrep/grep/egrep, which is that unlike the
> tr command, the grep family of commands does not support the \DDD
> notation for specifying arbitrary byte values.  Thus, you cannot use
> then to search for arbitrary byte values.
> I would thus suggest that you solve your problem using a Perl or C
> program.  

Perl is not in the base system, so that is not quite the answer. 
I'm a big fan of awk, awk is in the base system and should be able to do
it, right?

$ hd trees.txt 
00000000  66 69 72 0a 6f 61 6b 0a  63 65 64 00 61 72 0a 62  ||
00000010  69 72 63 68 0a 70 61 6c  6d 0a                    |irch.palm.|

Note the ascii null embedded in the word "cedar"

$ awk '/\x66\x69/{print $0}' trees.txt

So far so good. But with the ascii nul it behaves in an unexpected way:

$ awk '/\x00/{print $0}' trees.txt

Victor Sudakov,  VAS4-RIPE, VAS47-RIPN
2:5005/49 at fidonet
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: not available
URL: <>

More information about the freebsd-questions mailing list