in reply to How to sort data in the input file ?

You seem to be a lot more comfortable with shell programming than Perl programming. I think you could do all of what you want in the shell like so:

for file in $outdir/*postpaid* do sort -t '&' -k 2 < $file > $file.ok done

You can also do it all in Perl, and there are many ways to accomplish that. This is probably one of them:

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);