#!/usr/bin/perl use warnings; use strict; #test file to learn how to process multiple files at once #first get the path for current directory my $dir = '.'; # current working directory #open and read only the .txt files opendir my $DIR, $dir or die "Cannot open '$dir' because: $!"; my @textFiles = grep /\.txt\z/, readdir $DIR; closedir $DIR or die "Could not close '$dir' because: $!"; #open each file for further processing foreach my $text_file ( @textFiles ) { open my $text_file_fh_in, '<', $text_file or die "Cannot open '$text_file' because: $!"; #open for reading #create the names of the new files my $text_file_out = $text_file; $text_file_out =~ s/(?=\.txt\z)/_new/; open my $text_file_fh_out, '>', $text_file_out or die "Cannot open '$text_file_out' because: $!"; #open for writing #tests to see if it works #print "$text_file\n"; print "$text_file_fh_in\n"; #print "$text_file_out\n"; print "$text_file_fh_out\n"; while ( <$text_file_fh_in> ) { if ( /^(.*?)\.(.*?)\ (.*?)\.(.*?)\ (.*?)$/ ) { print $text_file_fh_out "$1\t$2\t$4\t$5\n"; } } #close the filehandles close $text_file_fh_in or die "Could not close '$text_file' because: $!"; close $text_file_fh_out or die "Could not close '$text_file_out' because: $!"; }