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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to match a value and print the subsequent lines to output

Status
Not open for further replies.

euni

MIS
Jan 5, 2005
4
US
Hello,

I am noob to Perl Programming. Here's what I need to do. I need to match a string value from a file and upon matching the value, I need the remainder of the file to be printed to an output.

For example: Here's the File I'll be reading:

Hilton also told Superior Court Judge Michael T. Sauer she was trying to take more responsibility for her life. This, after Hilton essentially laid the blame for her troubles on longtime publicist Elliot Mintz, whom she said told her the license was only suspended for 30 days, through last December, while it had actually been suspended into March.


Value to match = Elliot

Reqd Output:

Mintz, whom she said told her the license was only suspended for 30 days, through last December, while it had actually been suspended into March.


Here's the code i have so far:

open (FILE, "$infile") or die "Cannot Open Log File!\n";
open (DAT,">$outfile") || die("Cannot Open File");

while (@variable = <FILE>) {
for (@variable =~ m/Elliot/) {
@Stringvar = "@variable";
@Stringvars = split(m/Elliot/,@Stringvar);
# write to the data file
print DAT "$IOSvars[0]\n";
}
}
close DAT;
close FILE;
 
Code:
open (FILE, "$infile") or die "Cannot Open Log File!\n";
open (DAT,">$outfile") || die("Cannot Open File");

while (my $variable = <FILE>) {
   if ($variable =~ m/\b(Elliot\b.*)/) {
      print DAT $1;
      print DAT <FILE>;
   }
}
close DAT;
close FILE;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks a lot! That works like a charm. :)
 
is this school/class work?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top