Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort based on multiple variables 2

Status
Not open for further replies.

kHz

MIS
Dec 6, 2004
1,359
US
I have a file that contains 7000+ lines with the first column being a European date format (though I would
prefer American, ie, 06-08-2006) and the second column being a file name.

Part of file:
2006-06-08 hwinfo-8.99-0.3.x86_64.rpm
2006-05-10 samba-3.0.9-2.1.5.i586.patch.rpm
2006-02-14 sitar-0.9.3-0.4.noarch.rpm
2006-06-08 cups-devel-1.1.20-108.22.x86_64.patch.rpm
2006-02-14 novell-zenworks-rug-7.0.0-2.i586.rpm
2006-06-08 yast2-trans-it-2.9.15-0.2.noarch.patch.rpm
2006-06-07 sysstat-5.0.1-35.4.x86_64.rpm
2006-06-28 gpg-1.2.4-68.16.x86_64.patch.rpm
2006-06-08 pdksh-5.2.14-780.4.x86_64.patch.rpm
2006-06-07 sitar-0.9.3-0.4.noarch.patch.rpm
2006-06-08 mozilla-venkman-1.7.8-5.20.x86_64.rpm
2006-06-07 xfsprogs-2.6.25-0.6.x86_64.patch.rpm
2006-02-14 yast2-core-2.9.97-0.3.i586.patch.rpm
2006-06-08 kernel-default-2.6.5-7.202.7.x86_64.rpm
2006-06-08 yast2-packagemanager-2.9.67-0.3.x86_64.rpm
2006-02-14 glibc-2.3.3-98.61.i586.rpm
2006-06-08 yast2-installation-2.9.89-0.2.noarch.rpm
2006-04-11 yast2-storage-2.9.69-0.2.i586.patch.rpm
2006-06-08 kdebase3-kdm-3.2.1-68.33.x86_64.rpm
2006-06-08 sax2-4.8-103.33.x86_64.rpm
2006-06-08 liby2util-2.9.27-0.4.x86_64.rpm
2006-06-08 nscd-2.3.3-98.61.x86_64.rpm
2006-02-23 mod_php4-core-4.3.4-43.50.i586.patch.rpm
2006-04-11 mozilla-deat-1.7.6-0.2.i586.patch.rpm
2006-06-08 php4-sysvshm-4.3.4-43.50.x86_64.patch.rpm
2006-04-11 cron-3.0.1-920.4.i586.rpm
2006-05-10 zlib-1.2.1-70.6.i586.rpm
2006-02-14 scim-tables-zh-0.4.3-1.4.i586.rpm
2006-02-14 yast2-trans-zh_CN-2.9.20-0.3.noarch.patch.rpm
2006-02-14 XFree86-server-glx-4.3.99.902-43.71.i586.patch.rpm
2006-06-08 evms-ha-2.3.3-0.40.x86_64.rpm

What I want to do is sort the file so all filenames, e.g., zlib (regardless of version) are grouped
together with the oldest version (which would be based on date) at the top (date is ascending order)
and a space separating the different groups of files. And if I could get the European date format
changed to American that would be nice too.

Example of ending file:

02-14-2006 mailman-2.1.4-83.16.3.i586.rpm
02-14-2006 mailman-2.1.4-83.21.i586.rpm
04-06-2006 mailman-2.1.4-83.24.i586.rpm

02-14-2006 mailman-2.1.4-83.16.3.i586.patch.rpm
02-14-2006 mailman-2.1.4-83.21.i586.patch.rpm
04-06-2006 mailman-2.1.4-83.24.i586.patch.rpm

06-15-2006 php4-mysql-4.3.4-43.58.i586.rpm
06-21-2006 php4-mysql-4.3.4-43.61.i586.rpm

06-15-2006 php4-mysql-4.3.4-43.58.i586.patch.rpm
06-21-2006 php4-mysql-4.3.4-43.61.i586.patch.rpm

06-15-2006 php4-mysql-4.3.4-43.58.x86_64.rpm
06-21-2006 php4-mysql-4.3.4-43.61.x86_64.rpm

06-15-2006 php4-mysql-4.3.4-43.58.x86_64.patch.rpm
06-21-2006 php4-mysql-4.3.4-43.61.x86_64.patch.rpm
...

Thanks!!!
 
