Sort based on filenames

Hi All,
When i give ls -ltr my filenames looks like this:

Filename Pattern: Abc_Def_mmddyyyyHHmm.csv

$ ls -ltr
Jun 05 04:30 Abc_Def_060520111245.csv
Jun 05 08:40 Abc_Def_071220121458.csv
Jun 06 03:30 Abc_Def_071220111458.csv
Jun 06 04:25 Abc_Def_060620110439.csv
Jun 07 04:37 Abc_Def_060720111016.csv

Please Let me how to sort files based on filename's datestamp mmddyyyyHHmm present.

O/p: I want to display filenames as below.

Abc_Def_060520111245.csv
Abc_Def_060620110439.csv
Abc_Def_060720111016.csv
Abc_Def_071220111458.csv
Abc_Def_071220121458.csv

THanks in Advance

try..

$ ls Abc_Def* | sort -t'_' -k3.5,3.8 -k3.3,3.4 -k3.1,3.2 -k3.9,3.10 -k3.11,3.12
Abc_Def_071220101458.csv
Abc_Def_060520111245.csv
Abc_Def_060620110439.csv
Abc_Def_060720111016.csv
Abc_Def_071220111458.csv
Abc_Def_071220121458.csv
$ 

without using -t'_' cant it be sorted?
bcoz sometime i may get files as:
Abc_Def_ghi_091220101458.csv
jkl_pqr_trt_081220101458.csv

Hi -t he used as delimeter, I assume if your file will never have other than time stamp as number then u can very well use the same command or below are other options as well

Ascending and Descending orders....
$ nawk '{print $NF|"sort -n"}' file
Abc_Def_060520111245.csv
Abc_Def_060620110439.csv
Abc_Def_060720111016.csv
Abc_Def_071220111458.csv
Abc_Def_071220121458.csv
 
$ nawk '{print $NF|"sort -nr"}' /tmp/inpu
Abc_Def_071220121458.csv
Abc_Def_071220111458.csv
Abc_Def_060720111016.csv
Abc_Def_060620110439.csv
Abc_Def_060520111245.csv

Thanks for your reply... For the above example the logic is working, but for the below example I'm facing the same problem:

$ ls -C1 Market_Priority*
MP_040420102130.csv
MP_AB_060820101450.csv
MP_060820111230.csv
MP_CD_DE_080320101230.csv

$ ls -C1 Market_Priority* |nawk '{print $NF|"sort -n"}'
MP_040420102130.csv
MP_AB_060820101450.csv
MP_060820111230.csv
MP_CD_DE_080320101230.csv

My O/p should look like this:

MP_040420102130.csv
MP_AB_060820101450.csv
MP_CD_DE_080320101230.csv
MP_060820111230.csv

Thanks in Advance,

Try this..

 ls -lrt Abc_Def* | sort -n
#!/usr/bin/perl -w

@files = <>;

@for_sort = ();

for (@files) {
    chomp;
    $temp = $_;
    s/\D*(\d+)\.csv/$1/;
    push @for_sort, [$_, $temp];
}

print "$_\n" for map { $_->[1] } sort { $a->[0] cmp $b->[0] } @for_sort;
% cat testfile
MP_040420102130.csv
MP_AB_060820101450.csv
MP_CD_DE_080320101230.csv
MP_060820111230.csv

% perl sort.pl testfile
MP_040420102130.csv
MP_AB_060820101450.csv
MP_060820111230.csv
MP_CD_DE_080320101230.csv

AWK

awk -F"[_.]" '{printf "\n"$NF" ";
printf substr($(NF-1),5,4) substr($(NF-1),1,4)substr($(NF-1),9)" ";
for(i=1;i<(NF-1);i++){printf $i" ";}}' infile  |
sort -nk2 |
awk '{for(i=3;i<=NF;i++) { printf $i"_";}print substr($2,5,4)substr($2,1,4)substr($2,9)"."$1}'

Thanks for you reply...
Since Perl is not installed in our servers I'm not able to execute the above perl code.
Is there any shell script to perform the same step?

Thanks in Advance,

---------- Post updated at 06:28 AM ---------- Previous update was at 06:20 AM ----------

Thanks a lot...AWK is command for me...

This should fix the prob... :slight_smile:

ls filename*|nawk '{a[$0]++} END {for(x in a) print x}' input.txt

sorry Oops this wil not solve ur problem....plz ignore i ll put the correct one..

cat testfile | sed 's/\([^0-9]*\)\([0-9]*\).csv/\1 \2 .csv /'  | sort -k2 | sed 's/ //g'

Hi,

I tried the above command using sed and sort but I'm unable to get the files in sorted order.

$ cat infile.txt| sed 's/\([^0-9]*\)\([0-9]*\).csv/\1 \2 .csv /' | sort -k2 | sed 's/ //g'
MP_040420102130.csv
MP_060820101450.csv
MP_060820111230.csv
MP_080320101230.csv

% cat testfile
MP_040420102130.csv
MP_AB_060820101450.csv
MP_CD_DE_080320101230.csv
MP_060820111230.csv
app@ubuntu ~/tmp
% cat testfile | sed 's/\([^0-9]*\)\([0-9]*\).csv/\1 \2 .csv /'  | sort -k2 | sed 's/ //g'
MP_040420102130.csv
MP_AB_060820101450.csv
MP_060820111230.csv
MP_CD_DE_080320101230.csv