grep for ascii nul

Kurt Hackenberg kh at panix.com
Sat Nov 2 18:57:29 UTC 2019


On 2019-11-02 02:45, Victor Sudakov wrote:

> 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  |fir.oak.ced.ar.b|
> 00000010  69 72 63 68 0a 70 61 6c  6d 0a                    |irch.palm.|
> 0000001a
> $
> 
> Note the ascii null embedded in the word "cedar"
> 
> $ awk '/\x66\x69/{print $0}' trees.txt
> fir
> 
> So far so good. But with the ascii nul it behaves in an unexpected way:
> 
> $ awk '/\x00/{print $0}' trees.txt
> fir
> oak
> ced
> birch
> palm
> $

Looks like it has the same problem that I guess grep does: it takes that 
NUL as the end of a C string, so the regexp becomes a null string (zero 
length), which matches everything.


More information about the freebsd-questions mailing list