【问题标题】:Search for a pattern in a file and print it in jruby在文件中搜索模式并在 jruby 中打印
【发布时间】:2014-04-25 06:44:43
【问题描述】:

我有一个包含 n 行的文件。我需要在其中搜索一个字段(SessionId及其值)并打印其对应的值。

我已编写代码以访问该文件并逐行打印。在获取字段并打印其值方面需要帮助。

文件示例:

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

注意:该文件在指定字段的上方和下方有很多行

代码:

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

请帮助我了解要添加到此代码中的内容吗?

【问题讨论】:

    标签: jruby jrubyonrails jruby-java-interop


    【解决方案1】:
    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
    

    实际上没有必要循环遍历这些行。您可以只阅读文件并使用正则表达式来查找所需的文本。但无论什么适合你的猫。如果文件太大,有时读取整个文件可能会非常有问题。

    HTH

    【讨论】:

    • 谢谢,但是当我将以上行添加到我的代码 C:/jruby-1.6.8/lib/ruby/site_ruby/shared/builtin/javasupport/core_ext/object.rb 时,我得到了这个错误: 99 警告:已初始化常量字符串 NoMethodError:未定义的方法“包括?”用于 C:\FCSFinal\latest_Automation\Proj\file.rb:16 的 nil:NilClass (root)
    • 我对@9​​87654322@不太熟悉,但是你能发布完整的代码吗?只需编辑您上面的帖子并复制粘贴您正在使用的代码。
    • 苛刻请找到代码,需要 "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 而 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
    【解决方案2】:

    完成。这行得通!

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-14
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多