in reply to How can I find a line in a RTF file?
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.
UpsateForgot 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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How can I find a line in a RTF file?
by Bethany (Scribe) on Aug 08, 2014 at 02:31 UTC | |
Re^2: How can I find a line in a RTF file?
by Anonymous Monk on Aug 08, 2014 at 11:07 UTC | |
by thanos1983 (Parson) on Aug 08, 2014 at 19:36 UTC |