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

Replies are listed 'Best First'.
Re: what is grep command for and condition
by toolic (Bishop) 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.

    A reply falls below the community's threshold of quality. You may see it by logging in.
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 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 (Canon) 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 ')'.