#!/usr/bin/env perl use strict; use warnings; use File::Path qw/make_path/; use Cwd; my $dir = cwd(); opendir (my $dh, $dir) or die "Unable to open current directory! $!\n"; my @subdirs = readdir $dh or die "Unable to read directory! $!\n"; closedir $dh; my $result_path = "$dir/results"; make_path("$result_path"); for my $subdir ( sort @subdirs ) { chdir($dir); next unless -d $subdir; make_path("$result_path/$subdir"); my $search_text1 = qr/atom/; my $infile1="$subdir/input.txt"; my $outfile1="$result_path/output"; open my $in_fh1, '<', $infile1 or die qq{Failed to open "$infile1" for writing: $!}; open my $out_fh1, '>', $outfile1 or die qq{Failed to open "$outfile1" for writing: $!}; while (<$in_fh1>) { next unless /$search_text1/; my @fields1 = split; print $out_fh1 join("\t", @fields1[1]), "\n"; } close($in_fh1); close($out_fh1); chdir(".."); }