0

I have a file with n number of lines. I need to search for a field(SessionId and its value) in it and print its corresponding value.

I have coded to access the file and print it line by line. Need help in getting a field and printing its value.

Sample lines of file:

LastSessionTeardown.cc|598|Resolving:=N2BBSessionGateway.Factory
2013-12-23 06:03:22.488046 UTC VZ_QIP_S3_208 LastSessionTeardown.cc|636 <ERROR>:Failed     to resolve SessionGateway:N2BBSessionGateway.Factory
Cause : user exception, ID 'IDL:omg.org/CosNaming/NamingContext/NotFound:1.0'
2013-12-23 06:03:22.488078 UTC VZ_QIP_S3_208 LastSessionTeardown.cc|640|Total resolved     SessionGateways list(size):=0
2013-12-23 06:03:22.488098 UTC VZ_QIP_S3_208 LastSessionTeardown.cc|642|Out     resolveSGWs::
2013-12-23 06:07:17.485959 UTC VZ_QIP_S3_208 StreamServiceMasterImpl.cc|989|In     createStream::=77D23ECC4649571A367E9C314C4AA7AA
2013-12-23 06:07:17.487706 UTC VZ_QIP_S3_208 StreamServiceMasterImpl.cc|1036|StreamId:     77D23ECC4649571A367E9C314C4AA7AA  **SessionId: C0A800F0DB2A::1387778933::1501** ContentId: vault22_12.mpg 1xGoid: 
2013-12-23 06:07:17.505233 UTC VZ_QIP_S3_208 StreamServiceMasterImpl.cc|989|In     createStream::=E30CC868325B51D288A8E2D95322B840

Note : the file has lots of lines above and below the field specified

Code:

require "java"

include_class "java.io.BufferedReader"
include_class "java.io.FileReader"
include_class "java.lang.String"

fileReader = FileReader.new "protocoltiming.log.txt"

bufferReader = BufferedReader.new fileReader
str = bufferReader.readLine

while str
puts str.to_s
str = bufferReader.readLine
 end

Kindly help me on what to be added to this code?

Anders Ekdahl
  • 22,685
  • 4
  • 70
  • 59
karthick23
  • 1,313
  • 1
  • 8
  • 15

2 Answers2

0
while str
  str = bufferReader.readLine
  session_id = str.strip.scan(/SessionId: (.+)\s/)[0][0] if str.include?("SessionId: ")
  if session_id
    # session_id found
  else
    # session_id not found
  end
end

There is actually no need to loop through the lines. You can just read the file and use regular-expression to find required text. But whatever suits your cat. Sometimes reading whole file could be really problematic if it is too big.

HTH

Harsh Gupta
  • 4,348
  • 2
  • 25
  • 30
  • Thanks but i get this error wen i add the above lines to my code C:/jruby-1.6.8/lib/ruby/site_ruby/shared/builtin/javasupport/core_ext/object.rb:99 warning: already initialized constant String NoMethodError: undefined method `include?' for nil:NilClass (root) at C:\FCSFinal\latest_Automation\Proj\file.rb:16 – karthick23 Mar 19 '14 at 07:49
  • I am not too familiar with `jRuby`, but could you post complete code? Just edit your above post and copy-paste the code you are using. – Harsh Gupta Mar 19 '14 at 09:22
  • Harsh pls find the code, require "java" include_class "java.io.BufferedReader" include_class "java.io.FileReader" include_class "java.lang.String" fileReader = FileReader.new "StreamService.log" bufferReader = BufferedReader.new fileReader str = bufferReader.readLine #puts str.to_s while str str = bufferReader.readLine session_id = str.strip.scan(/SessionId: (.+)\s/)[0][0] if str.include?("SessionId: ") if session_id # session_id found else # session_id not found end end – karthick23 Mar 20 '14 at 06:12
0

done. this works!

require "java"

include_class "java.io.BufferedReader"
include_class "java.io.FileReader"
include_class "java.lang.String"

fileReader = FileReader.new "StreamService.log"

bufferReader = BufferedReader.new fileReader

#puts str
str = bufferReader.readLine

while str = bufferReader.readLine
#puts str.to_s

sessionid = ""

  sessionid = str.match(/SessionId:(.*)ContentId/)

  if sessionid.to_s != ''
    puts "Session ID = #{sessionid[1]}"
  end

end
karthick23
  • 1,313
  • 1
  • 8
  • 15