I am still in trouble to sort log file in order by date as (YYYYDDD).
INFO :
YYYY = Year
DDD = Day in the year 001 for 1st January
Here is Input
New File: 95106 Oct 21 TAG__SC___2000229_0.TAB
New File: 95040 Mar 29 TAG__KSM__2012023_0.TAB
New File: 95106 Oct 21 TAG__KG___2012229_0.TAB
New File: 83424 Oct 22 TAG__SC___2012230_0.TAB
New File: 79530 Oct 22 TAG__KG___2012293_0.TAB
New File: 79728 Oct 22 TAG__SC___2012293_0.TAB
New File: 95106 Oct 21 TAG__KSM__2001229_0.TAB
New File: 95106 Oct 21 TAG__KSO__2002229_0.TAB
New File: 83226 Oct 22 TAG__KSM__2012230_0.TAB
New File: 79530 Oct 22 TAG__KSM__2012293_0.TAB
New File: 79530 Oct 22 TAG__KSO__2012293_0.TAB
New File: 95106 Oct 23 TAG__KG___2007013_0.TAB
Output Needed
New File: 95106 Oct 21 TAG__SC___2000229_0.TAB
New File: 95106 Oct 21 TAG__KSM__2001229_0.TAB
New File: 95106 Oct 21 TAG__KSO__2002229_0.TAB
New File: 95106 Oct 23 TAG__KG___2007013_0.TAB
New File: 95040 Mar 29 TAG__KSM__2012023_0.TAB
New File: 95106 Oct 21 TAG__KG___2012229_0.TAB
New File: 83226 Oct 22 TAG__KSM__2012230_0.TAB
New File: 83424 Oct 22 TAG__SC___2012230_0.TAB
New File: 79530 Oct 22 TAG__KG___2012293_0.TAB
New File: 79728 Oct 22 TAG__SC___2012293_0.TAB
New File: 79530 Oct 22 TAG__KSM__2012293_0.TAB
New File: 79530 Oct 22 TAG__KSO__2012293_0.TAB
You need to sort according to some field but the sort key to start not at the first but another character. This can be done by a key definition using the "-k" switch of sort.
Notice that the indexing changes (0-based, 1-based) as you use or don't use the "-t" and the "-b" switches of "sort.
The following (not tested) should do what you want and at least you should be able to modify it to your needs:
<your list> | sort -k5.11,5.17 -k5
This sorts on the date part of the filename and - in case of equivalence - on the complete filename alphabetically. As the numbers involved are all zero-padded there is no difference between numeric and alphabetical sorting.
@OP:
In the expected output, shouldn't the line in red be the last one?
The last 4 lines have the same "yyyyddd" value, so the sorting on "yyyyddd" and then the 6th field would result in this order:
The Input was just some lines. In fact I used the code
sort -k6.11,6.17 -k 6
but the result is not good as expected.
Here is what I getting :
New File: 95106 Oct 23 TAG__KSM__2012292_0.TAB
New File: 79530 Oct 22 TAG__KSM__2012293_0.TAB
_+_ File: 94710 Oct 21 TAG__KSO__2012290_0.TAB
New File: 95040 Oct 20 TAG__KSO__2012291_0.TAB
New File: 95106 Oct 23 TAG__KSO__2012292_0.TAB
New File: 79530 Oct 22 TAG__KSO__2012293_0.TAB
_+_ File: 94974 Oct 21 TAG__SC___2012290_0.TAB
New File: 95040 Oct 20 TAG__SC___2012291_0.TAB
New File: 95106 Oct 23 TAG__SC___2012292_0.TAB
New File: 79728 Oct 22 TAG__SC___2012293_0.TAB
The date of file is tje most important for me, but if the order will fit :
KG < KSM < KSO < SC
that will be great.
Note :
the head of the line could be :
New File
_+_ File
_-_ File
$
$ cat f72
New File: 95106 Oct 23 TAG__KSM__2012292_0.TAB
New File: 79530 Oct 22 TAG__KSM__2012293_0.TAB
_+_ File: 94710 Oct 21 TAG__KSO__2012290_0.TAB
New File: 95040 Oct 20 TAG__KSO__2012291_0.TAB
New File: 95106 Oct 23 TAG__KSO__2012292_0.TAB
New File: 79530 Oct 22 TAG__KSO__2012293_0.TAB
_+_ File: 94974 Oct 21 TAG__SC___2012290_0.TAB
New File: 95040 Oct 20 TAG__SC___2012291_0.TAB
New File: 95106 Oct 23 TAG__SC___2012292_0.TAB
New File: 79728 Oct 22 TAG__SC___2012293_0.TAB
$
$
$ sort -k6.11 -k6 f72
_+_ File: 94710 Oct 21 TAG__KSO__2012290_0.TAB
_+_ File: 94974 Oct 21 TAG__SC___2012290_0.TAB
New File: 95040 Oct 20 TAG__KSO__2012291_0.TAB
New File: 95040 Oct 20 TAG__SC___2012291_0.TAB
New File: 95106 Oct 23 TAG__KSM__2012292_0.TAB
New File: 95106 Oct 23 TAG__KSO__2012292_0.TAB
New File: 95106 Oct 23 TAG__SC___2012292_0.TAB
New File: 79530 Oct 22 TAG__KSM__2012293_0.TAB
New File: 79530 Oct 22 TAG__KSO__2012293_0.TAB
New File: 79728 Oct 22 TAG__SC___2012293_0.TAB
$
$