Visit Our UNIX and Linux User Community


Sort based on filenames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort based on filenames
# 1  
Old 06-07-2011
Sort based on filenames

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

Filename Pattern: Abc_Def_mmddyyyyHHmm.csv

Code:
$ 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.
Code:
Abc_Def_060520111245.csv
Abc_Def_060620110439.csv
Abc_Def_060720111016.csv
Abc_Def_071220111458.csv
Abc_Def_071220121458.csv


THanks in Advance

Last edited by pludi; 06-07-2011 at 08:40 AM..
# 2  
Old 06-07-2011
try..

Code:
$ 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
$

# 3  
Old 06-07-2011
without using -t'_' cant it be sorted?
bcoz sometime i may get files as:
Abc_Def_ghi_091220101458.csv
jkl_pqr_trt_081220101458.csv
# 4  
Old 06-07-2011
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

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

# 5  
Old 06-08-2011
Thanks for your reply... For the above example the logic is working, but for the below example I'm facing the same problem:

Code:
$ 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:
Code:
MP_040420102130.csv
MP_AB_060820101450.csv
MP_CD_DE_080320101230.csv
MP_060820111230.csv

Thanks in Advance,

Last edited by Franklin52; 06-08-2011 at 05:02 AM.. Reason: Please use code tags
# 6  
Old 06-08-2011
Try this..

Code:
 ls -lrt Abc_Def* | sort -n

# 7  
Old 06-08-2011
Code:
#!/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;

Code:
% 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


Previous Thread | Next Thread
Test Your Knowledge in Computers #661
Difficulty: Easy
DOS uses the File Access Table (FAT) filesystem.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort based on one column

Hi All , I am having an input file like this Input file 7 sks/jsjssj/ddjd/hjdjd/hdhd/Q 10 0.5 13 dkdkd/djdjd/djdjd/djd/QB 01 0.5 ldld/dkd/jdf/fjfjf/fjf/Q 0.5 10 sjs/jsdd/djdkd/dhd/Q 01 0.5 21 kdkd/djdd/djdd/jdd/djd/QB 01 0.5 dkdld/djdjd/djd/Q 01 0.5 ... (9 Replies)
Discussion started by: kshitij
9 Replies

2. UNIX for Beginners Questions & Answers

Sort the filenames

Hello Unix experts: I have dir where few files are there, i want to sort these files and write the output to some other file but i need filenames with filepath too eg: i have filenames like 010020001_S-FOR-Sort-SYEXC_20171218_094256_0004.txt so i want to sort my files on first 5 fields of... (2 Replies)
Discussion started by: gnnsprapa
2 Replies

3. UNIX for Beginners Questions & Answers

How do I custom sort the files in a directory using the filenames in a text file.?

Hi all, (5 Replies)
Discussion started by: KMusunuru
5 Replies

4. Shell Programming and Scripting

Naming output files based on variable parameters and input filenames

Hello, I have a series of files in sub-directories that I want to loop through, process and name according to the input filename and the various parameters I'm using to process the files. I have a number of each, for example file names like AG005574, AG004788, AG003854 and parameter values like... (2 Replies)
Discussion started by: bdeads
2 Replies

5. Shell Programming and Scripting

Sort based on certain value in a column

Hi, i need to sort content of files based on a specific value. An example as below. Input1.txt Col_1 SW_MH2_ST ST_F72_9S SW_MH3_S6 Col_2 SW_MH3_AS7 ST_S15_9CH SW_MH3_AS8 SW_MH3_ST Col_3 ST_M93_SZ ST_C16_TC (12 Replies)
Discussion started by: redse171
12 Replies

6. Shell Programming and Scripting

Sort based on numbers

I have a file which has the following data :- how can I sort the data in descending order . My files may have the first column with 1 to 10000 numbers .I need to arrange them in descending order . Thanks (2 Replies)
Discussion started by: lazydev
2 Replies

7. Shell Programming and Scripting

Sort and extract based on two files

Hi, I am having trouble sorting one file based on another file. I tried the grep -f function and failed. Basically what I have is two files that look like this: File 1 (the list) gh aba for hmm File 2 ( the file that needs to be sorted) aba 2 4 6 7 for 2 4 7 4... (4 Replies)
Discussion started by: phil_heath
4 Replies

8. Shell Programming and Scripting

sort based on the own pattern

Hi Everyone, # cat 1.pl #!/usr/bin/perl use strict; use warnings; my @test=("a","b","c","d"); print @test; # ./1.pl abcd if i want to sort the @test, based on my own partten, the output is "cbda", how to do? as i know perl sort i can use cmp and <=>, but how to do with my own... (7 Replies)
Discussion started by: jimmy_y
7 Replies

9. Programming

Sort the filenames containing . in file name

Hi All, I have a list of files in a directory ..which are look like: 42420031.1000 42420031.1001 42420031.396 42420031.402 42420031.403 42420031.404 42420031.405 42420031.406 42420031.407 42420031.408 42420031.409 Here when i do ls 42420031* |sort it gives the output as ... (3 Replies)
Discussion started by: sanj_eck
3 Replies

10. Shell Programming and Scripting

sort by based on multiple columns

Hi, Is there any way to sort a file in cshell by sort command, sorting it by multiple fields, like to sort it first by the second column and then by the first column. Thanks forhead (1 Reply)
Discussion started by: Takeeshe
1 Replies

Featured Tech Videos