+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
*nix Thread, Grep syntax for multiple srtings? in Technical; I am writing something in perl and Im piping the output of a command through grep to search for certain ...
  1. #1
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106

    Grep syntax for multiple srtings?

    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 .

  2. #2

    Geoff's Avatar
    Join Date
    Jun 2005
    Location
    Fylde, Lancs, UK.
    Posts
    11,803
    Thank Post
    110
    Thanked 583 Times in 504 Posts
    Blog Entries
    1
    Rep Power
    224

    Re: Grep syntax for multiple srtings?

    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:

    Code:
    ls | grep 'something|otherthing'
    Although I could of written the above as:

    Code:
    ls | grep '(some|other)thing'
    and got the same answer.

  3. #3
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106

    Re: Grep syntax for multiple srtings?

    What I should have mentioned as well is I only need one of the strings to be found as well.

  4. #4

    Geoff's Avatar
    Join Date
    Jun 2005
    Location
    Fylde, Lancs, UK.
    Posts
    11,803
    Thank Post
    110
    Thanked 583 Times in 504 Posts
    Blog Entries
    1
    Rep Power
    224

    Re: Grep syntax for multiple srtings?

    Then your asking for grep to do something silly. Which it wont let you.

  5. #5
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106

    Re: Grep syntax for multiple srtings?

    Ok so what are my alternatives?

  6. #6


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339

    Re: Grep syntax for multiple srtings?

    ls | grep 'something|otherthing'
    thought this would only work with egrep, not with grep.
    you got egrep aliased to grep ?

  7. #7


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339

    Re: Grep syntax for multiple srtings?

    Code:
    egrep 'onething|anotherthing'
    is like doing an OR.

    you can also pipe one grep to another

    Code:
    grep onething | grep anotherthing
    to get an AND

  8. #8
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106

    Re: Grep syntax for multiple srtings?

    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 .

  9. #9

    Geoff's Avatar
    Join Date
    Jun 2005
    Location
    Fylde, Lancs, UK.
    Posts
    11,803
    Thank Post
    110
    Thanked 583 Times in 504 Posts
    Blog Entries
    1
    Rep Power
    224

    Re: Grep syntax for multiple srtings?

    Just snmpwalk the OID's your interested in. Not the entire thing.

  10. #10
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106

    Re: Grep syntax for multiple srtings?

    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.

  11. #11

    Geoff's Avatar
    Join Date
    Jun 2005
    Location
    Fylde, Lancs, UK.
    Posts
    11,803
    Thank Post
    110
    Thanked 583 Times in 504 Posts
    Blog Entries
    1
    Rep Power
    224

    Re: Grep syntax for multiple srtings?

    What about caching the snmp output and parsing the cached copy?

  12. #12
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106

    Re: Grep syntax for multiple srtings?

    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.

  13. #13


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,463
    Thank Post
    866
    Thanked 845 Times in 667 Posts
    Rep Power
    195

    Re: Grep syntax for multiple srtings?

    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.

  14. #14


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,463
    Thank Post
    866
    Thanked 845 Times in 667 Posts
    Rep Power
    195

    Re: Grep syntax for multiple srtings?

    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

  15. #15
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,999
    Thank Post
    120
    Thanked 280 Times in 258 Posts
    Rep Power
    106

    Re: Grep syntax for multiple srtings?

    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

    Code:
    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

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Vista multiple licences
    By xfinity in forum Windows Vista
    Replies: 4
    Last Post: 17th January 2008, 06:24 PM
  2. Multiple MAC Address
    By Nij.UK in forum Hardware
    Replies: 13
    Last Post: 2nd May 2007, 08:01 AM
  3. multiple NIC boot cd for ghost 8.x
    By angelsea in forum General Chat
    Replies: 2
    Last Post: 27th April 2007, 08:26 PM
  4. Multiple monitors
    By netadmin in forum Windows
    Replies: 9
    Last Post: 25th January 2007, 12:41 AM
  5. multiple vnc viewer
    By timbo343 in forum Windows
    Replies: 20
    Last Post: 11th January 2007, 11:33 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •