Hello Kevyt,

I am not really familiar with RTF Files but I created a few solutions that I think is exactly what you need.

I have tested them on a file test.rtf that I create. Based on what I see all solutions work fine.

Upsate

Forgot to wright, never forget to use the or die (function) when opening and closing files. They have saved me several times.

Small code modification updates
#!/usr/bin/perl use strict; use warnings; sub regex { my $file = 'test.rtf'; my $substr = 'Date of Last Update:'; open( my $in ,"<", $file) or die "Can not open file: ".$file.": $!\n"; while ( <$in> ) { chomp($_); # Solution: 1 substring # if (index($_, $substr) != -1) { # Solution: 2 regex string{times,} at least string 1s # if( $_ =~ /(?:$substr){1,}/) { # Solution: 3 regex match string or more times: + # if( $_ =~ /(?:$substr)+/ ) { # Solution: 4 quotemeta match string if( $_ =~ /\Q$substr\E/ ) { chop($_); # I use chop to remove the last trailing character ( +}) print $_ . "\n"; push(@_,$_); next; } } # Count the number of signature lines print "I found the string: ".@_." time(s)!\n"; close ($in) or die "Can not close file: ".$file.": $!\n"; } regex(); sub my_grep { # Solution 5: grep in perl!!!! (My favorite) my $file = 'test.rtf'; my $substr = 'Date of Last Update:'; open(my $in ,"<", $file) or die "Can not open file: ".$file.": $!\n"; @_ = <$in>; chomp @_; my @out = grep { $_ =~ /Date of Last Update:/ } @_; print "I found the string: ".@out." time(s)!\n"; close ($in) or die "Can not close file: ".$file.": $!\n"; } # &my_grep();

I hope this solves your problems.

Seeking for Perl wisdom...on the process...not there...yet!

In reply to Re: How can I find a line in a RTF file? by thanos1983
in thread How can I find a line in a RTF file? by kevyt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.