今度は前
あんまり需要無いとは思うが、なんとなく作ってみた。
aaa.txtの中から文字列をgrep。
grep_word = (ARGV[0]) out_num = (ARGV[1]).to_i out_lines = Array.new(out_num) match_num = 0 File.open('aaa.txt', 'r').each_with_index do |line, i| #マッチング処理 if line =~ /#{grep_word}/ match_num = i break else #マッチング以前を保持 out_lines.shift out_lines << line end end out_lines.each { |x| puts x}
aaa.txt
0aaaxxxxxxxxxxxxxxx 1bbbxxxxxxxxxxxxxxx 2cccxxxxxxxxxxxxxxx 3dddxxxxxxxxxxxxxxx 4eeexxxxxxxxxxxxxxx 5fffxxxxxxxxxxxxxxx
結果
./grep_before.rb ddd 2 1bbbxxxxxxxxxxxxxxx 2cccxxxxxxxxxxxxxxx