Solved

Unix Sorting Challenge

Posted on 2007-03-22
5
291 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
Comment Utility
perl -e 'print map/\0(.*)/s,sort map{sprintf"%s%9d\0%s",(/((.*,)(\d+)\s*)/)[1,2,0]}<>' file
0
 
LVL 61

Accepted Solution

by:
gheist earned 250 total points
Comment Utility
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 20

Assisted Solution

by:tfewster
tfewster earned 125 total points
Comment Utility
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 61

Expert Comment

by:gheist
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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 …
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now