bh_perl has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
As your information, I have a text file which are contain a sample data as below:-
1&20070102&string3&string4.... 2&20070101&string3&string4.... 3&20061212&&string3&string4....
Based on the sample data, I want to open the text file and sort the data based on the date (second &-delimited field) and the expected output data would be:-
3&20061212&&string3&string4.... 2&20070101&string3&string4.... 1&20070102&string3&string4....
Could somebody help me how to do this ?
Previously I have write the program as below but its not working on the last part, why this case happened ?
opendir(INDIR, $outdir) or die ("$indir not found\n"); while (my $infile = readdir(INDIR)) { chomp; next if $infile !~ /postpaid/; #change & to space for sorting purpose qx(cat $outdir/$infile |sed 's/\&/\t/g' > $outdir/$inf +ile.tmp); #sort the data based on date or second colum qx(cat $outdir/$infile.tmp |sort -2 > $outdir/$infile. +srt); #change back the space to & #NOTE: This one not working qx(cat $outdir/$infile.srt |sed 's/\t/\&/g' > $outdir/ +$infile.ok); #remove temporary file qx(rm -f $outdir/$infile.tmp); } closedir(INDIR);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to sort data in the input file ?
by kyle (Abbot) on Feb 09, 2007 at 02:32 UTC | |
|
Re: How to sort data in the input file ?
by fmerges (Chaplain) on Feb 09, 2007 at 02:45 UTC | |
|
Re: How to sort data in the input file ?
by ww (Archbishop) on Feb 09, 2007 at 02:20 UTC |