use strict; use warnings; { my $input = do { open my $in, '<', '1.txt'; local $/; <$in> }; until ( eof $in){ my $file = <$in>; $file =~ s/..![^!]*something.*\.c//; print $file . "\n"; }