It is currently Tue, 30 Nov 2021 17:44:21 GMT



 
Author Message
 finding files with words that may be on different lines
I'm trying to use grep to find files containing keywords.
Unfortunately these keywords are not all on the same line so grep won't
find all the correct files. Is there a way I can do this?

Paul

Sent via Deja.com http://www.**-**.com/
Share what you know. Learn what you don't.



 Fri, 23 Nov 2001 03:00:00 GMT   
 finding files with words that may be on different lines
Hey,

grep -E 'pattern1|pattern2|pattern3' file_list (or egrep, if it's there)

finds all lines in file_list containing one of the three patterns.

Andreas



 Sat, 24 Nov 2001 03:00:00 GMT   
 finding files with words that may be on different lines
Sorry I should have been more specific. I got the OR case, I'm trying
to get the AND case to work. Normal combining of paterns doesn't seem
to work. ex:

egrep '(pat1)(pat2)(pat3)' *.txt

I need to match all pat's and they are all on different lines.

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



 Sun, 25 Nov 2001 03:00:00 GMT   
 finding files with words that may be on different lines

Hard to do with grep in one pass.

If you can stand one pass per keyword, this will work:

  grep -q 'p1' file1 &&
    grep -q 'p2' file1 &&
      grep -q 'p3' file1 && echo 'file1 matched p1 and p2 and p3'

<Joe
--
 Joe Smith
 University of Pennsylvania
 School of Medicine, Cardiology Division
 Philadelphia, PA 19104
 j...@presto.med.upenn.edu



 Sun, 25 Nov 2001 03:00:00 GMT   
 finding files with words that may be on different lines
You can wrap this with for or while-loops...

Marc



 Mon, 26 Nov 2001 03:00:00 GMT   
 finding files with words that may be on different lines
PH>Sorry I should have been more specific. I got the OR case, I'm trying
PH>to get the AND case to work. Normal combining of paterns doesn't seem
PH>to work. ex:

PH>egrep '(pat1)(pat2)(pat3)' *.txt

PH>I need to match all pat's and they are all on different lines.

PH>> grep -E 'pattern1|pattern2|pattern3' file_list (or egrep, if it's
PH>there)
PH>>
PH>> finds all lines in file_list containing one of the three patterns.

Hmmmm, I know next-to-nothing in Linux.  If I were doing this in DOS you
could grep (I use greps ported to DOS) for the first pat, output those
filenames  file, grep THOSE files for the 2nd pattern ouput THOSE
filenames to another file and grep THOSE files, ouput to a 3rd file.
And THAT file would have your answers.

Sorta clumsy, unless there AINT a utility to do it, then it would woik.

Sand...@bbs.galilei.com
Galileo Informatica - St. Louis, Missouri  USA
          telnet: bbs.galilei.com
          WWW: http://www.galilei.com



 Mon, 26 Nov 2001 03:00:00 GMT   
 finding files with words that may be on different lines

Awk script?  Ala:

#! /usr/bin/awk -f

FNR==1 {
        if (pat1_found & pat1_found & pat3_found) {
                print "  All patterns matched.";
        }
        printf "%s: ", FILENAME;
        pat1_found = 0;
        pat2_found = 0;
        pat3_found = 0;
pat1 {
        pat1_found = 1;
pat2 {
        if (pat1_found)
                pat2_found = 1;
pat3 {
        if (pat1_found & pat2_found)
                pat3_found = 1;

## EOF

Art S. Kagel



 Mon, 26 Nov 2001 03:00:00 GMT   
 finding files with words that may be on different lines
Use perl!

The following will do
(suppose you search for regexp_1,...,regexp_n)

#!/usr/bin/perl

undef $/; # make <> read whole file in one go
while (<>) {
        next unless ( /regexp_1/omi );  # remove 'i' for case-insensitive
                                        # matching
        next unless ( /regexp_2/omi );  # the o is for speed only
        # ...
        next unless ( /regexp_n/omi );
        print "$ARGV\n"

__END__

Save the file and execute with
perl -x <filename>

Marc



 Mon, 26 Nov 2001 03:00:00 GMT   
 finding files with words that may be on different lines

Don't use grep, use awk!

Your requirement requires a small program, but here is a quick'n'dirty
hack that produces the output in 'grep -n' format listing the _first_
matched lines containing each pattern - edit the patterns in the file at
the /pat1/, /pat2/ and /pat3 lines...

----- CUT HERE, save as grep3 and chmod +x grep3 ------------
#!/usr/bin/awk -f
#############################################################
# usage: grep3 <filenames>
# if <filenames> is ommitted,  stdin is used
#############################################################

# called on each line to check file name
# and mark start of new file
{
    if ( FILENAME != curr_filename ){
        end_of_file();
        start_of_file();
    }

/pat1/ {
# print "matched pat1"
    if ( ! pat1_found_line ) {
        pat1_found_line=NR-start_line;
        pat1_line_text=$0;
    }

/pat2/ {
# print "matched pat2"
    if ( ! pat2_found_line ) {
        pat2_found_line=NR-start_line;
        pat2_line_text=$0;
    }

/pat3/ {
# print "matched pat2"
    if ( ! pat3_found_line ) {
        pat3_found_line=NR-start_line;
        pat3_line_text=$0;
    }

# clean-up code
END {
    end_of_file();

function start_of_file() {
    curr_filename=FILENAME;
    # print "checking",curr_filename
    pat1_line_text="";
    pat2_line_text="";
    pat3_line_text="";
    pat1_found_line=0;
    pat2_found_line=0;
    pat3_found_line=0;
    start_line=NR-1
----- CUT HERE ------------

--
Niel Markwick
Kontich, Belgium



 Tue, 27 Nov 2001 03:00:00 GMT   
 finding files with words that may be on different lines

I do this frequently using grep and xargs, like this:

    grep -l pattern1 | xargs grep -l pattern2 | xargs grep -l pattern3

It may not be elegant, but it's simple.

Gary



 Fri, 30 Nov 2001 03:00:00 GMT   
 
   [ 10 post ] 

Similar Threads

1. How to find the last line that contains a word in file

2. APPLIX: wordcount.am 1.1 - Word count macro for Words

3. How to find if the last line ends with a specified word

4. How to find the first readable word in one line

5. adding a word to a line in a file

6. Searching a file for 2 different regexp on 2 adjacent lines

7. Scanning a file and joining different groups of lines

8. Extract different lines from a text file

9. how to find the matched word in the file

10. to find the word in file by 2 rules


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software