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
Solved

Unix Sorting Challenge

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

838 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