• GNU grep and files with nulls

    From Kenny McCormack@21:1/5 to All on Sat Apr 12 15:03:13 2025
    So, I have a file that contains a line like:

    foo.bar.bletch.one.two

    where the dots represent null (\0) characters.

    I want to be able to grep this line, using this command:

    grep foo.bar.bletch.one.two file

    where the dots represent actual dots (regexp wildcards).
    (I do it this way because I don't know how to represent a null character in
    a grep RE; using dots instead gets you a "good enough for government work" result)

    Interestingly, in grep version 2.20, this worked as expected, but in grep version 3.3, it fails. I found this out when the above code stopped
    working, when the code was moved to a newer Linux system (which had the new version of grep installed).

    This is all out of curiosity, of course. I know this is a grey area in
    terms of standards and stuff like that, but it seems odd that they would actually removing working functionality. The man page says little on the subject; it does mention nulls in a few places, but it seems not relevant
    to this issue.

    Anyway, I switched the code to use GNU AWK (Gawk), which explicitly
    supports nulls in strings, so no worries there. The Gawk solution is shorter and cleaner anyway.

    --
    "They say if you play a Microsoft CD backwards, you hear satanic messages.
    Thats nothing, cause if you play it forwards, it installs Windows."

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to John McCue on Sat Apr 12 18:03:40 2025
    On 2025-04-12, John McCue <jmccue@magnetar.jmcunx.com> wrote:
    Kenny McCormack <gazelle@shell.xmission.com> wrote:
    So, I have a file that contains a line like:

    foo.bar.bletch.one.two

    where the dots represent null (\0) characters.

    I want to be able to grep this line, using this command:

    grep foo.bar.bletch.one.two file


    Did you try using argument '--binary-files=' ?
    Detail in the GNU grep man page :)

    I just remember the -a shortcut, which is the same
    as --binary-files=text. All the other --binary-files
    options are about /not/ grepping through binary files,
    so that useful one gets the one letter shortcut.

    We can probably mnemonize it as "all": "grep
    through all the darned files, including binaries".

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From John McCue@21:1/5 to Kenny McCormack on Sat Apr 12 17:45:03 2025
    Kenny McCormack <gazelle@shell.xmission.com> wrote:
    So, I have a file that contains a line like:

    foo.bar.bletch.one.two

    where the dots represent null (\0) characters.

    I want to be able to grep this line, using this command:

    grep foo.bar.bletch.one.two file


    Did you try using argument '--binary-files=' ?
    Detail in the GNU grep man page :)

    <snip>

    --
    [t]csh(1) - "An elegant shell, for a more... civilized age."
    - Paraphrasing Star Wars

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)