简体   繁体   English

在文件中搜索模式并在jruby中打印

[英]Search for a pattern in a file and print it in jruby

I have a file with n number of lines. 我有一个n行的文件。 I need to search for a field( SessionId and its value ) in it and print its corresponding value. 我需要在其中搜索一个字段( SessionId及其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? 请帮助我将此代码添加到什么内容?

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 高温超导

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM