LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 04-14-2015, 02:38 PM   #1
legendmac
LQ Newbie
 
Registered: Apr 2015
Location: AZ
Posts: 14

Rep: Reputation: Disabled
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!
 
Old 04-14-2015, 03:03 PM   #2
Lnthink
Member
 
Registered: May 2010
Location: Lafayette, LA
Distribution: Ubuntu, RH, Fedora
Posts: 38

Rep: Reputation: 8
chknsv NSV | egrep 'box1|box2|box3'

Well, not that it's what you're asking, but this could be changed to:

chknsv NSV | egrep 'box[123]'

to get the same result.

If you don't want to run it twice stick the output in a tmp file, and then run "wc" on it.

#!/bin/bash
MY_CHKNSV=/var/tmp/my_chknsv.$$
export MY_CHKNSV

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

rm $MY_CHKNSV
exit 0

Last edited by Lnthink; 04-14-2015 at 03:05 PM.
 
2 members found this post helpful.
Old 04-14-2015, 04:18 PM   #3
schedar
LQ Newbie
 
Registered: Apr 2015
Location: Poland
Distribution: Centos 7
Posts: 16

Rep: Reputation: Disabled
How many servers do you have? Don't answer, just enter into variable

Code:
howmany=3 ; chknsv NSV | [ $(grep -o box[1-3] | sort | uniq | wc -l) -eq $howmany ] ; echo $?

Last edited by schedar; 04-14-2015 at 04:20 PM.
 
1 members found this post helpful.
Old 04-14-2015, 04:48 PM   #4
legendmac
LQ Newbie
 
Registered: Apr 2015
Location: AZ
Posts: 14

Original Poster
Rep: Reputation: Disabled
Smile

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.
 
Old 04-14-2015, 05:05 PM   #5
schedar
LQ Newbie
 
Registered: Apr 2015
Location: Poland
Distribution: Centos 7
Posts: 16

Rep: Reputation: Disabled
Okay, so if you need to have a list it will be much easier:
Code:
chknsv NSV | egrep -o 'name1|name2|name3' | sort | uniq

Last edited by schedar; 04-14-2015 at 05:07 PM.
 
Old 04-15-2015, 01:17 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Once you are talking columns, I would suggest using awk.
 
2 members found this post helpful.
  


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
[SOLVED] Can grep search for multiple patterns from a variable pradeepdee6 Linux - Newbie 10 11-08-2012 05:52 AM
Searching for multiple patterns with grep dznunn Programming 2 06-26-2012 10:52 AM
[SOLVED] Search multiple patterns & print matching patterns instead of whole line Trd300 Linux - Newbie 29 03-05-2012 08:41 PM
Excluding multiple patterns from grep flamingo_l Linux - General 9 01-06-2011 09:05 AM
grep for multiple patterns???? lucastic Linux - Software 4 08-06-2010 07:07 PM


All times are GMT -5. The time now is 02:57 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration