If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > find and print to a file

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-25-09, 20:21
saccskiz saccskiz is offline
Registered User
 
Join Date: Feb 2004
Posts: 143
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
Reply With Quote
  #2 (permalink)  
Old 02-26-09, 02:12
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,258
you are missing the -print flag
find . -exec grep -i findthis '{}' \; -print > ha02.txt
Reply With Quote
  #3 (permalink)  
Old 02-26-09, 10:47
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,578
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
Reply With Quote
  #4 (permalink)  
Old 03-16-09, 17:01
saccskiz saccskiz is offline
Registered User
 
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
Reply With Quote
  #5 (permalink)  
Old 03-16-09, 21:07
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,578
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
Reply With Quote
  #6 (permalink)  
Old 03-17-09, 03:18
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,258
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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On