looping search question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting looping search question
# 1  
Old 11-15-2006
looping search question

This is my requirement--

I have a list file that contains a list of 50 words.
eg
word1
word2
word3

I want to search/grep for each of those words in a directory/and subdirectories and list the file in which that word exists.


Ne ideas for script/command?

I know grep -r <pattern> /direaddress will search for existence of a pattern in all files in a directory...but how will i grep for a pattern in a filelist in directory/subdirectories.

Last edited by alfredo123; 11-15-2006 at 04:03 PM..
# 2  
Old 11-15-2006
Code:
find /path -type f | \
while read file do
    grep -l  -f /path/to/wordfile  "$file"
done > list_of_found_files

# 3  
Old 11-15-2006
Thnx

Thanks a heap Jim...I think the below did the job for me and I needed to search the highest directory to see results from subdirectories.

Code:
!#usr/bin/ksh
#Example usage greptest.sh <filelist> <directory to search>

while read line
    do
    grep -r ${line} $2 
    done <$1
    
exit 0

# 4  
Old 11-16-2006
Quote:
Originally Posted by jim mcnamara
Code:
find /path -type f | \
while read file do
    grep -l  -f /path/to/wordfile  "$file"
done > list_of_found_files

The while loop can be replaced by the xargs command :
Code:
find /path -type f | xargs grep -l -f /path/to/wordfile  > list_of_found_files

The -r option doesn't exist for grep on all Unix flavors, it why we use a find command.

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk pattern match by looping through search patterns

Hi I am using Solaris 5.10 & ksh Wanted to loop through a pattern file by reading it and passing it to the awk to match that value present in column 1 of rawdata.txt , if so print column 1 & 2 in to Avlblpatterns.txt. Using the following code but it seems some mistakes and it is running for... (2 Replies)
Discussion started by: ananan
2 Replies

2. Shell Programming and Scripting

Search and replace question

Hi all, I am trying to modify an xml file and I wanted to search and replace using the sed command but here is my issue. I want to search and replace maximumHeapSize="512" and replace it with maximumHeapSize="768" but I have multiple files with different values so I can't search for... (2 Replies)
Discussion started by: reyes99
2 Replies

3. Shell Programming and Scripting

A question about awk search

I want to search a field and print first field in these record. There is a "*" in the field I want to search, like "TRBD2*01". I used the command like this: awk '/TRBD2*01/ {print $1}' test.txt But it doesn't work. The command line awk '/TRBD2/ {print $1}' test.txt will work. How... (4 Replies)
Discussion started by: xshang
4 Replies

4. Homework & Coursework Questions

Help with looping question

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I need to write a script that accepts 3 filenames as command line parameters and test if they are executable and... (1 Reply)
Discussion started by: tommynewb
1 Replies

5. UNIX for Dummies Questions & Answers

search and replace one more question

Hi, I have a file that contains the following contents: 14:05 apple orange123 456mango 16:45 banana I wanted to replace ONLY the "14:05 " and "16:45" with nothing and trying to use the following syntax sed -e 's/*//g' -e 's/^: //g' my_file > new_temp cat new_temp apple orange... (2 Replies)
Discussion started by: ghazi
2 Replies

6. Shell Programming and Scripting

Looping question

Hi, I have series of data stored in a variable xyz: (between 0 and 100) example: 20 45 98 21..... I need to find if there is/are any occurance of data > 95 Not sure what kind of looping is required to check. Please help. thanks (2 Replies)
Discussion started by: hemangjani
2 Replies

7. UNIX Desktop Questions & Answers

search question

From time to time I need to extract portions of a very large weblogic log file. Each line in the file begins with a date stamp like this: ####<Dec 26, 2005 10:58:30 PM CST> What would be the most efficient way to select all lines in the file between, say, 10:15 PM and 10:20 PM? (1 Reply)
Discussion started by: kf199
1 Replies

8. Shell Programming and Scripting

Perl question - looping through an array of hashrefs

I have an array of hashrefs that look like the following: my @LAYOUT = ( {SQL_1 => "select count (*) FROM prospect WHERE PROCESS_DATE = To_date('INSERT_DATE_HERE', 'mm/dd/yyyy') and tiff_filename is not null ... (2 Replies)
Discussion started by: kregh99
2 Replies

9. Shell Programming and Scripting

nested looping question

Hi, I'm having some trouble with the syntax in constructing a simple nested 'for' loop. My code is as follows: #!/bin/bash dir1="fred flume haystack" for dir2 in ${dir1} do fred="1 2 3" flume="a b c" ... (7 Replies)
Discussion started by: Sn33R
7 Replies

10. UNIX for Dummies Questions & Answers

looping question

I am writing a simple script and want to keep the user in a fuction until they are ready to get out. For some (probably stupid) reason, it doesn't seem to be working. You guys see anything that I'm overlooking? crsd() {until do /home/wcs3611.crsdtmp.sh echo 'run another? \c' ... (1 Reply)
Discussion started by: hedrict
1 Replies
Login or Register to Ask a Question