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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-17-2005, 03:03 PM
|
#1
|
LQ Newbie
Registered: Mar 2005
Posts: 16
Rep:
|
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!
|
|
|
07-17-2005, 03:25 PM
|
#2
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
egrep -e "BGP.*1\.1\.1\.1" filename
|
|
|
07-17-2005, 04:05 PM
|
#3
|
LQ Newbie
Registered: Mar 2005
Posts: 16
Original Poster
Rep:
|
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.
|
|
|
07-17-2005, 04:48 PM
|
#4
|
Member
Registered: May 2005
Posts: 378
Rep:
|
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.
|
|
|
07-17-2005, 04:59 PM
|
#5
|
Member
Registered: Jun 2005
Posts: 96
Rep:
|
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
|
|
|
07-17-2005, 06:02 PM
|
#6
|
LQ Newbie
Registered: Mar 2005
Posts: 16
Original Poster
Rep:
|
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?
|
|
|
07-17-2005, 06:24 PM
|
#7
|
Member
Registered: Jun 2005
Posts: 96
Rep:
|
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
|
|
|
07-17-2005, 06:32 PM
|
#8
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Not lazy enough ;}
Code:
awk '/BGP/ && /1.1.1.1/' filename
|
|
|
07-17-2005, 06:53 PM
|
#9
|
Member
Registered: Jun 2005
Posts: 96
Rep:
|
it never is Tinkster, it never is
|
|
|
04-09-2010, 04:23 PM
|
#10
|
LQ Newbie
Registered: Apr 2010
Posts: 1
Rep:
|
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 &&
|
|
|
04-09-2010, 08:33 PM
|
#11
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735
Rep:
|
Hi.]
Sigh. I didn't notice until after this post that this was such an old resurrected thread.
Quote:
Originally Posted by boozer_2
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 )
|
|
|
04-10-2010, 01:19 AM
|
#12
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017
|
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
|
|
|
All times are GMT -5. The time now is 12:41 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|