use warnings; use strict; my $DATAFILE = 'data.txt'; my $INSERTFILE = 'insert.txt'; my $OUTFILE = 'output.txt'; open my $ofh, '>', $OUTFILE or die "$OUTFILE: $!"; select($ofh); # "print"s will go to this handle now open my $dfh, '<', $DATAFILE or die "$DATAFILE: $!"; my $state = 'before nodes'; while ( my $line = <$dfh> ) { if ( $line =~ /^\s*NODE\b/ ) { if ( $state eq 'before nodes' ) { $state = 'in nodes'; } elsif ( $state eq 'in nodes' ) { } elsif ( $state eq 'after nodes' ) { die "expected only one section of NODEs" } else { die $state } } else { if ( $state eq 'before nodes' ) { } elsif ( $state eq 'in nodes' ) { do_node_insert($INSERTFILE); $state = 'after nodes'; } elsif ( $state eq 'after nodes' ) { } else { die $state } } print $line; } close $dfh; sub do_node_insert { my $file = shift; open my $ifh, '<', $file or die "$file: $!"; while ( my $line = <$ifh> ) { next unless $line =~ /^\s*NODE\b/; print $line; } close $ifh; } #### 'Node ID X Y Z BC NODE 1 4.51000 0.00000 79.00000 NODE 2 0.00000 0.00000 79.00000 NODE 3 0.00000 0.00000 78.27000 NODE 4 -1.88000 0.00000 78.27000 'Elem ID np1 np2 material geom lcoor ecc1 BEAM 1 2 1 2 36 2 BEAM 2 3 2 2 36 1 PIPE 19 4.489 0.022 PIPE 20 4.488 0.021 #### NODE 32 0.00000 0.00000 -1.90000 NODE 33 0.00000 0.00000 -5.50000 BEAM 26 27 26 1 14 1 #### 'Node ID X Y Z BC NODE 1 4.51000 0.00000 79.00000 NODE 2 0.00000 0.00000 79.00000 NODE 3 0.00000 0.00000 78.27000 NODE 4 -1.88000 0.00000 78.27000 NODE 32 0.00000 0.00000 -1.90000 NODE 33 0.00000 0.00000 -5.50000 'Elem ID np1 np2 material geom lcoor ecc1 BEAM 1 2 1 2 36 2 BEAM 2 3 2 2 36 1 PIPE 19 4.489 0.022 PIPE 20 4.488 0.021