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
# 8  
Old 06-08-2011
AWK
Code:
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}'

# 9  
Old 06-08-2011
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...
# 10  
Old 06-08-2011
This should fix the prob... Smilie

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

Last edited by Shahul; 06-08-2011 at 09:51 AM..
# 11  
Old 06-08-2011
Code:
cat testfile | sed 's/\([^0-9]*\)\([0-9]*\).csv/\1 \2 .csv /'  | sort -k2 | sed 's/ //g'

# 12  
Old 06-09-2011
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
# 13  
Old 06-09-2011
I really don't understand why.

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


Previous Thread | Next Thread
Test Your Knowledge in Computers #799
Difficulty: Medium
In digital numeral systems, the radix is the number of unique digits minus 1.
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