ruby 检查文件是否包含字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8408388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:34:20  来源:igfitidea点击:

Check if file contains string

rubyif-statement

提问by Danny

So I found this question on here, but I'm having an issue with the output and how to handle it with an if statement. This is what I have, but it's always saying that it's true even if the word monitor does not exist in the file

所以我在这里找到了这个问题,但是我遇到了输出问题以及如何使用 if 语句处理它。这就是我所拥有的,但它总是说即使文件中不存在monitor这个词也是如此

if File.readlines("testfile.txt").grep(/monitor/)
  do something
end

Should it be something like == "nil"? I'm quite new to ruby and not sure of what the outputs would be.

它应该是 == "nil" 之类的吗?我对 ruby​​ 很陌生,不确定输出会是什么。

采纳答案by Ed S.

Enumerable#grepdoes not return a boolean; it returns an array (how would you have access to the matches without passing a block otherwise?).

Enumerable#grep不返回布尔值;它返回一个数组(否则如何在不传递块的情况下访问匹配项?)。

If no matches are found it returns an empty array, and []evaluates to true. You'll need to check the size of the array in the ifstatement, i.e.:

如果未找到匹配项,则返回一个空数组,并[]计算为true。您需要检查if语句中数组的大小,即:

if File.readlines("testfile.txt").grep(/monitor/).size > 0
  # do something
end

The documentation should be your first resource for questions like this.

文档应该是您解决此类问题的第一个资源。

回答by the Tin Man

I would use:

我会用:

if File.readlines("testfile.txt").grep(/monitor/).any?

or

或者

if File.readlines("testfile.txt").any?{ |l| l['monitor'] }


Using readlineshas scalability issues though as it reads the entire file into an array. Instead, using foreachwill accomplish the same thing without the scalability problem:

使用readlines具有可扩展性问题,因为它将整个文件读入数组。相反, usingforeach将完成相同的事情而没有可扩展性问题:

if File.foreach("testfile.txt").grep(/monitor/).any?

or

或者

if File.foreach("testfile.txt").any?{ |l| l['monitor'] }

See "Why is "slurping" a file not a good practice?" for more information about the scalability issues.

有关可扩展性问题的更多信息,请参阅“为什么“slurping”文件不是一个好习惯?

回答by steenslag

Grep will give you an array of all found 'monitor's. But you don't want an array, you want a boolean: is there any 'monitor' string in this file? This one reads as little of the file as needed:

Grep 将为您提供所有找到的“监视器”的数组。但是你不想要一个数组,你想要一个布尔值:这个文件中是否有任何“监视器”字符串?这个根据需要读取尽可能少的文件:

if File.open('test.txt').lines.any?{|line| line.include?('monitor')}
  p 'do something'
end

readlinesreads the whole file, linesreturns an enumerator which does it line by line.

readlines读取整个文件,lines返回一个枚举器,它逐行执行。

回答by equivalent8

if anyone is looking for a solution to display last line of a file where that string occurs just do

如果有人正在寻找解决方案来显示该字符串出现的文件的最后一行,请执行

File.readlines('dir/testfile.txt').select{|l| l.match /monitor/}.last 

example

例子

file:

文件:

monitor 1
monitor 2
something else

you'll get

你会得到

monitor 2