system call in c to grep

Discussion in 'Mac Programming' started by farmerdoug, Feb 26, 2010.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    grep "09/10/2009,2345" /Users/doug/data/data7.txt
    This works from the command line but the exact same command in a system call

    system( "grep \"09/10/2009,2345\" /Users/doug/data/data7.txt");

    doesn't.

    Can this be done?
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Explain exactly how it doesn't work. Be specific.
     
  3. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #3
    Just as it would fail from the command line.

    Usage: grep [OPTION]... PATTERN [FILE]...
    Try `grep --help' for more information.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
  5. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #5
    Just a thought: try it without the quotes. Quotes are there to prevent the shell from interpreting certain characters itself, but you're not using a shell, so you may very well not need the quotes.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    The system() function uses a shell. Read the man page.
     
  7. larkost macrumors 6502a

    Joined:
    Oct 13, 2007
    #7
    The better question is why you are using grep when there are already regular expression functions available directly within c. As a first blush answer there is the regex standard c library that should do about what you want. There are a lot of other options if that does not work for you: a number of open source ones, Apple NSScanner can do some things, and OmniGroup's regex parser is very fast.
     
  8. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #8
    This code is proprietary, unfortunately. I'll try to clean it up but now that I know its supposed to work, I'll keep trying.

    thanks
     
  9. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #9
    Compiles and fails as before with same error message


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (int argc, const char * argv[]) {
        char *grep;
        
        grep = (char*)calloc(150, sizeof(char));
            
                
            strcpy(grep, "grep \"");
            strcat(grep, "09/10/2009");
            strcat(grep, ",1552\" ");
            strcat(grep, "/Users/doug/andy/");
            strcat(grep,"tmp");
            strcat(grep, ".txt");
            system ("grep");
            printf("%s\n",grep);
            
        return 0;
    }
    
    
     
  10. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #10
    Take a second look at what you're passing to system().
     
  11. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #11
    Considering that I spend half of day missing a * that should have been a +, I'm not sure I'm equipped to follow your advice. I did check and check to see that it is exactly what I typed on the command line. That's why the print statement,too. Go ahead make me feel foolish. What did I miss?
     
  12. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
  13. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #13
    Drop 0the quotes around "grep" in the system call!
     
  14. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #14
    Too old for this sh*t. I guess Detrius was talking about these quotes too, but I didn't quite understand.

    Now, if I may besides piping this to a file and then reading the file, how do I grab the output? Will a scanf work, if its the next line in the code?
     
  15. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #15
    If it's not already clear, that line is not doing what you think it's doing.

    You assembled a command-line before your call to system(), using a series of strcpy() and strcat() calls. Then you completely ignored what you just assembled, and instead passed system() the literal string "grep".

    Which is, of course, an invalid grep command, equivalent to typing this in Terminal:
    Code:
    grep
    
    Go ahead, try it. I'll wait.

    To further confound things, you got the printf() right: it does print the assembled command line.
     
  16. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #16
    No, he wasn't talking about those quotes, because you hadn't posted your malfunctioning code yet. All you'd posted was what you thought your code was doing, not the actual code that showed what was actually happening.

    No, a scanf will not work.
     
  17. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
  18. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #18
    change

    Code:
    system ("grep");
    To

    Code:
    system (grep); 
    ;) - Just wanted to join the party! LOL
     
  19. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #19
    Seconded, but like any resource, be sure to pclose when you've read to the EOF.

    -Lee
     
  20. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #20
    corncob = popen(grep, "r");
    fgets(buf,100,corncob);
    printf("%s\n", buf);

    Thanks guys.

    Chown. There are command line functions that can not be used in a system call which is why the original post was a simple question.

    I do try and debug my own code.
    Time lost looking for a * that should have been a +
    Time lost more than once: for(...);
    Time lost on yesterdays "".
    If anybody has a beach house I can use for awhile ... LOL.
     
  21. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #21
    I stand corrected. Can't know everything all the time. ;)
     
  22. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #22
    Given*the*Pacific Ocean Tsunami*warning*today*don't*tempt*anyone*…
    :eek:
     
  23. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008

Share This Page