in reply to grep and delete
Hmm.. that removes the 5 lines after each match .. re-reading OP it seems like it mightbe asking for just delete all the lines after the match ... using similar method:my @lines; open FILE, "<", "foo.txt"; while( <FILE> ){ push @lines, $_; next unless /\byourWord\b/; <FILE> for 1 .. 5; } close FILE; open FILE, ">", "foo.txt"; print FILE, @lines; close FILE;
Another option is Tie::File and a similar approach to above .. or start looping through the array, and just splice off everything after your word is found.my @lines; open FILE, "<", "foo.txt"; while( <FILE> ){ push @lines, $_; last if /\byourWord\b/; } close FILE; open FILE, ">", "foo.txt"; print FILE, @lines; close FILE;
|
|---|