Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > A regexp?

Reply
Thread Tools

A regexp?

 
 
Derek Smith
Guest
Posts: n/a
 
      03-22-2010
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=<(E-Mail Removed)>, 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...0b30748490a5c4c48900e4e9912059c0e18d1d24d d45d1896d5150b1f17551f975b124c9b1b12499b11974d1d44 099f4d135f440c46d305d...)
Mar 21 18:58:08
Mar 21 18:58:08
Mar 21 18:58:08
to=<(E-Mail Removed)>, 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=<(E-Mail Removed)>, 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=<(E-Mail Removed)>, 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/.

 
Reply With Quote
 
 
 
 
brabuhr@gmail.com
Guest
Posts: n/a
 
      03-22-2010
On Sun, Mar 21, 2010 at 10:23 PM, Derek Smith
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Derek Smith
Guest
Posts: n/a
 
      03-23-2010
unknown wrote:
> On Sun, Mar 21, 2010 at 10:23 PM, Derek Smith
> <(E-Mail Removed)> 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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments