[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!
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.
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.
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[123]' >$MY_CHKNSV
numlines=`wc -l $MY_CHKNSV`
if [ $numlines -ne 3 ]
then
echo "error: did not get all three boxes"
< do what ever you want to do about this issue, etc >
else
echo "got all three boxes"
<do what you want now that you've got exactly three boxes returned>
fi
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[123]' for future use. For this, I was providing a generic example. The boxes I'm actually searching for have unique names.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.