Reading RTF + Text File and pattern matching

Discussion in 'Ruby' started by Sukhwinder Tambar, Jan 12, 2009.

  1. 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!(/<TodayDate>/, Date.today) # Pattern (
    <TodayDate> )
    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

    Attachments:
    http://www.ruby-forum.com/attachment/3149/pattern_matching_solution.txt

    --
    Posted via http://www.ruby-forum.com/.
     
    Sukhwinder Tambar, Jan 12, 2009
    #1
    1. Advertising

  2. Or, in little shorter way:

    data = File.readlines("test1.txt") #read file into array
    data.map! {|line| line.gsub(/world/, "ruby")} #invoke on each line gsub
    File.open("test2.txt", "a") {|f| f.puts data} #output data to other file

    Although I don't understand, why would you want to append instead of
    write?


    Sukhwinder Tambar wrote:
    > Reading RTF and Text File in Ruby and Pattern Matching/searching from
    > file and replacing with your data....


    --
    Posted via http://www.ruby-forum.com/.
     
    Jarmo Pertman, Jan 13, 2009
    #2
    1. Advertising

  3. Thanks Jarmo Pertman for the little way to do this. i am using append
    mode bcz i have lot of Pattern need to be replace with my date
    dynamically. so i open the file in append mode inside the loop.

    One more thing. in text file gsub find the required pattern easily but
    while reading from RTF file we get the data in other way. so i did this

    line= line.gsub!(/<TodayDate>/, Date.today)

    i am agree with map you used.

    bye the thank for shorter the code.
    --
    Posted via http://www.ruby-forum.com/.
     
    Sukhwinder Tambar, Jan 15, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    4
    Views:
    629
    Aaron Sherman
    Feb 11, 2005
  2. Tony
    Replies:
    2
    Views:
    312
  3. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    251
    Marc Bissonnette
    Jan 13, 2004
  4. Bobby Chamness
    Replies:
    2
    Views:
    246
    Xicheng Jia
    May 3, 2007
  5. nani
    Replies:
    2
    Views:
    174
    comp.llang.perl.moderated
    Mar 14, 2008
Loading...

Share This Page