在文件中搜索字符串的最好方法是什么?

时间:2022-09-15 20:22:02

The title speaks for itself really. I only want to know if it exists, not where it is. Is there a one liner to achieve this?

标题本身就说明了一切。我只想知道它是否存在,而不是它在哪里。有一个班轮可以达到这个目的吗?

5 个解决方案

#1


41  

File.open(filename).grep(/string/)

This loads the whole file into memory. If you're dealing with large files, prefer slurping them.
That means loading one line at a time, instead of the whole file.

这会将整个文件载入内存。如果你处理的是大文件,那就更喜欢吃。这意味着每次只加载一行,而不是加载整个文件。

File.foreach(filename).grep(/string/)

It's good practice to clean up after yourself rather than letting the garbage collector handle it at some point. This is more important if your program is long-lived and not just some quick script. Using a code block ensures that the File object is closed when the block terminates.

最好的做法是自己清理,而不是让垃圾收集器在某个时候处理它。如果您的程序是长期存在的,而不仅仅是一些快速脚本,那么这一点就更为重要。使用代码块可确保在块终止时关闭文件对象。

File.foreach(filename) do |file|
  file.grep(/string/)
end

#2


10  

grep for foo OR bar OR baz, stolen from ruby1line.txt.

为foo或bar或baz,偷自ruby1line.txt。

$  ruby -pe 'next unless $_ =~ /(foo|bar|baz)/' < file.txt

#3


4  

If your OS has a grep package, you could use a system call:

如果您的操作系统有一个grep包,您可以使用一个系统调用:

system("grep meow cat_sounds.txt")

This will return true if grep returns anything, false if it does not.

如果grep返回任何东西,它将返回true,如果它没有返回false。

If you find yourself on a system with grep, you may find this is the "best" way because Ruby can be slow when it comes to file operations.

如果您发现自己正在使用grep系统,您可能会发现这是“最好的”方法,因为Ruby在文件操作方面可能会比较慢。

#4


3  

Well it seems eed3si9n has the one liner down, here's the longer solution:

看起来eed3si9n有一个线性下降,这是更长的解:

f = File.new("file.txt")
text = f.read
if text =~ /string/ then
#relevant code
end

#5


0  

This reads the file only to the first appearance of 'string' and processes it line by line - not reading the whole file at once.

这将只读取文件到“string”的第一个外观,并逐行处理它——而不是一次读取整个文件。

def file_contains_regexp?(filename,regexp)
  File.foreach(filename) do |line|
    return true if line =~ regexp
  end
  return false
end

#1


41  

File.open(filename).grep(/string/)

This loads the whole file into memory. If you're dealing with large files, prefer slurping them.
That means loading one line at a time, instead of the whole file.

这会将整个文件载入内存。如果你处理的是大文件,那就更喜欢吃。这意味着每次只加载一行,而不是加载整个文件。

File.foreach(filename).grep(/string/)

It's good practice to clean up after yourself rather than letting the garbage collector handle it at some point. This is more important if your program is long-lived and not just some quick script. Using a code block ensures that the File object is closed when the block terminates.

最好的做法是自己清理,而不是让垃圾收集器在某个时候处理它。如果您的程序是长期存在的,而不仅仅是一些快速脚本,那么这一点就更为重要。使用代码块可确保在块终止时关闭文件对象。

File.foreach(filename) do |file|
  file.grep(/string/)
end

#2


10  

grep for foo OR bar OR baz, stolen from ruby1line.txt.

为foo或bar或baz,偷自ruby1line.txt。

$  ruby -pe 'next unless $_ =~ /(foo|bar|baz)/' < file.txt

#3


4  

If your OS has a grep package, you could use a system call:

如果您的操作系统有一个grep包,您可以使用一个系统调用:

system("grep meow cat_sounds.txt")

This will return true if grep returns anything, false if it does not.

如果grep返回任何东西,它将返回true,如果它没有返回false。

If you find yourself on a system with grep, you may find this is the "best" way because Ruby can be slow when it comes to file operations.

如果您发现自己正在使用grep系统,您可能会发现这是“最好的”方法,因为Ruby在文件操作方面可能会比较慢。

#4


3  

Well it seems eed3si9n has the one liner down, here's the longer solution:

看起来eed3si9n有一个线性下降,这是更长的解:

f = File.new("file.txt")
text = f.read
if text =~ /string/ then
#relevant code
end

#5


0  

This reads the file only to the first appearance of 'string' and processes it line by line - not reading the whole file at once.

这将只读取文件到“string”的第一个外观,并逐行处理它——而不是一次读取整个文件。

def file_contains_regexp?(filename,regexp)
  File.foreach(filename) do |line|
    return true if line =~ regexp
  end
  return false
end