Sort Date YYYYDDD

Hello,

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

Thanks

Try like...

sort -r test.txt 

Try:

perl -ne 'push @lines,$_;END{print sort {$a=~/_\d{7}_/;$x=$&;$b=~/_\d{7}_/;$y=$&;$x cmp $y} @lines}' input

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.

I hope this helps.

bakunin

Dear bmk, dear bakunin

I am sorry but your code are not working as expected.

Don't worry I am not blaming you. I am blaming me to not be able to do it by myself. I did try with "SORT" but all my tests failed.

I am not strong enought to solved this on my own. Is there a way for SORT do do such job ?

PS : PERL is working fine but I would preferre to use some code that I could understand .. But thanks a lot at BARTUS11

bakunin's solution is actually working, there is just small mistake in the field specification. It should be:

sort -k6.11,6.17 -k 6 input

oops, my bad: i miscounted the fields, sorry.

bakunin

BARTUS11,

Thanks a lot for your correction.

The code is working perfect, but If I may what mean it ?

-k6.11,6.17 -k 6

In same time, how to execute the PERL code in shell script ? There is a condition ? Sorry for my question ...but I got a "cannot execute error".

Thanks

@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:

KG < KSM < KSO < SC

tyler_durden

Hello,

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

Thanks

awk -F'_|__|___' '{print $3,$0 | "sort"}' 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
$
$

tyler_durden

1 Like