简体   繁体   English

Ruby grep,匹配并返回

[英]Ruby grep, match and return

Is there anyway to check if a value exist in a file without ALWAYS going through entire file ? 是否总要检查整个文件中是否存在文件中是否存在值?

Currently I used: 目前,我使用了:

if open('file.txt').grep(/value/).length > 0
  puts "match"
else 
  puts "no match"
end

But it's not efficient as I only want to know whether it exists or not. 但这效率不高,因为我只想知道它是否存在。 Really appreciate a solution with grep / others similar one-liner. 非常感谢使用grep /其他类似单线的解决方案。

Please note the "ALWAYS" before down-vote my question 在对我的问题投反对票之前,请注意“总是”

If you want line-by-line comparison using a one-liner: 如果要使用单线逐行比较:

matches = open('file.txt') { |f| f.lines.find { |line| line.include?("value") } }
puts matches ? "yes" : "naaw"

Here's a ruby one-liner that will work from the linux command line to perform a grep on a text file, and stop on first found. 这是一个Ruby红线,可以从Linux命令行运行,以对文本文件执行grep,并在首次找到时停止。

ruby -ne '(puts "first found on line #{$.}"; break) if $_ =~ /regex here/' file.txt

-n gets each line in the file and feeds it to the global variable $_ -n获取文件中的每一行并将其馈送到全局变量$ _

$. $。 is a global variable that stores the current line number 是存储当前行号的全局变量

If you want to find all lines matching the regex, then: 如果要查找与正则表达式匹配的所有行,则:

ruby -ne 'puts "found on line #{$.}" if $_ =~ /regex here/' file.txt

By definition, the only way you can tell if an arbitrary expression exists in a file is by going over the file and looking for it. 根据定义,您可以判断文件中是否存在任意表达式的唯一方法是遍历文件并寻找它。 If you're looking for the first instance, then on average you'll be scanning half the file until you find your expression when it's there. 如果要查找第一个实例,那么平均来说,您将扫描文件的一半,直到找到表达式为止。 If the expression isn't there then you'll have to scan the entire file to figure that out. 如果表达式不存在,那么您必须扫描整个文件以找出答案。

You could implement that in a one-liner by scanning the file line-by-line. 您可以通过一行一行地扫描文件,以单行方式实现该功能。 Use IO.foreach 使用IO.foreach

If you do this often, then you can make the search super efficient by indexing the file first, eg by using Lucene. 如果您经常这样做,则可以通过首先为文件建立索引(例如,使用Lucene)来提高搜索效率。 It's a trade-off - you still have to scan the file, but only once since you save it's content in a more search-friendly data structure. 这是一个折衷方案-您仍然必须扫描文件,但是只扫描一次,因为您将文件的内容保存在对搜索更友好的数据结构中。 However, if you don't access a given file very frequently, it's probably not worth the overhead - implementation, maintenance and extra storage. 但是,如果您不经常访问给定的文件,则可能不值得进行额外的开销-实现,维护和额外的存储。

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

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