Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Grep syntax for multiple srtings?


Recommended Posts

Posted

I am writing something in perl and Im piping the output of a command through grep to search for certain strings. I can search for one string at once but I want to search for any of multiple strings eg

 

somecommand | grep Ready|job requires

 

etc etc I have looked at egrep as well but I am not getting anywhere someone enlighten me please :) .

Posted

Using the pipe symbol is correct. Just beware it might be stolen by the shell rather than grep getting to see it. To that end, supply your pattern to grep within quotes. eg:

 

ls | grep 'something|otherthing'

 

Although I could of written the above as:

 

ls | grep '(some|other)thing'

 

and got the same answer. :)

Posted

Ok let me explain fully what I am trying to do:

I am writing a nagios plugin in perl to monitor my Xerox phaser printers. All the code I need is in place apart from this last bit.

I am doing a snmpwalk on the printer as some values only appear in certain situations, so I cant just do an snmpget.

 

I want to parse the output of the snmpwalk command which I can do with

 

snmpwalk -v1 -c $community $IP|Grep ready

 

to find ready but I want to look for other messages as well without doing more snmp walks that take time to execute.

 

If I have to I can start at a certain point but would rather do it all in one.

 

Suggestions welcomed :) .

Posted

I shall have to start in a certain section like you say and luckily they are all near the end. Thing is though if they were not like that I would be stuck as some of the OIDs seem to get created on the fly to report a certain status.

I am just reading up on

 

You can also evaluate grep in a "list context". If you assign the return value of grep to an array instead of to a scalar, then the array will be filled with the matched elements.

 

@found = grep /a/, ( "ant", "bug", "cat", );

 

would leave the array @found with the values like this:

 

@found = ( "ant", "cat");

Which might be what I need.

Posted

I have dumped it into a variable and have been grepping that using the above method. The only problem is that if it finds what its looking for it dumps the whole thing into the result rather than the line I want. So I need it to just return the OID.

I ahve a backup plan anyway as there is one static OID for the main messages such as ready or processig jobs.

When it needs a different paper type that message turns to a hex string and I need the message a certain range of OIDs that the last number increments everytime eg 2.43.18.1.1.8.1.423, 2.43.18.1.1.8.1.424

Now I can do it that way but iam just concerned when it gets to a certain number it may go to 2.43.18.1.1.8.2.1 I can specify way back in the range to say 2.43.18 so that wont be a problem but it doesnt seem very elegant.

Posted

Do it within the perl? Pick out a peice of each result (eg "job ready") thats extractable by the same regex, then use a hash of fucntion pointers to fire off the requisite messages?

 

Hard to answer without complete visibility on the project though.

Posted

Hang on mate, why are you using grep if you have perl?

To be honest, the big killer here is forking and executing grep. You can do regexes in perl comme ca:

 

if($string =~ m/cat/)

{

print "contained a cat";

}

 

better, you can extract bits:

$string =~ m/status: ([^ ]+)/;

print "status $1\n";

 

in this case, it gets the string of nonspace characters after "status: " and puts em in $1.

 

Finally, a useful while loop uses substitution:

while($string =~ s/cat//)

{

$cat++;

}

will tell you the number of occurrences of "cat", as each one is replaced by the null string, and the while is true until no match is found. does blat the original tho, so use a copy

Posted

You will have to give me an example tom as I only started using perl yesterday ;)

 

If I dump the contents of snmpwalk to a variable the contents will look like this

 

SNMPv2-SMI::mib-2.43.16.5.1.2.1.1 = STRING: "Ready To Print."
SNMPv2-SMI::mib-2.43.18.1.1.1.1.423 = INTEGER: 423
SNMPv2-SMI::mib-2.43.18.1.1.2.1.423 = INTEGER: 3
SNMPv2-SMI::mib-2.43.18.1.1.3.1.423 = INTEGER: 3
SNMPv2-SMI::mib-2.43.18.1.1.4.1.423 = INTEGER: 8
SNMPv2-SMI::mib-2.43.18.1.1.5.1.423 = INTEGER: -1
SNMPv2-SMI::mib-2.43.18.1.1.6.1.423 = INTEGER: 0
SNMPv2-SMI::mib-2.43.18.1.1.7.1.423 = INTEGER: 809
SNMPv2-SMI::mib-2.43.18.1.1.8.1.423 = STRING: "Job requests Letter Paper."
SNMPv2-SMI::mib-2.43.18.1.1.9.1.423 = Timeticks: (109679367) 12 days, 16:39:53.6

 

So I need to pick out the "ready" or "job requests" from it. In the example above both are present but thats because I pasted them both in normally it would be one or the other.

 

Cheers

 

Chris

Posted
Right thanks tom I posted my last post before reading your last one. The reason I was using grep was I used another script as a reference to learn the bits of perl I needed to know. It seems to be doing what I want now pretty much cheers.
Posted

Ok mission accomplished pretty much. I wrote a nagios plugin earlier using similar code to what I need to check HD space on a machine as a test and thats working fine.

Thanks for everyones input and I will finish my Xerox one on monday and maybe put it on nagios exchange :) .

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...