Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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?
 
Just as it would fail from the command line.

Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
 
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.
 
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.

The system() function uses a shell. Read the man page.
 
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.
 
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
 
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;
}
 
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?
 
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?
 
Code:
        system ("grep");
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.
 
Too old for this sh*t. I guess Detrius was talking about these quotes too, but I didn't quite understand.

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.

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?

No, a scanf will not work.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.