From: Derek Smith on 21 Mar 2010 22:23 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=<colossus1821(a)juno.com>, 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=<scottw(a)webav.com>, 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=<stevew(a)webav.com>, 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=<ssams(a)phoenixformations.com>, 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/.
From: brabuhr on 21 Mar 2010 23:04 On Sun, Mar 21, 2010 at 10:23 PM, Derek Smith <derekbellnersmith(a)yahoo.com> 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. Here is my code: >... > ##p(line.chomp.scan(/^(.{15})\s+(\S+)\s+(\S+)\s+(.+)$/).first) > datestr = (line.chomp.scan(/^.{15}/)) > mailmsg = > (line.chomp.scan(/^.?connect\sto.*|to=.*|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=|connect\sto.*|host.* I stuck your regex and data in rubular,check it out: http://www.rubular.com/r/zhcQ1lccgp
From: Derek Smith on 22 Mar 2010 20:10 unknown wrote: > On Sun, Mar 21, 2010 at 10:23 PM, Derek Smith > <derekbellnersmith(a)yahoo.com> 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/.
|
Pages: 1 Prev: Reading contents of all files from a Directory Next: IPv6 multicast server/client |