in reply to File parsing query

This should work (I think: untested code)

my $modified_text = ""; my $counter = 0; my $db_line = ""; my $text; open (FH, "<", "some_file"); while (<FH>) { if (m/^\s*DATABASE: /i) { # This will only match if 'DATABASE:' ap +pears on the start of the line, or is prefixed with whitespace. /i si +nce the case of the word probably doesn't matter. if (defined $db_line) { my $insert = "WIDGETS FOUND: $counter\n"; # The text to insert + right after the 'database:'-line. $modified_data .= $db_line . $insert . $text; } $counter = 0; $text = ""; $db_line = $_; next; } # When should the counter be increased? if (m/WIDGET/) { $counter++: } $text .= $_; } close (FH); open (FH, ">", "some_file"); print FH $modified_text; if (defined $db_line) { my $insert = "WIDGETS FOUND: $counter\n"; # The text to insert rig +ht after the last 'database:'-line. print FH $db_line . $insert . $text; } close (FH);

Update: this approach will store all the data in the memory, if the file is too large to do that, then you should create a temp file, and change the $modified_data .= ...; line to print TEMP ...;

And then you can rename/remove the old file and rename/copy the temp file.

Update2: forgot to print the last database-section...