in reply to help: extracting multiple lines from file based on match in one line
The modified version of the original file is written to a temp file, then the original file is deleted and the temp file is renamed. The script works - I tested it using your sample input data.use strict; my $match = 'var2 = 3'; my $in = 'in.txt'; my $tmp = 'temp.txt'; my $ext = 'extract.txt'; my ($inh, $tmph, $exth, $last); open($inh, $in); open($tmph, ">$tmp"); open($exth, ">$ext"); while (<$inh>) { if (index($_, $match) == -1) { print $tmph $last; $last = $_; next; } print $exth $last, $_; $last = ''; print $exth (scalar<$inh>) for (1..9); } close($inh); close($tmph); close($exth); unlink($in); rename($tmp, $in);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: help: extracting multiple lines from file based on match in one line
by my_perl (Initiate) on Nov 17, 2004 at 21:03 UTC |