lines=File.open(“hi.txt”,“r”)
output=[]
lines.each do |single|
out1=single.split(’ ‘)
out2=out1[0].gsub(’‘, ‘’)
out3= out2.gsub(’‘, ‘’)
out4=out3.gsub(’‘,’')
output << out4 # I need to overwrite the appropriate line.
end
Here i want to replace the first line in hi.txt file 1 to 2.
You can’t replace “lines” in a file because file on disk doesn’t know
about
lines.
You can read one file, filter its content, write into another file,
close
first file, close second file, delete first file or rename to backup,
rename
second file to first file.
This would be the easiest way.
or
You read complete file into memory, filter there, seek to file start,
write
contents from memory, resize.
More dangerous on errors.
or
You replace characters of these tags by spaces directly in the file, may
be
possible but needs more repositioning in file.
I also would recommend to analyze the XML-tree instead of simply
removing
the tags wherever found.
Use, for example, “rexml” as contained in Ruby 1.8.
You can read one file, filter its content, write into another file,
close
first file, close second file, delete first file or rename to backup,
rename
second file to first file.
This would be the easiest way.