not grep

Discussion in 'Mac Programming' started by farmerdoug, Apr 23, 2010.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    If grep fails to find a file it prints out an error message. Neither redirecting the output of grep
    Code:
     strcpy(grep, "grep        \"");
                strcat(grep, date2[d]);
                strcat(grep, ",1552\" ");
                strcat(grep, dir);
                strcat(grep, &ratios[i*pagesize]);
                strcat(grep, ".txt > \"out.txt\"");
                corncob = popen(grep, "r");
                fgets(buf,100,corncob);
                pclose(corncob);
    
    nor redirecting the output of the program

    ./code > "out.txt"

    gets me a list of the files it can't find. Any suggestions on how to do that?
     
  2. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #2
    not grep part 2

    If I use ls file | wc, I can tell if the file exists. How do I get the values out of wc?
     
  3. mslide macrumors 6502a

    Joined:
    Sep 17, 2007
    #3
    Is your goal to redirect those grep error messages (about the fact that it can't find certain files) to another file? The standard redirect symbol ( > ) with nothing else only redirects stuff sent to the standard output stream. Error messages (like messages about the fact that grep can't find a certain file) are sent to the standard error stream and are redirected differently.

    Here are a few examples:

    Redirect normal results and errors:
    Code:
    grep foo bar > results_and_errors.txt 2>&1
    A simplified version of the above command is (which may not work on all types of shells):
    Code:
    grep foo bar >& results_and_errors.txt
    Redirect errors only:
    Code:
    grep foo bar 2> errors_only.txt
    The "Redirection (computing)" wikipedia article has a lot of good info about this. I hope this is what you were asking.
     
  4. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #4
    I search google but didn't think to search redirection.
    Method 3 worked for me.
    Thanks
     

Share This Page