Harish Selvam
Harish Selvam

Reputation: 11

Check if a file contains multiple patterns

I want to know how to grep multiple suggestions from a file only if the condition is true.

Example.

file.txt contains

AA
BB
CC
DD

If I do a grep AA & ZZ from file.txt. it should not give any output since it has no ZZ pattern in it. It should satisfy both the condition. Awk or sed anything is fine

Upvotes: 0

Views: 360

Answers (3)

Mathews Jose
Mathews Jose

Reputation: 409

Why don't you search with

grep -e pattern1 -e pattern2 ... file

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203254

You'll want something like this:

awk '/AA/{f=a=1} /ZZ/{f=z=1} f{hits=hits $0 ORS; f=0} END{if (a && z) printf "%s", hits}'

to handle the cases where AA and/or ZZ appear multiple times, possible interleaved with each other, e.g.:

$ cat file
a
AA
b
AA
c
ZZ
d
AA
e
ZZ
f

$ awk '/AA/{f=a=1} /ZZ/{f=z=1} f{hits=hits $0 ORS; f=0} END{if (a && z) printf "%s", hits}' file
AA
AA
ZZ
AA
ZZ

The general solution to find any number of regexps in the file, btw, would be:

$ cat tst.awk
BEGIN {
    split(tgts,tmp)
    for (idx in tmp) {
        wanted[tmp[idx]]
    }
}
{
    found = 0
    for (regexp in wanted) {
        if ($0 ~ regexp) {
            found = 1
            seen[regexp]
        }
    }
}
found {
    hits = hits $0 ORS
}
END {
    if ( length(wanted) == length(seen) ) {
        printf "%s", hits
    }
}

$ awk -v tgts="AA ZZ" -f tst.awk file
AA
AA
ZZ
AA
ZZ

String instead of regexp matching would be:

    for (string in wanted) {
        if (index($0,string) {
            found = 1
            seen[string]
        }
    }

Upvotes: 1

fedorqui
fedorqui

Reputation: 289535

Use:

grep -vf <(grep -f patterns_file file) patterns_file

If you want to cross check if a file contains all the given patterns, you can use the following approach:

  • Store the patterns in a different file, one in a different line.

  • grep the original file against this patterns file.

  • Finally, grep this result against the patterns file, with the inverted flag. This way, all the remaining patterns will show. If the result is empty, it means that everything was found.

File with the data:

$ cat data
AA
BB
CC
DD

File with the patterns:

$ cat pat 
AA
CC
TT

Check for matches in data

$ grep -f pat data
AA
CC

Check if any of the patterns remain unfound:

$ grep -vf <(grep -f pat data) pat
TT

Upvotes: 2

Related Questions