Thread: programming with grep

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    programming with grep

    Hello everyone,


    I am programming with grep command in my C/C++ application to steal its string manipulation functions. :-)

    I have completed several simple cases. I am meeting issues with two cases, and I am wondering how to represent it in grep.

    1. find the lines contain "foo" or "goo";
    2. find the lines contain "foo" but no "goo".

    could anyone help please?


    thanks in advance,
    George

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I am programming with grep command in my C/C++ application to steal its string manipulation functions. :-)
    Ugh!

    Try using the regexp library which presents the same functionality as grep but without all the expensive overhead of spawning a separate process every time you want to do something.

    > 1. find the lines contain "foo" or "goo";
    egrep '[fg]oo'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thank you Salem!


    Quote Originally Posted by Salem
    > 1. find the lines contain "foo" or "goo";
    egrep '[fg]oo'
    Your method does not work in this case. How to find the string contains either "abc" or "def"? Any general ways to implement or logics?


    regards,
    George

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    egrep 'read|the|manual' file.txt
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Cool Salem, thank you!


    Quote Originally Posted by Salem
    Code:
    egrep 'read|the|manual' file.txt

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple grep problem
    By cdave in forum Windows Programming
    Replies: 7
    Last Post: 12-29-2005, 05:48 PM
  2. Grep Last Pattern Output
    By cfriend in forum Linux Programming
    Replies: 1
    Last Post: 09-18-2004, 10:14 AM
  3. Grep
    By cfriend in forum Linux Programming
    Replies: 3
    Last Post: 09-17-2004, 05:34 AM
  4. Grep
    By Adam in forum C Programming
    Replies: 0
    Last Post: 06-07-2002, 06:06 AM
  5. grep
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-26-2002, 12:11 PM