grep for ascii nul
Dale Scott
dalescott at shaw.ca
Sun Nov 3 17:23:07 UTC 2019
There are ports for many GNU utilities, and BSD has a long history of staying pure to its roots (IMHO), so why not just install gnuawk and symlink awk to gnuawk?
Cheers,
Dale
----- Original Message -----
> From: "Victor Sudakov" <vas at sibptus.ru>
> To: "freebsd-questions" <freebsd-questions at freebsd.org>
> Sent: Sunday, November 3, 2019 9:47:56 AM
> Subject: Re: grep for ascii nul
> Kurt Hackenberg 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.
>
> You know what, lang/gawk does not have this problem:
>
> $ gawk '/\x00/{print $0}' trees.txt
> cedar
> $
>
> This is not the first time BSD AWK sucks in comparison with GNU AWK :-(
>
> Do you think this is worth a PR?
>
> --
> Victor Sudakov, VAS4-RIPE, VAS47-RIPN
> 2:5005/49 at fidonet http://vas.tomsk.ru/
More information about the freebsd-questions
mailing list