Operating System - HP-UX
1834327 Members
2454 Online
110066 Solutions
New Discussion

Re: multiple grep question

 
SOLVED
Go to solution
Edgar_10
Frequent Advisor

multiple grep question

Hi,

I have approx. 6000 files & want to search for about 7 different numeric strings. Any ideas how this could be done?

Regards,
9 REPLIES 9
Colin Topliss
Esteemed Contributor
Solution

Re: multiple grep question

Try using egrep:

wedny:/root=> ls |egrep -e "PHCO_27673" -e "lvmrc" -e "test1.txt"
PHCO_27673
PHCO_27673.depot
PHCO_27673.text
lvmrc
test1.txt

Col.
John Palmer
Honored Contributor

Re: multiple grep question

Identify your files and search them with grep. One way is to combine find and grep...

find -exec grep -e string1 -e string2 -e string3... {} \;

If you only want to know which files contain one of the strings, supply the -l flag to grep.

Regards,
John
Robin Wakefield
Honored Contributor

Re: multiple grep question

Hi,

A number of ways exist, depending on where your files are located. If they're all in one directory, then:

grep -e 123 -e 456 -e 789 *

or

grep -E '(123|456|789)' *

or

create a file, say /tmp/strings containing the search strings and:

grep -f /tmp/strings *


If the files are in subdirectories, then:

find /dir -type f | xargs grep -E '(123|456|789)'

or any of the other greps above.

rgds, Robin
Donald Kok
Respected Contributor

Re: multiple grep question

Hi,

I use a script called findgrep:

case $# in
1) cat /tmp/filelist | grep $1 ;;
2) cat /tmp/filelist | grep $1 | grep $2 ;;
3) cat /tmp/filelist | grep $1 | grep $2 | grep $3 ;;
4) cat /tmp/filelist | grep $1 | grep $2 | grep $3 | grep $4 ;;
5) cat /tmp/filelist | grep $1 | grep $2 | grep $3 | grep $4 | grep $5 ;;
*) echo " Thats much parameters " ;;
esac


It makes use of a file /tmp/filelist. This file is made by a cron job which performs: find / -print > /tmp/filelist

Thsi way the findgrep works very fast.

HTH
Donald
My systems are 100% Murphy Compliant. Guaranteed!!!
RAC_1
Honored Contributor

Re: multiple grep question

ll|grep -Ei "you|me|they|anythingyouwant" /filename
There is no substitute to HARDWORK
RAC_1
Honored Contributor

Re: multiple grep question

grep -Ei "you|me|they|anythingyouwant" /filename
There is no substitute to HARDWORK
Michael Steele_2
Honored Contributor

Re: multiple grep question

Use find command to parse out the 6000 files in the directory and the -exec option in find combined with grep -e to search each file. For example:

cd /dir
find . -name "file pattern" -exec grep -e string -e string.

Modify your grep with:
-i = case insensative
-v = omit

For example, your string has upper and lower case char.s:

cd /dir
find . -name "file pattern" -exec grep -i -e string -e string.

For example, you want to eliminate everything and take the remainder:

cd /dir
find . -name "file pattern" -exec grep -v -e string -e string.

For example, your string has upper and lower case char.s AND you want to eliminate everything and take the remainder:

cd /dir
find . -name "file pattern" -exec grep -i -v -e string -e string.

Support Fatherhood - Stop Family Law
Michael Steele_2
Honored Contributor

Re: multiple grep question

Gees, you know, after all of that I forgot to close off the find command with curly brackets.

cd /dir
find . -name "file pattern" -exec grep -v -e string -e string {} \;

NOTE the end of all find is {} \;

(* I hate that when it happens. :-) *)
Support Fatherhood - Stop Family Law
Edgar_10
Frequent Advisor

Re: multiple grep question

Hi,

Thanks for the GREAT info!!

Regards,