You don't ask for much, do you?
Code:
ruby -e 'ARGF.map{|x| x =~
/^(\S+) (\S+?)-\d.*?\.([a-z][^.]*)(\.patch\.|\.)?rpm$/ ; [$2,$3,$4,$1,x]}.sort.each{|x| puts if x[0,3]!=$prev;
 $prev=x[0,3]; puts x.last}' myfile
 
Hi

Use [tt]sort[/tt] to sort, and [tt]awk[/tt] only to arrange the date and insert the empty lines :
Code:
sort -k2 -k1 /input/file | awk '{$1=substr($1,6)"-"substr($1,1,4);n=$2;sub(/-([[:digit:]]+\.)+[[:digit:]]+-.*/,"",n);if(l!=n){if(l)print"";l=n}}1'
Tested with GNU [tt]awk[/tt], should work with other versions too.

Feherke.
 
Feherke, you didn't think about this.
sort the file so all filenames, e.g., zlib (regardless of version) are grouped
together

Feherke, you didn't think about this.
Your sort produces this:
Code:
2006-06-15 php4-mysql-4.3.4-43.58.i586.patch.rpm
2006-06-15 php4-mysql-4.3.4-43.58.i586.rpm
2006-06-15 php4-mysql-4.3.4-43.58.x86_64.patch.rpm
2006-06-15 php4-mysql-4.3.4-43.58.x86_64.rpm
2006-06-21 php4-mysql-4.3.4-43.61.i586.patch.rpm
2006-06-21 php4-mysql-4.3.4-43.61.i586.rpm
2006-06-21 php4-mysql-4.3.4-43.61.x86_64.patch.rpm
2006-06-21 php4-mysql-4.3.4-43.61.x86_64.rpm
2006-06-08 php4-sysvshm-4.3.4-43.50.x86_64.patch.rpm
The o.p. wants them in this order:
Code:
2006-06-15 php4-mysql-4.3.4-43.58.i586.rpm
2006-06-21 php4-mysql-4.3.4-43.61.i586.rpm
2006-06-15 php4-mysql-4.3.4-43.58.i586.patch.rpm
2006-06-21 php4-mysql-4.3.4-43.61.i586.patch.rpm
2006-06-15 php4-mysql-4.3.4-43.58.x86_64.rpm
2006-06-21 php4-mysql-4.3.4-43.61.x86_64.rpm
2006-06-15 php4-mysql-4.3.4-43.58.x86_64.patch.rpm
2006-06-21 php4-mysql-4.3.4-43.61.x86_64.patch.rpm
2006-06-08 php4-sysvshm-4.3.4-43.50.x86_64.patch.rpm

Here's a shorter correct solution.
Code:
ruby '
ARGF.map{|x| x =~
/^(\S+) (\S+?)-\d.*?\.([a-z].*)\.rpm$/ ;
[$2,$3,$1,x]}.sort.each{|x|  puts if x[0,2]!=$prev;
$prev=x[0,2]; puts x.last}' myfile

After the file is sorted, you can use this awk program to fix the dates.
Code:
awk 'NF{split($1,a,"-");$0=a[2]"-"a[3]"-"a[1]" "$2}8'
 
Hi

Hmm... So you say the patch word is part of the file name. That is abit beyond the nice [tt]awk[/tt] solutions and I will not come up with a [tt]perl[/tt] here.

Anyway, I tried your code, but
Code:
ruby: No such file or directory --  (LoadError)
I have [tt]ruby[/tt] 1.6.7. Is it too old for your code ?

Feherke.
 
Thanks to both of you for your help.
 
Feherke, my mistake. Unlike awk, but like Perl, Ruby needs -e
before its code on the command line. I was running the
code in question from a file.
Code:
ruby -e '
ARGF.map{|x| x =~
/^(\S+) (\S+?)-\d.*?\.([a-z].*)\.rpm$/ ;
[$2,$3,$1,x]}.sort.each{|y|  puts if y[0,2]!=$prev;
$prev=y[0,2]; puts y.last}' myfile
ARGF is sort of a handle to all of the files
on the command line. In order to perform the tricky
sort, we replace each line of the file with the array
[$2,$3,$1,x]. The $ns are captures from the regular
expression; x is simply a string containing the
text of the line. Then we sort. This is the
Schwartzian Transform. When we're ready to print,
we use .last to print only the plain text of the line.
 
Hi

Oops. Sorry, I already knew that -e thing. For the moment I can not find any excuse for why I not observed that is missing...

Anyway, I am glad to see we have a Ruby scripter among us and I hope to learn from you more in the Ruby forum.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top