Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

OT: what is grep command for and condition

by anakin30 (Acolyte)
on Aug 17, 2010 at 15:04 UTC ( #855508=perlquestion: print w/ replies, xml ) Need Help??
anakin30 has asked for the wisdom of the Perl Monks concerning the following question:

i have a filename access and i tail the file to grep "MOD" or "DEL" with the below command. tail -f access | egrep -i '(MOD|DEL)' But how can I grep with 'and' condition? I tried below command. tail -f access | egrep -i '(MOD&DEL)', but nothing is returned

Comment on OT: what is grep command for and condition
Re: what is grep command for and condition
by moritz (Cardinal) on Aug 17, 2010 at 15:06 UTC
    Just use two grep commands in a pipe: ... | grep -i mod |grep -i del
    Perl 6 - links to (nearly) everything that is Perl 6.
Re: what is grep command for and condition
by toolic (Chancellor) on Aug 17, 2010 at 15:15 UTC
    Since this is a Perl site, here's a Perl solution (see perlrun):
    tail -f access | perl -ne 'print if /mod/i and /del/i'

    In the future, please use code tags for your code. See Writeup Formatting Tips.

      i executed this command below: tail -f access | grep -i "MOD" | grep -i "DEL" it's not working.. anyway thanks for your response...

        Firstly, you replied to the wrong node. You should have replied to moritz's node.

        Secondly, that looks like correct grep syntax to me. I bet that your 'access' file does not have a line with both MOD and DEL in it, something like:

        foo DEL bar MOD goo
Re: what is grep command for and condition
by aquarium (Curate) on Aug 17, 2010 at 23:30 UTC
    Make sure you understand your logic..as programming logical operators (and not or) behave sometimes a bit differently to what one might intend or write in spoken language terms. So if you actually want lines that match "MOD" or "DEL" to show up in your output, you need to have a OR operator. Using AND operator will only ever show lines that have both match strings on the same line.
    the hardest line to type correctly is: stty erase ^H

      thanks for your explanation. i have a doubt, i have below log file. how to grep only lines which has MOD or DEL only and pipe into a new file. can you please help me. 18/Aug/2010:08:30:39 +0000 conn=108559 op=3375 msgId=3376 - RESULT err=0 tag=120 nentries=0 etime=0 18/Aug/2010:08:30:39 +0000 conn=105611 op=38083 msgId=38084 - EXT oid="1.3.6.1.4.1.42.2.27.9.6.8" 18/Aug/2010:08:30:39 +0000 conn=105611 op=0 msgId=38084 - MOD dn="uid=cpb4tpz1,ou=users,o=dhl.com" 18/Aug/2010:08:30:39 +0000 conn=105611 op=0 msgId=38084 - RESULT err=0 tag=103 nentries=0 etime=0 csn=4c6c19c100020c1d0000 18/Aug/2010:08:30:39 +0000 conn=105611 op=38083 msgId=38084 - RESULT err=0 tag=120 nentries=0 etime=0 18/Aug/2010:08:30:39 +0000 conn=105611 op=38084 msgId=38085 - EXT oid="1.3.6.1.4.1.42.2.27.9.6.5" 18/Aug/2010:08:32:37 +0000 conn=108681 op=0 msgId=95 - DEL dn="cn=asia,cn=mydigi.maxis.com.my,cn=meta-directories,cn=join

Re: what is grep command for and condition
by Marshall (Prior) on Aug 18, 2010 at 09:05 UTC
    Forget about tail until you get the "main thing" working... egrep -i 'MOD|DEL' Get rid of the '(' and ')'.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://855508]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-12-22 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (109 votes), past polls