?
Solved

Unix Sorting Challenge

Posted on 2007-03-22
5
Medium Priority
?
329 Views
Last Modified: 2008-02-01
Here's a sorting challenge.  You can use Unix sort command or Perl/awk.  Price to the most succinct answer.

Given the following input file:

fx_act1_r-2006-04-29.lis,1
fx_act2_r-2006-04-29.lis,1
fx_act1_r-2006-04-26.lis,10
fx_act1_r-2006-04-26.lis,11
fx_act1_r-2006-04-26.lis,9
fx_act1_r-2006-04-27.lis,10
fx_act1_r-2006-04-27.lis,28
fx_act1_r-2006-04-27.lis,8
fx_act1_r-2006-04-27.lis,9
fx_act1_r-2006-04-28.lis,10
fx_act1_r-2006-04-28.lis,11
fx_act1_r-2006-05-01.lis,21
fx_act1_r-2006-05-01.lis,22
fx_act2_r-2006-04-26.lis,10
fx_act2_r-2006-04-26.lis,11

the file needs to be sorted alphabetically by filename (eg: fx_act1_r), then by date (eg: 2006-04-27), then by sequence number suffix (eg: 11)

So for the above input, the sorted output would be:

fx_act1_r-2006-04-26.lis,9
fx_act1_r-2006-04-26.lis,10
fx_act1_r-2006-04-26.lis,11
fx_act1_r-2006-04-27.lis,8
fx_act1_r-2006-04-27.lis,9
fx_act1_r-2006-04-27.lis,10
fx_act1_r-2006-04-27.lis,28
fx_act1_r-2006-04-28.lis,10
fx_act1_r-2006-04-28.lis,11
fx_act1_r-2006-04-29.lis,1
fx_act1_r-2006-05-01.lis,21
fx_act1_r-2006-05-01.lis,22
fx_act2_r-2006-04-26.lis,10
fx_act2_r-2006-04-26.lis,11
fx_act2_r-2006-04-29.lis,1


Be creative!
0
Comment
Question by:Tintin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 500 total points
ID: 18777672
perl -e 'print map/\0(.*)/s,sort map{sprintf"%s%9d\0%s",(/((.*,)(\d+)\s*)/)[1,2,0]}<>' file
0
 
LVL 62

Accepted Solution

by:
gheist earned 1000 total points
ID: 18778381
Using GNU sort from their coreutils, as written in /etc/passwd example at "info coreutils sort" info page:

sort -t, -k1b,1 -k2,2n
0
 
LVL 21

Assisted Solution

by:tfewster
tfewster earned 500 total points
ID: 18778439
gheist, you could shorten your answer to
sort -t, -k1,1 -k2n

I never thought of treating name+date as one "field", so I came up with this:
sed 's/-\([0-9][0-9]*\)/,\1/' file|sort -t, -k1,1 -k2,2 -k3n|sed 's/,\([0-9][0-9]*\)/-\1/'
0
 
LVL 62

Expert Comment

by:gheist
ID: 18783247
One can use two "sort" commandswith differentseparators, assuming undocumented feature to keep tails after comma sorted after resorting against date.
0
 
LVL 48

Author Comment

by:Tintin
ID: 18787385
All very good solutions, but I always like the simplest solution.    As usual, ozo comes up with a great Perl one liner.
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question