Answer the question
In order to leave comments, you need to log in
How to clear logs with Ruby?
Good afternoon. I’ll make a reservation right away that it’s not even June. There is a gigabyte log file and two questions:
1. How to cut off the last n characters in each line? I write this:
#create a file for data output
clear = File.new("C:/users/x/ruby/clear.txt", "w+") #open
the working file
logs = File.open("C:/users /x/ruby/logs.txt",'r+') #apply logs.each
to each line
do |line|
#trim the last n characters for each line
n.times do line.chop!
#how to write the result to a new file at this moment?
end
end
2. How to search for a mention of a certain word in each line of the logs?
Answer the question
In order to leave comments, you need to log in
For example:
n = 3
clear = 'C:/users/x/ruby/clear.txt'
logs = 'C:/users/x/ruby/logs.txt'
File.open(clear, 'w') do |f|
File.open(logs).each do |line|
f.puts line[0...-n]
end
end
string.include?(substring)
one.
n = 2
p "hello"[0..-1-n]
File.open('Gemfile', 'r') do |f|
f.each_line do |line|
clear.write( line.chomp[0..-1-n] )
end
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question