LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-17-2005, 03:03 PM   #1
boozer_2
LQ Newbie
 
Registered: Mar 2005
Posts: 16

Rep: Reputation: 0
Question grep/egrep logical AND function?


I know one can use egrep for a logical OR search, but is there a way for a logical AND...

Say i want to search for BGP and 1.1.1.1

I know that:

egrep 'BGP|1.1.1.1' filename

will search for either BGP or 1.1.1.1

But I want them both. I know I can put multiple greps together to perform an AND like this:

cat filename | grep BGP | grep 1.1.1.1

but this seems like unecessary command line use to me. I like to keep things as short as possible. I searched around quite a bit and can't find anything on an AND function. Anyone know how to do it?

Thanks!
 
Old 07-17-2005, 03:25 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
egrep -e "BGP.*1\.1\.1\.1" filename
 
Old 07-17-2005, 04:05 PM   #3
boozer_2
LQ Newbie
 
Registered: Mar 2005
Posts: 16

Original Poster
Rep: Reputation: 0
I need to clarify my question a bit. I need an AND function that will find 'x AND y' OR 'y AND x'. Your solution works as long as x precedes y, but I also want to find y followed by x using the shortest command possible. Is there not just an AND symbol or something like egrep 'x&y' which finds x.*y or y.*x? I figured that since they threw in an symbol for the OR logical function, I assumed there would be a symbol for AND, or similar simple way of performing the AND logical function.
 
Old 07-17-2005, 04:48 PM   #4
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
But I want them both. I know I can put multiple greps together to perform an AND like this:

cat filename | grep BGP | grep 1.1.1.1

but this seems like unecessary command line use to me. I like to keep things as short as possible. I searched around quite a bit and can't find anything on an AND function. Anyone know how to do it?
Your example doesn't show that you like to keep things as short as possible - using cat is pointless here! The shortest way I can think of is:
Code:
grep BGP filename | grep '1\.1\.1\.1'
In a single grep expression you'd have to do something like
Code:
egrep 'BGP.*1\.1\.1\.1|1\.1\.1\.1*BGP' filename
which is a little longer than the double grep above.

Last edited by eddiebaby1023; 07-17-2005 at 04:49 PM.
 
Old 07-17-2005, 04:59 PM   #5
mhallbiai
Member
 
Registered: Jun 2005
Posts: 96

Rep: Reputation: 16
correction to eddiebaby's last snippet, which should work but is missing the . before the 2nd *
Code:
egrep 'BGP.*1\.1\.1\.1|1\.1\.1\.1.*BGP' filename
 
Old 07-17-2005, 06:02 PM   #6
boozer_2
LQ Newbie
 
Registered: Mar 2005
Posts: 16

Original Poster
Rep: Reputation: 0
eddiebaby1023, I do like to keep things as short as possible, but I am fairly new to this as well. The way I was shown was to use cat when piping multiple greps, thank you for pointing out it is not necessary. That will definitely help me shorten things up.

Also, thanks for the examples. They work well with 2 variables. However, what would you do if the number of variables were increased?

For example, lets say you have a text log from thousands of hosts. You want to find a log entry that contains 15 hostnames, and the names could be in any order.
Is the only way to find that entry to pipe 15 greps together?
 
Old 07-17-2005, 06:24 PM   #7
mhallbiai
Member
 
Registered: Jun 2005
Posts: 96

Rep: Reputation: 16
boozer_2,
you may want to look into something a little more robust.
might i suggest (g)awk
Code:
cat filename | awk '/BGP/ && /1.1.1.1/'
cat filename | awk '/hostname1/ && /hostname2/ && /hostname3/ && ... /hostname14/ && /hostname15/'
rather than write hostname1-15, you get the idea
the second example would present only lines that contained all 15 hostnames (strings), not sure what logs you would be looking at that would have 15 hostnames on a single line but there it is

this will give you the logical ANDing that you were seeking


hope this helps
 
Old 07-17-2005, 06:32 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Not lazy enough ;}

Code:
awk '/BGP/ && /1.1.1.1/' filename
 
Old 07-17-2005, 06:53 PM   #9
mhallbiai
Member
 
Registered: Jun 2005
Posts: 96

Rep: Reputation: 16
it never is Tinkster, it never is
 
Old 04-09-2010, 04:23 PM   #10
mikeuguru
LQ Newbie
 
Registered: Apr 2010
Posts: 1

Rep: Reputation: 0
Cool

I believe you are looking for:

awk '/BGP/ || /1.1.1.1/' filename


The other one will only give you lines where both are, this way it would be more like grep with a logical &&
 
Old 04-09-2010, 08:33 PM   #11
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.]

Sigh. I didn't notice until after this post that this was such an old resurrected thread.

Quote:
Originally Posted by boozer_2 View Post
But I want them both. ...
You may be interested in:
Code:
NAME
       glark - Search text files for complex regular expressions

SYNOPSIS
       glark [options] expression file ...

DESCRIPTION
       Similar to "grep", "glark" offers: Perl-compatible regular expressions,
       color highlighting of matches, context around matches, complex expres-
       sions ("and" and "or"), grep output emulation, and automatic exclusion
       of non-text files. Its regular expressions should be familiar to per-
       sons experienced in Perl, Python, or Ruby. File may also be a list of
       files in the form of a path.
It was in the Debian repository that I use, but if not in yours, see
http://www.incava.org/projects/glark/

Best wishes ... cheers, makyo

Last edited by makyo; 04-09-2010 at 08:36 PM. Reason: ( Edit 1: note about age of thread )
 
Old 04-10-2010, 01:19 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017

Rep: Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196Reputation: 3196
I believe this needs a slight adjustment:

Code:
awk '/BGP/ && /1\.1\.1\.1/' filename
Otherwise you will match lines like BGP <blah> 1213141 which is not what the OP wants

Quote:
I believe you are looking for:

awk '/BGP/ || /1.1.1.1/' filename


The other one will only give you lines where both are, this way it would be more like grep with a logical &&
Not sure how you get logical && out of this as it would return lines with only BGP on them which is not && and not
what OP requested
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem matching strings with grep/egrep Seb74 Linux - Newbie 5 05-26-2005 01:40 PM
Help with egrep smart_sagittari Linux - Newbie 2 05-02-2005 08:18 AM
Using Grep and Egrep linux-nerd Linux - General 5 10-10-2004 11:37 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM
Using egrep Barbarian Programming 5 10-20-2002 02:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration