grep command with AND condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep command with AND condition
# 1  
Old 01-25-2010
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.
# 2  
Old 01-25-2010
awk '/WINDOWS/&&/UNIX/&&/ORACLE/' *.txt
# 3  
Old 01-25-2010
Code:
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

# 4  
Old 01-25-2010
Hi.

Utility glark allows you to make a single pass through a file noting when a complex combination of matching expressions is satisfied:
Code:
#!/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):
Code:
% ./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:
Code:
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

Last edited by drl; 01-25-2010 at 08:30 AM.. Reason: Edit 1: additional expression
# 5  
Old 01-25-2010
if you have number of patterns limited

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

# 6  
Old 01-25-2010
Quote:
Originally Posted by 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:

Code:
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:

Code:
{print FILENAME; nextfile}

# 7  
Old 01-26-2010
Thank you XOOPs....Your suggestion works!!!


Franklin,my awk version is supporting 'nextfile',still thanka a lot for your help
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep with condition

I have file input AAAA_XX_Start> rlong . 0W 130526-11:36:13 10.128.13.226 9.0j RBS_NODE_MODEL_N_1_50 stopfile=/tmp/13019 .. ================================================================================================================= MO ... (2 Replies)
Discussion started by: radius
2 Replies

2. UNIX for Dummies Questions & Answers

How to grep with certain condition?

hi all, i have an xml what i have to do is to search for the source id(s1) and if it matches with that in xml then extract the file mask from the name of the file i.e if the file name is idr_%YYYY%%MM%%DD%_%N%.idr then , i want the part after first % and before last % ie in this case ... (5 Replies)
Discussion started by: ramsavi
5 Replies

3. UNIX for Dummies Questions & Answers

Grep certain lines with condition

file input aaaa,52C aaaa,50C bbbb,50C bbbb,58C aaaa,52C bbbb,50C aaaa,30C bbbb,58C cccc,60C i want to print uniq lines with its max value of column2 expected output aaaa,52C bbbb,58C cccc,60C tks (4 Replies)
Discussion started by: radius
4 Replies

4. Shell Programming and Scripting

Where condition in grep or awk?

Dear All, I need help.. I am having a csv file. Home_TITLE,People_TITLE,Repo_ALIAS HMN5530,RKY5807,/mine_repo/rike001 HMN5530,SRY6443,/mine_repo/rike001 HMN5530,ARDY001,/mine_repo/rike001 If i have two value in varible RKY5807, HMN5530. how can fetch and store another value... (6 Replies)
Discussion started by: yadavricky
6 Replies

5. Shell Programming and Scripting

grep not equal to condition

I have below files under dir_a and dir_b and i want to sort out number of dir_a and dir_b files seperately and if i do the grep it should retrun 2 files in dir_a and 1 file in dir_b. /dir_a/12345678 /dir_a/87654321 /dir_a/dir_b/12345687 But i am getting cat file|grep dir_a|wc -l 3... (6 Replies)
Discussion started by: prash358
6 Replies

6. Shell Programming and Scripting

grep in the if condition

Hi, In this code can able to match the pattern without case sensitive. Is that possible? if u knw plz help me... code: echo "Enter name to search" read n if ; echo "name found" else echo "Not Found" fi (8 Replies)
Discussion started by: boopal
8 Replies

7. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

8. Shell Programming and Scripting

grep inside if condition - need help

hi i need help with below code. if ] then log "Exiting the script as ID= NULL" log "Please run script first." fi i am calling grep inside this but its not running any ideas why ?? input file is like this -- Msg 102, Level 20, State 1: Server... (4 Replies)
Discussion started by: dazdseg
4 Replies

9. Homework & Coursework Questions

Grep line above X condition

1. The problem statement, all variables and given/known data: I have to grep a data file called datebook.txt. The last information in each line is a salary. I have to grep all the lines which precede those lines with 6 figure salaries. I can't SID it, or use Perl. It has to be grep (or egrep or... (3 Replies)
Discussion started by: DrSammyD
3 Replies
Login or Register to Ask a Question