for file in $outdir/*postpaid* do sort -t '&' -k 2 < $file > $file.ok done #### opendir(INDIR, $outdir) or die "Can't opendir $outdir: $!\n"; while (my $infile = readdir(INDIR)) { next if $infile !~ /postpaid/; open my $infile_fh, '<', "$outdir/$infile" or die "Can't read $infile: $!\n"; my @lines = <$infile_fh>; close $infile_fh; open my $outfile_fh, '>', "$outdir/$infile.ok" or die "Can't write $infile.ok: $!\n"; print $outfile_fh sort { ($a1) = ($a =~ /^\d+\&(\d{8})\&/); ($b1) = ($b =~ /^\d+\&(\d{8})\&/); $a1 <=> $b1 } @lines; close $outfile_fh; } closedir(INDIR);