[SOLVED] How to search what is returned by grep for multiple patterns?
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!
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.
How to search what is returned by grep for multiple patterns?
Hi, I am new to Linux and the full capabilities of grep, so I'm having some difficulty figuring out how to obtain the information I want. I'm using grep to search if specific servers are listed in my local nameserver like this:
chknsv NSV | egrep 'box1|box2|box3'
Where chknsv is a function to list all items in the nameserver that include substring NSV in the line.
Ideally, what is returned from egrep is a list like this:
$NSV - box1 - 40000 - tcp
$NSV - box2 - 40000 - tcp
$NSV - box3 - 40000 - tcp
(The first column represents Service name, the second Host name, the third Port, and the fourth Protocol)
What I am wondering is how to check what is returned from egrep to make sure that all of the server names I searched for are included? The exit status of the egrep command returns 0 when anything is returned regardless of how many server names I was searching for. So, it would return 0 even if only one or two of the three servers I'm searching for are listed. This kind of scenario is what I'm trying to detect to verify whether or not all of the server names I searched for are active in the nameserver.
I know it can be done with multiple calls to egrep, but that seems really clunky to me - I'm hoping there's a more elegant solution that I'm just unaware of.
Any advice/suggestions would be greatly appreciated! Thanks!
chknsv NSV | egrep 'box' >$MY_CHKNSV
numlines=`wc -l $MY_CHKNSV`
if [ $numlines -ne 3 ]
echo "error: did not get all three boxes"
< do what ever you want to do about this issue, etc >
echo "got all three boxes"
<do what you want now that you've got exactly three boxes returned>
Thank you, Lnthink! That will work perfectly. I wasn't sure how to export the results to a file. Now, I can expand on that and have my script check the number of lines, and if less than 3, find out which server isn't listed.
I'll also keep the chknsv NSV | egrep 'box' for future use. For this, I was providing a generic example. The boxes I'm actually searching for have unique names.