Computing.Net > Forums > Unix > sorting recoreds in a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

sorting recoreds in a file

Reply to Message Icon

Name: vijay (by bvijays)
Date: January 4, 2008 at 06:29:17 Pacific
OS: hp-unix
CPU/Ram: p4,5GB
Product: intel
Comment:

Hi,
i neeed to sort a file which has lines like
310494550615007O ptt
310494550615008P ptt
310494550615007P ptt
310494550615008Q ptt
310494550615007P ptt
310494550615007Q ptt
310494550615008O ptt

the 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 ptt

can 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



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: January 4, 2008 at 12:54:30 Pacific
Reply:

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


0

Response Number 2
Name: FishMonger
Date: January 4, 2008 at 17:11:43 Pacific
Reply:

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


0

Response Number 3
Name: FishMonger
Date: January 4, 2008 at 17:17:10 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: sorting recoreds in a file

Sort a file www.computing.net/answers/unix/sort-a-file/2474.html

deleting rows in a file www.computing.net/answers/unix/deleting-rows-in-a-file/3676.html

how to replace a line in a file www.computing.net/answers/unix/how-to-replace-a-line-in-a-file/7214.html