Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
i neeed to sort a file which has lines like
310494550615007O ptt
310494550615008P ptt
310494550615007P ptt
310494550615008Q ptt
310494550615007P ptt
310494550615007Q ptt
310494550615008O pttthe need to sort from 1.1 to 1.15 and then after that sort the with the char(16i.e., O|P|Q)but in reverse alphabetic order...the o/p should be like,
310494550615007Q ptt
310494550615007P ptt
310494550615007P ptt
310494550615007O ptt
310494550615008Q ptt
310494550615008P ptt
310494550615008O pttcan anybody suggest how to do that...
can i use sort with loops ...how to sort among a set of lines in the same file? is there any such option
Please suggest its very urgent
thanks,
Vijay

Gosh, I was thinking that the reverse flag could be specified on a key by key basis.
Until you find something better, following is an awk solution that inputs a sorted file, and it just reverses each group of lines. It stores a group into array slots 1 thru n, then pulls them out of the array in reverse order.
sort file.in |
awk '\
BEGIN {
getline
holdkey=substr($0,1,15)
lbuf[1]=$0
top=1
}function flush_array() {
#print "flushing group: " holdkey
for (k=top;k>0;k--)
print lbuf[k]
top=0
}{
key=substr($0,1,15)
if (key!=holdkey)
{flush_array()
holdkey=key}
top++
lbuf[top]=$0
}END {flush_array()}' > file.out

perl -e 'print map{$_->[0]} sort{$a->[1] <=> $b->[1] || $b->[2] cmp $a->[2]} map{[$_, /(\d+)/, uc($_)]} <>;' file.txt

It might be easier to understand if it's split across multiple lines.
perl -e '\
print map{ $_->[0] }
sort{ $a->[1] <=> $b->[1]
||
$b->[2] cmp $a->[2] }
map{[$_, /(\d+)/, uc($_)]} <>;' file.txt

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |