我让它在两条线上工作。
代码示例
$max_lines_in_log=55
$cron_log = "C:/EduTester/cron/rufus.log"
array = File.read($cron_log).split("\n")[-$max_lines_in_log,$max_lines_in_log]
open($cron_log, 'w') { |f| f.puts array.join("\n") }
答案 0 :(得分:1)
`tail -n #{$max_lines_in_log} #{$cron_log} > #{$cron_log}`
答案 1 :(得分:1)
我讨厌用一行写它,但这就是你所问的:
File.open("o_f.log", "w") { |o_f| o_f.write File.open("i_f.log") { |i_f| i_f.each_line.each_cons(55).inject{ |a, e| e } } }
如果nlines < 55
不起作用,那么版本会更好:
File.open("o_f.log", "w") { |o_f| o_f.write File.open("i_f.log") { |i_f| i_f.each_line.inject([]){ |ls, l| ls.shift(54).unshift(l) }.reverse } }
答案 2 :(得分:1)
File.open(output, 'w'){|out| out.puts File.open(input).readlines[-55,55]}
答案 3 :(得分:0)
buff=[]
IO.foreach('c:/test.txt') { |line|
if buff.length < n
buff << line
else
buff.shift
buff << line
end
}
result = buff.join