Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2004
    Posts
    143

    Unanswered: find and print to a file

    Am on SunOS 5.8
    When I try this :

    server01-(/export/home/ha02) $ find . -exec grep -i findthis '{}' \; -print

    Gives me

    findthis
    ./med/test123.ksh
    findthis
    ./test456.ksh

    All I needed is that what printed above to be written to a file
    So, I try this :

    server01-(/export/home/ha02) $ find . -exec grep -i findthis '{}' \; > ha02.txt

    Then, when I do
    server01-(/export/home/ha02) $ cat ha02.txt

    Gives me

    findthis
    findthis
    findthis
    findthis

    The file paths as seen above while giving the print option is not written to this ha02.txt. What am I missing ? Also, I am surprised that findthis gets written 4 times in the file (whereas there are only 2 files that have this string occurance) !!

    Thanks

  2. #2
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    you are missing the -print flag
    find . -exec grep -i findthis '{}' \; -print > ha02.txt

  3. #3
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    Here is an example what you could do:
    Code:
    find . -type f -a \( ! -exec grep -qi findthis "{}" \; -prune -o -print -exec grep -i findthis "{}" \; \)
    Here is how it works:
    • -type f => only process files
    • ! -exec grep -q ... -prune => filter-out files that do NOT contain "findthis"
    • -o -print => print out the file name (before matching lines)
    • -exec grep ... => print out the matching lines in the file
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  4. #4
    Join Date
    Feb 2004
    Posts
    143

    Thank you both

    Pdreyer,
    Thanks. Your suggestion just worked fine.
    Stolze,
    Thanks for your response on a different variation to this. Though I got what I needed, I was equally curious to try your solution. Maybe due to the way we have set up or so(not sure), but I got a bunch of errors stating

    Usage: grep -hblcnsviw pattern file . . .
    grep: illegal option -- q
    Usage: grep -hblcnsviw pattern file . . .
    grep: illegal option -- q
    Usage: grep -hblcnsviw pattern file . . .
    grep: illegal option -- q
    Usage: grep -hblcnsviw pattern file . . .
    grep: illegal option -- q

  5. #5
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    I guess you are not using GNU grep, so you have to check the man page for your grep how you can test for matches and use the proper option instead of -q.
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  6. #6
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    On SunOS the -q option is available if you use the version of grep in
    /usr/xpg4/bin/grep
    Code:
    man XPG4
         ...
         If the behavior required by POSIX.2, POSIX.2a, XPG4, SUS, or
         SUSv2  conflicts  with  historical Solaris utility behavior,
         the original Solaris version of the utility is unchanged;  a
         new version that is standard-conforming has been provided in
         /usr/xpg4/bin.
         ...
         An application that wants to use standard-conforming  utili-
         tues  must  set  the PATH...
    Last edited by pdreyer; 03-17-09 at 03:24.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •