logical AND and OR with grep?

  • Follow


Hi All,

Without using two lines of grep, is there a way to
do a logical AND or OR with grep?

either abc or xyz

        cat xxx | grep -i  abc OR xyz

or must have both

        cat xxx | grep -i  abc AND xyz

Many thanks,
-T
0
Reply Todd 5/10/2010 5:44:33 PM

On 05/10/2010 10:44 AM, Todd wrote:
> Hi All,
>
> Without using two lines of grep, is there a way to
> do a logical AND or OR with grep?
>
> either abc or xyz
>
> cat xxx | grep -i abc OR xyz
>
> or must have both
>
> cat xxx | grep -i abc AND xyz
>
> Many thanks,
> -T

I figured out the logical OR.

$ grep -i -E "Nested|vpid" WS08-1.info.txt
Nested Paging:   off
VT-x VPID:       off


Anyone know how to do a logical AND?

-T
0
Reply Todd 5/10/2010 7:32:57 PM


Todd <todd@invalid.com> writes:

> Without using two lines of grep, is there a way to
> do a logical AND or OR with grep?

You can do logical OR.

> either abc or xyz
> 
>         cat xxx | grep -i  abc OR xyz

cat xxx | grep    '\(abc\|xyz\)'
cat xxx | grep -E '(abc|xyz)'
cat xxx | egrep   '(abc|xyz)'

> or must have both
> 
>         cat xxx | grep -i  abc AND xyz

Not really.

cat xxx | egrep '(abc.*xyz|xyz.*abc)'

but this obviously doesn't scale...

Vilmos
0
Reply Vilmos 5/10/2010 7:35:37 PM

On 05/10/2010 12:35 PM, Vilmos Soti wrote:
> Todd<todd@invalid.com>  writes:
>
>> Without using two lines of grep, is there a way to
>> do a logical AND or OR with grep?
>
> You can do logical OR.
>
>> either abc or xyz
>>
>>          cat xxx | grep -i  abc OR xyz
>
> cat xxx | grep    '\(abc\|xyz\)'
> cat xxx | grep -E '(abc|xyz)'
> cat xxx | egrep   '(abc|xyz)'
>
>> or must have both
>>
>>          cat xxx | grep -i  abc AND xyz
>
> Not really.
>
> cat xxx | egrep '(abc.*xyz|xyz.*abc)'
>
> but this obviously doesn't scale...
>
> Vilmos


Thank you.

Figured out the logical AND, but not with grep:

$ awk '/Nested/ && /Paging/' WS08-1.info.txt
Nested Paging:   off

0
Reply Todd 5/10/2010 7:43:50 PM

On Mon, 10 May 2010 12:32:57 -0700, Todd wrote:
 > On 05/10/2010 10:44 AM, Todd wrote:

 >> Without using two lines of grep, is there a way to
 >> do a logical AND or OR with grep?
 >>
 >> either abc or xyz
 >>
 >> cat xxx | grep -i abc OR xyz
 >>
 >> or must have both
 >>
 >> cat xxx | grep -i abc AND xyz

 > Anyone know how to do a logical AND?

cat xxx | grep -i abc | grep -i xyz

Or is that "two lines of grep"?

Bob T.
0
Reply Bob 5/10/2010 7:50:37 PM

Todd wrote:
> On 05/10/2010 12:35 PM, Vilmos Soti wrote:
>> Todd<todd@invalid.com>  writes:
>>
>>> Without using two lines of grep, is there a way to
>>> do a logical AND or OR with grep?
>>
>> You can do logical OR.
>>
>>> either abc or xyz
>>>
>>>          cat xxx | grep -i  abc OR xyz
>>
>> cat xxx | grep    '\(abc\|xyz\)'
>> cat xxx | grep -E '(abc|xyz)'
>> cat xxx | egrep   '(abc|xyz)'
>>
>>> or must have both
>>>
>>>          cat xxx | grep -i  abc AND xyz
>>
>> Not really.
>>
>> cat xxx | egrep '(abc.*xyz|xyz.*abc)'
>>
>> but this obviously doesn't scale...
>>
>> Vilmos
> 
> 
> Thank you.
> 
> Figured out the logical AND, but not with grep:
> 
> $ awk '/Nested/ && /Paging/' WS08-1.info.txt
> Nested Paging:   off
> 
:-)

WE always used to reach for the awk manual when grep got silly.
0
Reply The 5/10/2010 7:52:30 PM

On 05/10/2010 12:50 PM, Bob Tennent wrote:
> On Mon, 10 May 2010 12:32:57 -0700, Todd wrote:
>   >  On 05/10/2010 10:44 AM, Todd wrote:
>
>   >>  Without using two lines of grep, is there a way to
>   >>  do a logical AND or OR with grep?
>   >>
>   >>  either abc or xyz
>   >>
>   >>  cat xxx | grep -i abc OR xyz
>   >>
>   >>  or must have both
>   >>
>   >>  cat xxx | grep -i abc AND xyz
>
>   >  Anyone know how to do a logical AND?
>
> cat xxx | grep -i abc | grep -i xyz
>
> Or is that "two lines of grep"?
>
> Bob T.

