Reading RTF and Text File in Ruby and Pattern Matching/searching from
file and replacing with your data.
if you want to read RTF of Text file then u can take help from this code
. Try out following solution.
filename =‘sukhitambar.rtf’
targetfilename =‘princetambar.rtf’
File.open(filename, ‘r’) do |f| # open file for reading
lines = f.readlines # read into array of lines
lines.each do |line| # traversing line by line
line= line.gsub!(//, Date.today) # Pattern (
)
or
line= line.gsub!(/<…>/,data to be replaced)
File.open(targetfilename, ‘a’) do |content| # opening new
target file in append mode
content.puts line # saving the line to another
rtf file
end
end
end
In the above code user can read data from Rich Text Format (RTF) file
and search for some specific pattern and replace with his required
data.and saved it to another RTF file. i hope this code will help for
those whose want to read from RTF file and do pattern matching.
Thanks
By :-
Sukhwinder Singh Tambar