Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple question

Status
Not open for further replies.

cnw40007

Technical User
Mar 4, 2003
35
IE
Hello,
How would i go about matching a word from a file and taking out the word and everything below it until it reaches a blank line and put it into another file in perl???
 
I just hard-coded a string to search and printed the output to terminal, but you can just get and put thing things from and into files instead.
Code:
use strict;
use warnings;

my $text = qq(This is the first line
and another

above me
the line below me is blank

end);

my $word = 'the';

while($text =~ 
/     #start regex
(?:   #start group and don't remember it
\W|\A #non-word character \W or start of string \A
)     #end group
(     #start new group (and remember it)
$word #search for the special word
\W    #followed by a non-word character
.*?   #and a minimalist matching of zero or more of any chars
)     #end group
\n\n  #followed by a blank newline (two in a row)
/sgx  #end regex and have modifiers
      #s treats the string as one long string and not as separate lines
      #it allows the . to match a newline
      #g searches globally, so turn this off if you want only the first match
      #x allows comments and ignores spacing, letting the regex span multiple lines
)     #end while condition
{
	print "Match: $1\n"; #$1 stores the match from $word to \n\n
}
----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Here's a line by line version that reads in a file passed from the command line.
Code:
# open OUT fh to output file
my $word = 'test'; #Define word here
while (<>) {
    if (s/^.*?(\b$word\b)/$1/../^\s*$/) {
        unless (/^\s*$/) {
            $rec .= $_;
        }
        else {
            print OUT $rec;
            $rec = '';
        }
    }
}
jaa
 
I tried both examples.with the first one i don't know where the end of the paragraph will be, so i couldn't get the program to run and the second example i ran it but it didn't do anything.Basically the way my program works is it matchs for a certain email address and if it is found it will delete only that email from the inbox and leave the rest alone. Any more ideas??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top