I am being a perfectionist and trying to get it
all on one line.  :'[

-T
0
Reply Todd 5/10/2010 7:56:41 PM

>> Figured out the logical AND, but not with grep:
>>
>> $ awk '/Nested/ && /Paging/' WS08-1.info.txt
>> Nested Paging: off
>>
> :-)
>
> WE always used to reach for the awk manual when grep got silly.

Do you know how to do it (with awk) case insensitive?
0
Reply Todd 5/10/2010 7:57:56 PM

Todd wrote:
>>> Figured out the logical AND, but not with grep:
>>>
>>> $ awk '/Nested/ && /Paging/' WS08-1.info.txt
>>> Nested Paging: off
>>>
>> :-)
>>
>> WE always used to reach for the awk manual when grep got silly.
> 
> Do you know how to do it (with awk) case insensitive?

Sorry.. too many years have passed..and I sold my sed awk and grep 
o'reilly :-)
0
Reply The 5/10/2010 8:11:12 PM

The Natural Philosopher wrote:
> Todd wrote:
>> On 05/10/2010 12:35 PM, Vilmos Soti wrote:
>>> Todd<todd@invalid.com>  writes:
>>>
>>>> Without using two lines of grep, is there a way to
>>>> do a logical AND or OR with grep?
>>>
>>> You can do logical OR.
>>>
>>>> either abc or xyz
>>>>
>>>>          cat xxx | grep -i  abc OR xyz
>>>
>>> cat xxx | grep    '\(abc\|xyz\)'
>>> cat xxx | grep -E '(abc|xyz)'
>>> cat xxx | egrep   '(abc|xyz)'
>>>
>>>> or must have both
>>>>
>>>>          cat xxx | grep -i  abc AND xyz
>>>
>>> Not really.
>>>
>>> cat xxx | egrep '(abc.*xyz|xyz.*abc)'
>>>
>>> but this obviously doesn't scale...
>>>
>>> Vilmos
>>
>>
>> Thank you.
>>
>> Figured out the logical AND, but not with grep:
>>
>> $ awk '/Nested/ && /Paging/' WS08-1.info.txt
>> Nested Paging:   off
>>
> :-)
> 
> WE always used to reach for the awk manual when grep got silly.

I hate awk.  The commmands are so cryptic that whomever tries to read 
the code later has to work at it to understand it.  A simple shell 
script is more useful and reliable in the long run.
0
Reply A 5/10/2010 8:33:17 PM

On 2010-05-10, Todd <todd@invalid.com> wrote:

>>>  Anyone know how to do a logical AND?
>>
>> cat xxx | grep -i abc | grep -i xyz
>>
>> Or is that "two lines of grep"?
>
> I am being a perfectionist and trying to get it
> all on one line.  :'[

That _was_ all on one line.

-- 
Grant Edwards               grant.b.edwards        Yow! I have accepted
                                  at               Provolone into my life!
                              gmail.com            
0
Reply Grant 5/10/2010 8:50:14 PM

Todd <todd@invalid.com> writes:

>>> Figured out the logical AND, but not with grep:
>>>
>>> $ awk '/Nested/ && /Paging/' WS08-1.info.txt
>>> Nested Paging: off
>>>
>> :-)
>>
>> WE always used to reach for the awk manual when grep got silly.
> 
> Do you know how to do it (with awk) case insensitive?

awk 'BEGIN {IGNORECASE=1} /nested/ && /paging/' WS08-1.info.txt

Vilmos
0
Reply Vilmos 5/10/2010 10:02:18 PM

On 05/10/2010 01:50 PM, Grant Edwards wrote:
> On 2010-05-10, Todd<todd@invalid.com>  wrote:
>
>>>>   Anyone know how to do a logical AND?
>>>
>>> cat xxx | grep -i abc | grep -i xyz
>>>
>>> Or is that "two lines of grep"?
>>
>> I am being a perfectionist and trying to get it
>> all on one line.  :'[
>
> That _was_ all on one line.
>

You are correct.  I was trying to get it all in one statement.
Just being a perfectionist.
0
Reply Todd 5/11/2010 5:11:41 PM

On 05/10/2010 03:02 PM, Vilmos Soti wrote:
> awk 'BEGIN {IGNORECASE=1}/nested/  &&  /paging/' WS08-1.info.txt
>
Cool.  Thank you!
0
Reply Todd 5/11/2010 5:12:37 PM

On 05/10/2010 01:33 PM, A Watcher wrote:

> I hate awk. The commmands are so cryptic that whomever tries to read the
> code later has to work at it to understand it.

I hear you there.  I keep a list of examples.  That is
the only way I remember.
0
Reply Todd 5/11/2010 5:13:55 PM

On 05/11/10 13:11, Todd wrote:
> On 05/10/2010 01:50 PM, Grant Edwards wrote:
>> On 2010-05-10, Todd<todd@invalid.com> wrote:
>>
>>>>> Anyone know how to do a logical AND?
>>>>
>>>> cat xxx | grep -i abc | grep -i xyz
>>>>
>>>> Or is that "two lines of grep"?
>>>
>>> I am being a perfectionist and trying to get it
>>> all on one line. :'[
>>
>> That _was_ all on one line.
>>
>
> You are correct. I was trying to get it all in one statement.
> Just being a perfectionist.

cat xxx|grep -i abc
is  2 statements.
grep -i abc <xxx
is 1 statement.
0
Reply Joe 5/11/2010 5:41:46 PM

The Natural Philosopher <tnp@invalid.invalid> wrote:
>  Todd wrote:
> > On 05/10/2010 12:35 PM, Vilmos Soti wrote:
> >> Todd<todd@invalid.com>  writes:
> >>
> >>> Without using two lines of grep, is there a way to
> >>> do a logical AND or OR with grep?
> >>
> >> You can do logical OR.
> >>
> >>> either abc or xyz
> >>>
> >>>          cat xxx | grep -i  abc OR xyz
> >>
> >> cat xxx | grep    '\(abc\|xyz\)'
> >> cat xxx | grep -E '(abc|xyz)'
> >> cat xxx | egrep   '(abc|xyz)'
> >>
> >>> or must have both
> >>>
> >>>          cat xxx | grep -i  abc AND xyz
> >>
> >> Not really.
> >>
> >> cat xxx | egrep '(abc.*xyz|xyz.*abc)'
> >>
> >> but this obviously doesn't scale...
> >>
> >> Vilmos
> > 
> > 
> > Thank you.
> > 
> > Figured out the logical AND, but not with grep:
> > 
> > $ awk '/Nested/ && /Paging/' WS08-1.info.txt
> > Nested Paging:   off
> > 
> :-)
> 
>  WE always used to reach for the awk manual when grep got silly.

Until perl, right?

-- 
PLEASE post a SUMMARY of the answer(s) to your question(s)!
Unless otherwise noted, the statements herein reflect my personal
opinions and not those of any organization with which I may be affiliated.
0
Reply Kevin 5/11/2010 7:42:05 PM

Kevin the Drummer wrote:
> The Natural Philosopher <tnp@invalid.invalid> wrote:
>>  Todd wrote:
>>> On 05/10/2010 12:35 PM, Vilmos Soti wrote:
>>>> Todd<todd@invalid.com>  writes:
>>>>
>>>>> Without using two lines of grep, is there a way to
>>>>> do a logical AND or OR with grep?
>>>> You can do logical OR.
>>>>
>>>>> either abc or xyz
>>>>>
>>>>>          cat xxx | grep -i  abc OR xyz
>>>> cat xxx | grep    '\(abc\|xyz\)'
>>>> cat xxx | grep -E '(abc|xyz)'
>>>> cat xxx | egrep   '(abc|xyz)'
>>>>
>>>>> or must have both
>>>>>
>>>>>          cat xxx | grep -i  abc AND xyz
>>>> Not really.
>>>>
>>>> cat xxx | egrep '(abc.*xyz|xyz.*abc)'
>>>>
>>>> but this obviously doesn't scale...
>>>>
>>>> Vilmos
>>>
>>> Thank you.
>>>
>>> Figured out the logical AND, but not with grep:
>>>
>>> $ awk '/Nested/ && /Paging/' WS08-1.info.txt
>>> Nested Paging:   off
>>>
>> :-)
>>
>>  WE always used to reach for the awk manual when grep got silly.
> 
> Until perl, right?
> 
No.

Looked at Perl, and decided it was more opaque than C, and much larger 
and useless than awk. But I didn't get involved in that sort of code 
after that anyway. Left it to the minions ;-)

I was involved in system design not system admin.

0
Reply The 5/11/2010 8:02:55 PM

On 2010-05-10, Vilmos Soti <vilmos@soti.ca> wrote:
> Todd <todd@invalid.com> writes:
>
>>>> Figured out the logical AND, but not with grep:
>>>>
>>>> $ awk '/Nested/ && /Paging/' WS08-1.info.txt
>>>> Nested Paging: off
>>>>
>>> :-)
>>>
>>> WE always used to reach for the awk manual when grep got silly.
>> 
>> Do you know how to do it (with awk) case insensitive?
>
> awk 'BEGIN {IGNORECASE=1} /nested/ && /paging/' WS08-1.info.txt
>
> Vilmos

I think IGNORECASE is a gawk feature. For other awks you could write
awk 'tolower($0) ~ /nested/ && tolower($0) ~ /paging/'

0
Reply Bill 5/22/2010 1:02:01 AM

18 Replies
1032 Views

(page loaded in 0.4 seconds)

Similiar Articles:


















7/27/2012 9:56:32 AM


Reply: