#! /usr/bin/perl use strict; use warnings; my @output; my $count = 0; # read input data line by line while ( my $line = ) { # one block in input data file starts with "Time Step X" and ends with empty line if ( $line =~ m{^Time Step \d+$} .. $line =~ m{^\s*$} ) { # save data blocks push @output, $line; # process data block when end of block if ( $line =~ m{^\s*$} ) { for my $i ( 1 .. 24 ) { # replace number in time step line $output[0] =~ s/\d+$/ ++$count/e; # print data block to STDOUT print @output; } # reset @output for next data block @output = (); } } } __DATA__ Time Step 1 a b c d Time Step 2 e f g h Time Step 3 i j k l