Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

sorting recoreds in a file

Original Message
Name: bvijays
Date: January 4, 2008 at 06:29:17 Pacific
Subject: sorting recoreds in a file
OS: hp-unix
CPU/Ram: p4,5GB
Model/Manufacturer: 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


Report Offensive Message For Removal


Response Number 1
Name: James Boothe
Date: January 4, 2008 at 12:54:30 Pacific
Subject: sorting recoreds in a file
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 2
Name: FishMonger
Date: January 4, 2008 at 17:11:43 Pacific
Subject: sorting recoreds in a file
Reply: (edit)
 perl -e 'print map{$_->[0]} sort{$a->[1] <=> $b->[1] || $b->[2] cmp $a->[2]} map{[$_, /(\d+)/, uc($_)]} <>;' file.txt


Report Offensive Follow Up For Removal

Response Number 3
Name: FishMonger
Date: January 4, 2008 at 17:17:10 Pacific
Subject: sorting recoreds in a file
Reply: (edit)
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


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: sorting recoreds in a file

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




My PC has been hijacked!

Lexmark 2600 Printer Issues

btk1w1 infected start here post

Unwanted message remians on screen

Slow boot time


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC