开发者

Parse a particular number of lines

I'm trying to read through a file, find a certain pattern and then grabbing a set number of lines of text after the line that contains that开发者_C百科 pattern. Not really sure how to approach this.


If you want the n number of lines after the line matching pattern in the file filename:

lines = File.open(filename) do |file|
  line = file.readline until line =~ /pattern/ || file.eof;
  file.eof ? nil : (1..n).map { file.eof ? nil : file.readline }.compact
end

This should handle all cases, like the pattern not present in the file (returns nil) or there being less than n lines after the matching lines (the resulting array containing the last lines of the file.)


First parse the file into lines. Open, read, split on the line break

lines = File.open(file_name).read.split("\n")

Then get index

index = line.index{|x| x.match(/regex_pattern/)}

Where regex_pattern is the pattern that you are looking for. Use the index as a starting point and then the second argument is the number of lines (in this case 5)

lines[index, 5]

It will return an array of 'lines'

You could combine it a bit more to reduce the number of lines. but I was attempting to keep it readable.


If you're not tied to Ruby, grep -A 12 trivet will show the 12 lines after any line with trivet in it. Any regex will work in place of "trivet"


matched = false;
num = 0;
res = "";

new File(filename).each_line { |line|
    if (matched) {
        res += line+"\n";
        num++;
        if (num == num_lines_desired) {
            break;
        }
    } elsif (line.match(/regex/)) {
        matched = true;
    }

}

This has the advantage of not needing to read the whole file in the event of a match.

When done, res will hold the desired lines.


in rails (only difference is how I generate the file object)

file = File.open(File.join(Rails.root, 'lib', 'file.json'))

 #convert file into an array of strings, with \n as the separator

 line_ary = file.readlines
 line_count = line_ary.count

 i = 0
  #or however far up the document you want to be...you can get very fancy with this or just do it manually

 hsh = {} 
 line_count.times do |l|
    child_id = JSON.parse(line_ary[i])
    i += 1
    parent_ary = JSON.parse(line_ary[i])
    i += 1
    hsh[child_id] = parent_ary
  end

haha I've said too much that should definitely get you started

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