regex - Extracting specific fields from Data in Ruby -


this ruby program in have extract specific fields using regular expression data in file. data in file in following format:

nov 13 01:46:57 10.232.47.76 qas-adaptiveip-10-232-47-76 2015-11-13 01:46:57 +0000 [info]: qas-296d1fa95fd0ac5a84ea73234c0c48d64f6ea22d has been deregistered adap_tdagt

i need extract following values 1)2015-11-13 01:46:57 +0000 2)qas-296d1fa95fd0ac5a84ea73234c0c48d64f6ea22d

i have written code it's not working properly. can please me out problem.

  class task5   def initialize   #   @f=file.open('c:/users/aroraku/desktop,boc-adap_td-agent.log-2.log',r)   @count=0   end    def check_line(line)       if(line=~/deregistered adap_tdagt$/)            line=~ (/.*(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} +\d{4})/)                puts "#{$1}"       end   end    def file_read      open("boc-adap_td-agent.log-2.log") { |f|           while line=f.gets              check_line(line)           end      }     # return @count   end end 

str = "nov 13 01:46:57 10.232.47.76 qas-adaptiveip-10-232-47-76 2015-11-13 01:46:57 +0000 [info]: qas-296d1fa95fd0ac5a84ea73234c0c48d64f6ea22d has been deregistered adap_tdagt" 

as problem code has been identified, suggest way extract desired information each line:

r = /     (?:                # begin non-capture group       \d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s\+\d{4} # match date string     )                  # end non-capture group     |                  # or     (?:                # begin non-capture group       (?<=\[info\]:\s) # match "[info:] " in positive lookbehind       \s+              # match >= 1 characters other whitespace     )                  # end non-capture group     /x                 # extended/free-spacing regex definition mode  str.scan(r)   #=> ["2015-11-13 01:46:57 +0000", "qas-296d1fa95fd0ac5a84ea73234c0c48d64f6ea22d"]  

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -