简体   繁体   English

Ruby grep与行号

[英]Ruby grep with line number

What could be the best way of getting the matching lines with the line numbers using Ruby's Enumerable#grep method. 使用Ruby的Enumerable#grep方法获取匹配行与行号的最佳方法是什么? (as we use -n or --line-number switch with grep command). (因为我们使用-n--line-number切换与grep命令)。

Enumerable#grep doesn't let you do that, at least by default. 可枚举#grep不允许你这样做,至少在默认情况下是这样。 Instead, I came up with: 相反,我想出了:

text = 'now is the time
for all good men
to come to the aid
of their country'

regex = /aid/

hits = text.lines.with_index(1).inject([]) { |m,i| m << i if (i[0][regex]); m }
hits # => [["to come to the aid\n", 3]]

maybe something like this: 也许是这样的:

module Enumerable
  def lgrep(pattern)
    map.with_index.select{|e,| e =~ pattern}
  end
end

这不是优雅或高效,但为什么不在grepping之前对行进行编号?

You can kludge it in Ruby 1.8.6 like so: 你可以在Ruby 1.8.6中像这样使用它:

require 'enumerator'
class Array
  def grep_with_index(regex)
    self.enum_for(:each_with_index).select {|x,i| x =~ regex}
  end
end
arr = ['Foo', 'Bar', 'Gah']
arr.grep_with_index(/o/) # => [[0, 'Foo']]
arr.grep_with_index(/a/) # => [[1, 'Bar'], [2, 'Gah']]

Or if you're looking for tips on writing a grep-like utility in Ruby. 或者,如果您正在寻找有关在Ruby中编写类似grep的实用程序的技巧。 Something like this should work: 这样的事情应该有效:

def greplines(filename, regex)
  lineno = 0
  File.open(filename) do |file|
    file.each_line do |line|
      puts "#{lineno += 1}: #{line}" if line =~ regex
    end
  end
end
>> lines=["one", "two", "tests"]
=> ["one", "two", "tests"]
>> lines.grep(/test/){|x| puts "#{lines.index(x)+1}, #{x}" }
3, tests

To mash up the Tin Man's and ghostdog74's answers 混搭Tin Man和ghostdog74的答案

text = 'now is the time
for all good men
to come to the aid
of their country'

regex = /aid/

text.lines.grep(/aid/){|x| puts "#{text.lines.find_index(x)+1}, #{x}" }
# => 3, to come to the aid

Put text in a file 将文本放入文件中

test.log test.log中

 now is the time
 for all good men
 to come to the aid
 of their country

Command line (alternative of grep or awk command ) 命令行(grep或awk命令的替代)

ruby -ne ' puts $_  if $_=~/to the/' test.log

Try this also 试试这个吧

ruby -na -e ' puts $F[2] if $_=~/the/' test.log

Similarly 同样

ruby -na -e ' puts $_.split[2] if $_=~/the/' test.log

This is similar to awk command. 这类似于awk命令。

Another suggestion: 另一个建议是:

lines.find_index{ |l| l=~ regex } lines.find_index{ |l| l=~ regex } . lines.find_index{ |l| l=~ regex }

A modification to the solution given by the Tin Man. 锡人给出的解决方案的修改。 This snippet will return a hash having line numbers as keys, and matching lines as values. 此代码段将返回一个散列,其中行号作为键,匹配行作为值。 This one also works in ruby 1.8.7. 这个也适用于红宝石1.8.7。

text = 'now is the time
for all good men
to come to the aid
of their country'

regex = /aid/


hits = text.lines.each_with_index.inject({}) { |m, i| m.merge!({(i[1]+1) => i[0].chomp}) if (i[0][regex]); m}

hits #=> {3=>"to come to the aid"} 

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

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