Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3348014
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:29:26+00:00 2026-05-18T01:29:26+00:00

Say I have generated the following binary file: # generate file: python -c ‘import

  • 0

Say I have generated the following binary file:

# generate file:
python -c 'import sys;[sys.stdout.write(chr(i)) for i in (0,0,0,0,2,4,6,8,0,1,3,0,5,20)]' > mydata.bin

# get file size in bytes
stat -c '%s' mydata.bin

# 14

And say, I want to find the locations of all zeroes (0x00), using a grep-like syntax.

 

The best I can do so far is:

$ hexdump -v -e "1/1 \" %02x\n\"" mydata.bin | grep -n '00'

1: 00
2: 00
3: 00
4: 00
9: 00
12: 00

However, this implicitly converts each byte in the original binary file into a multi-byte ASCII representation, on which grep operates; not exactly the prime example of optimization 🙂

Is there something like a binary grep for Linux? Possibly, also, something that would support a regular expression-like syntax, but also for byte “characters” – that is, I could write something like ‘a(\x00*)b‘ and match ‘zero or more’ occurrences of byte 0 between bytes ‘a’ (97) and ‘b’ (98)?

EDIT: The context is that I’m working on a driver, where I capture 8-bit data; something goes wrong in the data, which can be kilobytes up to megabytes, and I’d like to check for particular signatures and where they occur. (so far, I’m working with kilobyte snippets, so optimization is not that important – but if I start getting some errors in megabyte long captures, and I need to analyze those, my guess is I would like something more optimized 🙂 . And especially, I’d like something where I can “grep” for a byte as a character – hexdump forces me to search strings per byte)

EDIT2: same question, different forum 🙂 grepping through a binary file for a sequence of bytes

EDIT3: Thanks to the answer by @tchrist, here is also an example with ‘grepping’ and matching, and displaying results (although not quite the same question as OP):

$ perl -ln0777e 'print unpack("H*",$1), "\n", pos() while /(.....\0\0\0\xCC\0\0\0.....)/g' /path/to/myfile.bin

ca000000cb000000cc000000cd000000ce     # Matched data (hex)
66357                                  # Offset (dec)

To have the matched data be grouped as one byte (two hex characters) each, then “H2 H2 H2 …” needs to be specified for as many bytes are there in the matched string; as my match ‘.....\0\0\0\xCC\0\0\0.....‘ covers 17 bytes, I can write ‘"H2"x17‘ in Perl. Each of these “H2” will return a separate variable (as in a list), so join also needs to be used to add spaces between them – eventually:

$ perl -ln0777e 'print join(" ", unpack("H2 "x17,$1)), "\n", pos() while /(.....\0\0\0\xCC\0\0\0.....)/g' /path/to/myfile.bin

ca 00 00 00 cb 00 00 00 cc 00 00 00 cd 00 00 00 ce
66357

Well.. indeed Perl is very nice ‘binary grepping’ facility, I must admit 🙂 As long as one learns the syntax properly 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:29:27+00:00Added an answer on May 18, 2026 at 1:29 am

    One-Liner Input

    Here’s the shorter one-liner version:

    % perl -ln0e 'print tell' < inputfile
    

    And here’s a slightly longer one-liner:

    % perl -e '($/,$\) = ("\0","\n"); print tell while <STDIN>' < inputfile
    

    The way to connect those two one-liners is by uncompiling the first one’s program:

    % perl -MO=Deparse,-p -ln0e 'print tell'
    BEGIN { $/ = "\000"; $\ = "\n"; }
    LINE: while (defined(($_ = <ARGV>))) {
        chomp($_);
        print(tell);
    }
    

    Programmed Input

    If you want to put that in a file instead of a calling it from the command line, here’s a somewhat more explicit version:

    #!/usr/bin/env perl
    
    use English qw[ -no_match_vars ];
    
    $RS  = "\0";    # input  separator for readline, chomp
    $ORS = "\n";    # output separator for print
    
    while (<STDIN>) {
        print tell();
    }
    

    And here’s the really long version:

    #!/usr/bin/env perl
    
    use strict;
    use autodie;  # for perl5.10 or better
    use warnings qw[ FATAL all  ];
    
    use IO::Handle;
    
    IO::Handle->input_record_separator("\0");
    IO::Handle->output_record_separator("\n");
    
    binmode(STDIN);   # just in case
    
    while (my $null_terminated = readline(STDIN)) {
        # this just *past* the null we just read:
        my $seek_offset = tell(STDIN);
        print STDOUT $seek_offset;  
    
    }
    
    close(STDIN);
    close(STDOUT);
    

    One-Liner Output

    BTW, to create the test input file, I didn’t use your big, long Python script; I just used this simple Perl one-liner:

    % perl -e 'print 0.0.0.0.2.4.6.8.0.1.3.0.5.20' > inputfile
    

    You’ll find that Perl often winds up being 2-3 times shorter than Python to do the same job. And you don’t have to compromise on clarity; what could be simpler that the one-liner above?

    Programmed Output

    I know, I know. If you don’t already know the language, this might be clearer:

    #!/usr/bin/env perl
    @values = (
        0,  0,  0,  0,  2,
        4,  6,  8,  0,  1,
        3,  0,  5, 20,
    );
    print pack("C*", @values);
    

    although this works, too:

    print chr for @values;
    

    as does

    print map { chr } @values;
    

    Although for those who like everything all rigorous and careful and all, this might be more what you would see:

    #!/usr/bin/env perl
    
    use strict;
    use warnings qw[ FATAL all ];
    use autodie;
    
    binmode(STDOUT);
    
    my @octet_list = (
        0,  0,  0,  0,  2,
        4,  6,  8,  0,  1,
        3,  0,  5, 20,
    );
    
    my $binary = pack("C*", @octet_list);
    print STDOUT $binary;
    
    close(STDOUT); 
    

    TMTOWTDI

    Perl supports more than one way to do things so that you can pick the one that you’re most comfortable with. If this were something I planned to check in as school or work project, I would certainly select the longer, more careful versions — or at least put a comment in the shell script if I were using the one-liners.

    You can find documentation for Perl on your own system. Just type

    % man perl
    % man perlrun
    % man perlvar
    % man perlfunc
    

    etc at your shell prompt. If you want pretty-ish versions on the web instead, get the manpages for perl, perlrun, perlvar, and perlfunc from http://perldoc.perl.org.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a binary file (generated with Java) containing a 32 bit int
Say I have the following file structure: app/ app.py controllers/ __init__.py project.py plugin.py If
Please help me to generate the following query. Say I have customer table and
Let's say I have a web site for hosting community generated content that targets
Say we have the following method: private MyObject foo = new MyObject(); // and
Say I have the following models: class Image(models.Model): image = models.ImageField(max_length=200, upload_to=file_home) content_type =
Let's say I have the following data in the Customers table: (nothing more) ID
Let's say I have a page that refers to a .js file. In that
Say, I have the following test: [Test] public void MyTest( [RandomNumbers( Count=100, Minimum=0, Maximum=1000
Let's say, that I have the following source output: <p>This is first paragraph</p> <p>This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.