Reputation: 559
I have data like this
HOS05 23/12/2008 10AM
HOS06 15/12/2008 2PM
HOS62 29/12/2008 10AM
HOS64 23/12/2008 2PM
HOS70 26/12/2008 10AM
ZFT01 06/12/2008 10AM
HOS73 11/12/2008 2PM
MHOS0 05/12/2008 10AM
MHOS0 20/12/2008 2PM
MHOS0 27/12/2010 2PM
MHOS0 11/12/2008 10AM
MHOS0 30/12/2009 2PM
^^ ^^^^
I have to sort by two offsets which is indicated with ^
s.
The first highlighted offset should be sorted ASC order and then second highlighted in DESC order. So i did like this
sort -k 1.6,1.8 date.txt | sort -k 1.14,1.17 -r > final.txt
output:
MHOS0 27/12/2010 2PM
MHOS0 30/12/2009 2PM
ZFT01 06/12/2008 10AM
MHOS0 20/12/2008 2PM
MHOS0 11/12/2008 10AM
MHOS0 05/12/2008 10AM
HOS73 11/12/2008 2PM
HOS70 26/12/2008 10AM
HOS64 23/12/2008 2PM
HOS62 29/12/2008 10AM
HOS06 15/12/2008 2PM
HOS05 23/12/2008 10AM
This is working fine but i need in single sort command. Any suggestions ?
Upvotes: 2
Views: 149
Reputation: 2017
I do not know how to acheive this with a single sort
command. When I have to solve this type of problem I use a perl
script. Something similar to the following simple script will do the job for you (and give you the flexibility to sort by whatever fields you want, in whatever order you want):
#!/usr/bin/perl
@lines = ();
while (<>) { push(@lines, $_); }
@sorted = sort {
$a1 = substr($a, 0, 6);
$ayear = substr($a, 12, 4);
$b1 = substr($b, 0, 6);
$byear = substr($b, 12, 4);
if ($ayear == $byear) { return $b1 cmp $a1; }
else { return $byear cmp $ayear; }
} @lines;
print @sorted;
and then, of course,
$ perl sorter.perl < data.txt
The perl
sort
documentation is at http://perldoc.perl.org/functions/sort.html
Upvotes: 1
Reputation: 19514
I found this juicy tidbit in the manpage:
POS is F[.C][OPTS], where F is the field number and C the character position in the field; both
are origin 1. If neither -t nor -b is in effect, characters in a field are counted from the
beginning of the preceding whitespace. OPTS is one or more single-letter ordering options,
which override global ordering options for that key. If no key is given, use the entire line as
the key.
And here's the first take:
sort -k 1.6,1.8 date.txt | sort -k 1.14,1.17 -r
So how about:
sort -k 2.7r,2.10 -k 2.1,2.2 date.txt
Remember[*], fields are separated by whitespace and are counted from 1, so your date field is field 2.
[*] Don't remember this! I'm being rhetorical. Just read the manpage when you use an unfamiliar command. How fields are numbered is one of the details that vary widely among different commands.
Upvotes: 2