# grep command with AND condition

**URL:** <https://community.unix.com/t/grep-command-with-and-condition/258155>\
**Category:** Shell Programming and Scripting\
**Created:** [January 24, 2010, 11:42pm UTC](https://community.unix.com/t/grep-command-with-and-condition/258155 "2010-01-24T23:42:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![prasperl](https://community.unix.com/letter_avatar/prasperl/32/5_5575768a8748004e209b776fc1b2916d.png) [@prasperl](https://community.unix.com/u/prasperl)\
**Post date:** [January 24, 2010, 11:42pm UTC](https://community.unix.com/t/grep-command-with-and-condition/258155/1 "2010-01-24T23:42:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dinjo\_jo](https://community.unix.com/letter_avatar/dinjo_jo/32/5_5575768a8748004e209b776fc1b2916d.png) [@dinjo\_jo](https://community.unix.com/u/dinjo_jo)\
**Post date:** [January 25, 2010, 12:41am UTC](https://community.unix.com/t/grep-command-with-and-condition/258155/2 "2010-01-25T00:41:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/5_5575768a8748004e209b776fc1b2916d.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [January 25, 2010, 1:06am UTC](https://community.unix.com/t/grep-command-with-and-condition/258155/3 "2010-01-25T01:06:12Z")

</div>

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![drl](https://community.unix.com/user_avatar/community.unix.com/drl/32/542_2.png) [@drl](https://community.unix.com/u/drl)\
**Post date:** [January 25, 2010, 7:24am UTC](https://community.unix.com/t/grep-command-with-and-condition/258155/4 "2010-01-25T07:24:47Z")

</div>

Hi.

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

```nohighlight
#!/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):

```nohighlight
% ./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:

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![xoops](https://community.unix.com/letter_avatar/xoops/32/5_5575768a8748004e209b776fc1b2916d.png) [@xoops](https://community.unix.com/u/xoops)\
**Post date:** [January 25, 2010, 7:59am UTC](https://community.unix.com/t/grep-command-with-and-condition/258155/5 "2010-01-25T07:59:00Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [January 25, 2010, 8:03am UTC](https://community.unix.com/t/grep-command-with-and-condition/258155/6 "2010-01-25T08:03:38Z")

</div>

> [@dinjo\_jo](#):
>
> awk '/WINDOWS/&&/UNIX/&&/ORACLE/' \*.txt

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

Try this instead:

```plaintext
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:

```plaintext
{print FILENAME; nextfile}

```

---

<div class="post-metadata">

**Author:** ![prasperl](https://community.unix.com/letter_avatar/prasperl/32/5_5575768a8748004e209b776fc1b2916d.png) [@prasperl](https://community.unix.com/u/prasperl)\
**Post date:** [January 26, 2010, 3:54am UTC](https://community.unix.com/t/grep-command-with-and-condition/258155/7 "2010-01-26T03:54:54Z")

</div>

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

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