Solved

Unix Sorting Challenge

Posted on 2007-03-22
5
315 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
5 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 125 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 250 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 125 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

730 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