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

In reply to Re: How to sort data in the input file ? by kyle
in thread How to sort data in the input file ? by bh_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.