On Jan 14, 5:08 pm, [EMAIL PROTECTED] (Gunnar Hjalmarsson) wrote: > [EMAIL PROTECTED] wrote: > > I'm a nearly absolute beginner to Perl, > > Then this site ought to be useful:http://learn.perl.org/ > > > and a lot of the text manipulation things confuse me. > > Really? Which things specifically, and in what way? > > > I have a large text file with > > information essentially broken into lines like this: > > > findable text with a regexp > > information I care about > > more findable text > > > There are plenty of sections like this in the file. How can I write a > > program that opens the file then searches for the middle line and > > prints it to a new file? > > What have you tried so far? > > Example: > > open my $IN, '<', 'infile.txt' or die $!; > open my $OUT, '>', 'outfile.txt' or die $! > while ( <$IN> ) { > print $OUT scalar <$IN> if /^findable/; > } > > -- > Gunnar Hjalmarsson > Email:http://www.gunnar.cc/cgi-bin/contact.pl
OK here's what I've got: #!/usr/local/bin/perl use strict; use warnings; open (OUT, ">output.txt") or die "Couldn't open output: $!"; open (IN, "input.txt") or die "Couldn't open input: $!"; while (<IN>) { print OUT $_; } close OUT; close IN; This (obviously) copies the whole text file. How can I select only certain lines to copy based on either the line above or below it? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/