grep command with AND condition

I want to do a grep with AND condition.

I have three files.

file1.txt

UNIX
......
WINDOWS
........
ORACLE

file2.txt

UNIX
.......
WINDOWS

...and many such files in a directory

I want to get the file which contains UNIX,WINDOWS,ORACLE.

I tried grep -l -e WINDOWS -e UNIX -e ORACLE *.txt

It is returning file1.txt and file2.txt since it works like OR condition

Is there a way to put AND condition in grep?Ideally I would like to see file1.txt as result.

awk '/WINDOWS/&&/UNIX/&&/ORACLE/' *.txt

for file in `ls *.txt`
do
  echo "********check on $file********"
  if grep WINDOWS $file ; then 
        if grep UNIX $file; then
            if grep ORACLE $file; then 
                echo "$file is the file which I am looking for. "
            fi
        fi
   fi
done

Hi.

Utility glark allows you to make a single pass through a file noting when a complex combination of matching expressions is satisfied:

#!/usr/bin/env bash

# @(#) s1	Demonstrate complex matching expressions, glark.
# http://www.incava.org/projects/glark/

# Infrastructure details, environment, commands for forum posts. 
echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
c=$(  ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) 
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p my-nl glark
set -o nounset
echo

FILES="data*"

echo " Content of files :" $FILES
my-nl $FILES

echo
echo " Results with utility glark:"
glark -l \( \( ORACLE -a -1 UNIX \) -a -1 WINDOWS \)  $FILES

exit 0

producing (on your files named data1 and data2):

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
my-nl (local) 296
glark, version 1.8.0

 Content of files : data1 data2

==> data1 <==

  1 UNIX
  2 ......
  3 WINDOWS
  4 ........
  5 ORACLE

==> data2 <==

  1 UNIX
  2 .......
  3 WINDOWS

 Results with utility glark:
data1

The somewhat easiler-to-understand expression:

glark -l \( ORACLE -a -1 UNIX -a -1 WINDOWS \)  $FILES

also seems to work.

The utility was in the Debian repositories for me. See the URL in the script if you need to download it yourself, and for additional information, such as examples of use. The code is actually written in ruby, so one would need that as well.

Best wishes ... cheers, drl

 
$ cat file1.txt:
UNIX
......
WINDOWS
........
ORACLE
 
$ cat file2.txt
UNIX
.......
 
$ grep -l WINDOWS *.txt | xargs grep -l ORACLE | xargs grep -l UNIX
file1.txt

That prints only the lines that contain those 3 patterns, not what the OP expects.

Try this instead:

awk 'FNR=1{w=u=o=0}
/WINDOWS/{w++}
/UNIX/{u++}
/ORACLE/{o++}
w && u && o {print FILENAME}' *.txt

If your awk version supports the nextfile statement you can add it in the last command:

{print FILENAME; nextfile}

Thank you XOOPs....Your suggestion works!!!

Franklin,my awk version is supporting 'nextfile',still thanka a lot for your help