A regexp?

D

Derek Smith

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=<[email protected]>, 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=<[email protected]>, 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=<[email protected]>, 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=<[email protected]>, 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)
 
B

brabuhr

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top