#!/usr/bin/perl -w use strict; use autodie qw(open close); { undef $/; my %file = ($1 => split /<\s*(.+)\n/ , ); map { $_ and open my $fh, q{>}, $_; select $fh; print $file{$_} and close; } keys %file; } __DATA__ < file1.txt line1 of file1 line2 of file1 line3 of file1 line4 of file1 < file2.txt line1 of file2 line2 of file2 line3 of file2 < file3.txt line1 of file3 line2 of file3 line3 of file3 line4 of file3