简体   繁体   English

Ruby在文本文件中找到一个字符串,然后输出在其上找到的行

[英]Ruby find a string in a text file and output the line it's found on

I need to add the functionality to output the entire line a string is found on. 我需要添加功能以输出在​​其上找到字符串的整行。 So here's the code I have working so far. 所以这是到目前为止我正在使用的代码。

if type == "asa"
    if File.readlines(file).grep(/http server enabled/).any?
        $httpserver_failures.push(file)
        out.puts "FAILED: does have http enabled"
    else
        $httpserver_passes.push(file)
        out.puts "PASSED: does not have http enabled"
    end
elsif type == "ios"
    if File.readlines(file).grep(/no ip http server/).any?
        $httpserver_failures.push(file)
        out.puts "FAILED: does have http enabled"
    else
        $httpserver_passes.push(file)
        out.puts "PASSED: does not have http enabled"
    end
end

So I just need to add a line to also output the line as it's found. 因此,我只需要添加一行以将找到的行也输出即可。 I just don't know the syntax. 我只是不知道语法。

Thanks 谢谢

grep method takes a block also. grep方法也需要一个块。 Thus I think you could write as below : 因此,我认为您可以这样写:

if type == "asa"
  File.readlines(file).grep(/http server enabled/) do |line|
    unless line.empty?
      $httpserver_failures.push(file)
      out.puts "FAILED: does have http enabled"
      puts line # output the line
    else
      $httpserver_passes.push(file)
      out.puts "PASSED: does not have http enabled"
    end
  end
elsif type == "ios"
  File.readlines(file).grep(/no ip http server/) do |line|
    unless line.empty?
      $httpserver_failures.push(file)
      out.puts "FAILED: does have http enabled"
      puts line # output the line
    else
      $httpserver_passes.push(file)
      out.puts "PASSED: does not have http enabled"
    end
  end
end

暂无
暂无

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

相关问题 如何用在Ruby文本文件中写入的每一行上找到的文件内容替换从控制台获取的URL中找到的特定字符串? - How to replace a particular string found in URL taken from console with file content found on each line written in text file in Ruby? 如何在文本文件中搜索字符串,然后打印/返回/输出在Ruby中作为数字找到的文件的哪一行 - How do I search a text file for a string and then print/return/put out what line of the file it was found on as a number in Ruby 从文本文件中搜索字符串并删除该行RUBY - search a string from a text file and delete that line RUBY 如果使用Ruby找到字符串,如何从$ stdout中搜索特定字符串并将整个$ stdout存储在文本文件中? - how to search particular string from $stdout and store whole $stdout in text file if string is found using Ruby? 如何逐行读取 ruby 中的文本文件(将其托管在 s3 上)? - How do I read line by line a text file in ruby (hosting it on s3)? 在Ruby中逐行比较文件中的字符串 - compare a string in a file line by line in ruby 红宝石输出到文件导致不必要的换行 - ruby output to file is causing unwanted line breaks 在文件中查找特定的文本段,然后在ruby中搜索特定的行 - Find a specific text segment within a file and than search for specific line in ruby Ruby可以打开文件,查找关键字或单词,然后在该行之后写文本吗? - Can Ruby Open a file, Find a keyword or words then write text after that line? Ruby在文件中找到一行并添加到其下方 - Ruby find a line in a file and add below it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM