Thread: C program that does UNIX strings | grep

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    2

    C program that does UNIX strings | grep

    Hi,

    I am not a programmer, C-wise. Mainly a shell scripter.

    I am hoping that maybe someone from this FORUM know of any C program that I can use that can work like UNIX strings | grep "pattern"

    At the moment, on a binary file, I am using UNIX strings and then grep for a pattern and print out that line and does some awk -F":" thing. This is manageable for when the binary file is 50M but not for where files are 100M and above. I mean it still works but takes longer since strings work on the entire file.

    Basically the information that I am trying to extract from the binary file is some type of header that is contained in a file. I am thinking of maybe writing a 'simple' C program that will read the first few bytes of the binary file and then print the line that contains the string that I am looking for. I don't believe the header is at the end of the file but more in the beginning. I am wanting to extract the header information to be able to rename or make a copy of the binary in a more user-friendly name.

    Any good reference/link to a sample program on reading a binary file maybe. Main reason why I am wanting a C program is because the binary file can be on Windows or *nix.

    Any advice much appreciated. Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by newbie01.linux
    I am hoping that maybe someone from this FORUM know of any C program that I can use that can work like UNIX strings | grep "pattern"

    At the moment, on a binary file, I am using UNIX strings and then grep for a pattern and print out that line and does some awk -F":" thing. This is manageable for when the binary file is 50M but not for where files are 100M and above. I mean it still works but takes longer since strings work on the entire file.

    Basically the information that I am trying to extract from the binary file is some type of header that is contained in a file. I am thinking of maybe writing a 'simple' C program that will read the first few bytes of the binary file and then print the line that contains the string that I am looking for. I don't believe the header is at the end of the file but more in the beginning. I am wanting to extract the header information to be able to rename or make a copy of the binary in a more user-friendly name.
    Maybe all you need to do is to avoid having grep read the entire file. Looking at my manual page for ack-grep, I see:
    Code:
    -m=NUM, --max-count=NUM
        Stop reading a file after NUM matches.
    If indeed the header is at the beginning of the file, I would hazard a guess that this would tremendously speedup the grep.

    If you really do want to write a C program to do this, then a regex library like PCRE would be useful. But then you need to know how to program in C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You can also use the head utility to limit scope of files. e.g. if you only want to consider the first megabyte of a big file

    Code:
    head -c1024K bigfile | strings | grep foo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Awk, grep from C program
    By astrogal in forum C Programming
    Replies: 2
    Last Post: 09-30-2011, 10:18 AM
  2. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 10:54 AM
  3. Call a remote C# program from a C program on unix
    By goup in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 07:46 PM
  4. writing a grep program
    By cnovice in forum C Programming
    Replies: 8
    Last Post: 11-22-2005, 08:05 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM