Linux - NewbieThis 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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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?
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.
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 05:49 PM.
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?
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.