Solved

Unix Sorting Challenge

Posted on 2007-03-22
5
322 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 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

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…
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 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.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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