A regexp?

Discussion in 'Ruby' started by Derek Smith, Mar 22, 2010.

  1. Derek Smith

    Derek Smith Guest

    Hi All!

    Thank you in advance! :)

    I am parsing through /var/log/mail.log via regexp and I need to skip
    date lines that are NOT followed by to|from|connect. Here is my code:

    mailog = "/var/log/mail.log"

    File.open(mailog, 'r+') do |mlog|
    if mlog.flock(File::LOCK_SH) == 0
    File.foreach mailog do |line|
    ##p(line.chomp.scan(/^(.{15})\s+(\S+)\s+(\S+)\s+(.+)$/).first)
    datestr = (line.chomp.scan(/^.{15}/))
    mailmsg =
    (line.chomp.scan(/^.?connect\sto.*|to=.*|host.*$/))
    puts datestr
    puts mailmsg
    end
    else
    string = 'LOCK_SH was not obtained on /var/log/maillog!'
    end ### END if ###

    mlog.flock(File::LOCK_UN)

    end ### END do mlog ###

    The output of this code is below and I DO NOT want, can skip the output
    such as
    Mar 21 18:58:08
    Mar 21 18:58:08
    Mar 21 18:58:08

    I only want ouput IF the date is followed by to=|connect\sto.*|host.*


    to=<>, relay=mx.dca.untd.com[64.136.44.37]:25,
    delay=177323, delays=177322/0.02/1.8/0, dsn=4.0.0, status=deferred (host
    mx.dca.untd.com[64.136.44.37] refused to talk to me: 550 Access
    denied...0b30748490a5c4c48900e4e9912059c0e18d1d24dd45d1896d5150b1f17551f975b124c9b1b12499b11974d1d44099f4d135f440c46d305d...)
    Mar 21 18:58:08
    Mar 21 18:58:08
    Mar 21 18:58:08
    to=<>, relay=none, delay=187566,
    delays=187536/0.05/30/0, dsn=4.4.1, status=deferred (connect to
    webav.com[74.117.116.83]:25: Operation timed out)
    Mar 21 18:58:08
    to=<>, relay=none, delay=187566,
    delays=187535/0.02/30/0, dsn=4.4.1, status=deferred (connect to
    webav.com[74.117.116.83]:25: Operation timed out)
    Mar 21 18:58:08
    Mar 21 18:58:08
    to=<>, relay=none, delay=187576,
    delays=187546/0.02/30/0, dsn=4.4.1, status=deferred (connect to
    mail.phoenixformations.com[69.61.147.50]:25: Operation timed out)
    --
    Posted via http://www.ruby-forum.com/.
     
    Derek Smith, Mar 22, 2010
    #1
    1. Advertising

  2. Derek Smith

    Guest

    On Sun, Mar 21, 2010 at 10:23 PM, Derek Smith
    <> wrote:
    > I am parsing through /var/log/mail.log via regexp and I need to skip
    > date lines that are NOT followed by to|from|connect. =A0Here is my code:
    >...
    > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0##p(line.chomp.scan(/^(.{15})\s+(\S+)\s+(\=

    S+)\s+(.+)$/).first)
    > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0datestr =3D (line.chomp.scan(/^.{15}/))
    > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0mailmsg =3D
    > (line.chomp.scan(/^.?connect\sto.*|to=3D.*|host.*$/))
    >...
    > The output of this code is below and I DO NOT want, can skip the output
    > such as
    > Mar 21 18:58:08
    > Mar 21 18:58:08
    > Mar 21 18:58:08
    >...
    > I only want ouput IF the date is followed by to=3D|connect\sto.*|host.*


    I stuck your regex and data in rubular,check it out:

    http://www.rubular.com/r/zhcQ1lccgp
     
    , Mar 22, 2010
    #2
    1. Advertising

  3. Derek Smith

    Derek Smith Guest

    unknown wrote:
    > On Sun, Mar 21, 2010 at 10:23 PM, Derek Smith
    > <> wrote:
    >> Mar 21 18:58:08
    >> Mar 21 18:58:08
    >> Mar 21 18:58:08
    >>...
    >> I only want ouput IF the date is followed by to=|connect\sto.*|host.*

    >
    > I stuck your regex and data in rubular,check it out:
    >
    > http://www.rubular.com/r/zhcQ1lccgp



    I don't see your working regexp on this page. I tested the regexp
    labeled Yours:
    ^(.{15})\s+(to|from|connect)(\S+)\s+(\S+)\s+(.+)$

    and it does not work!
    please advise!
    --
    Posted via http://www.ruby-forum.com/.
     
    Derek Smith, Mar 23, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Hurrell
    Replies:
    4
    Views:
    178
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    543
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    405
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    214
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    203
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page