Sorting file based on name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting file based on name
# 1  
Old 08-21-2012
Sorting file based on name

Hi team,

We have few files landing to our server based on sequence number. These files have to be processed in the sequence number order. Once the sequence number has reached its maximum, the files with sequence number 0000 has to be processed.
For example:
Code:
IN9997
IN9998
IN9999
IN0000
IN0001

Above is the correct order in which the files should be processed.

But 'ls' command processed the files in the wrong order as given below,
Code:
IN0000
IN0001
IN9997
IN9998
IN9999

Which is not recommended as there will be a seq number gap

ls | sort -rn also gives the wrong output.
Code:
IN9999
IN9998
IN9997
IN0000
IN0001

Please suggest ways to get the order as highlighted in red above.


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-21-2012 at 05:39 AM.. Reason: code tags
# 2  
Old 08-21-2012
you can try by modified date as..

Code:
ls -ltr

# 3  
Old 08-21-2012
The file time stamp might vary. The requirement is sort only based on the file name,.
# 4  
Old 08-21-2012
try this....

Code:
ls | sed 's/IN00/INLS/g' |  sort | sed 's/INLS/IN00/g'

This can do your job....

but not the best solution.....
# 5  
Old 08-21-2012
Code:
ls | awk 'NR==1{x=substr($1,3);i=1;a[i]=$1;i++;f=0;next}(f==0){y=substr($1,3);if(y-x>1){f=1}else{a[i]=$1;i++};x=y}(f==1){print}END{for(j=1;j<i;j++)print a[j]}'

---------- Post updated at 01:10 PM ---------- Previous update was at 01:09 PM ----------

output of ls should be
Code:
IN0000
IN0001
IN9997
IN9998
IN9999

# 6  
Old 08-21-2012
Quote:
Originally Posted by raj_saini20
Code:
ls | awk 'NR==1{x=substr($1,3);i=1;a[i]=$1;i++;f=0;next}(f==0){y=substr($1,3);if(y-x>1){f=1}else{a[i]=$1;i++};x=y}(f==1){print}END{for(j=1;j<i;j++)print a[j]}'

---------- Post updated at 01:10 PM ---------- Previous update was at 01:09 PM ----------

output of ls should be
Code:
IN0000
IN0001
IN9997
IN9998
IN9999

Required output is --

Code:
IN9997
IN9998
IN9999
IN0000
IN0001

# 7  
Old 08-21-2012
code output is this only
Code:
IN9997
IN9998
IN9999
IN0000
IN0001

the output i mentioned is for ls
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting based on File name

Hi All I have a requirement to list all the files in chronological order based on the date value in the file name.For ex if I have three files as given below ABC_TEST_20160103_1012.txt ABC_TEST_20160229_1112.txt ABC_TEST_20160229_1112.txt I have written code as given below to list out... (2 Replies)
Discussion started by: ginrkf
2 Replies

2. Shell Programming and Scripting

Sorting/Arranging file based on tags using awk

Hi, I have file which contains data based on tags. Output of the file should be in order of tags. Below are the files : Tags.txt f12 f13 f23 f45 f56 Original data is like this : Data.txt 2017/01/04|09:07:00:021|R|XYZ|38|9|1234|f12=CAT|f23=APPLE|f45=PENCIL|f13=CAR... (5 Replies)
Discussion started by: Prathmesh
5 Replies

3. Shell Programming and Scripting

List the files after sorting based on file content

Hi, I have two pipe separated files as below: head -3 file1.txt "HD"|"Nov 11 2016 4:08AM"|"0000000018" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" head -3 file2.txt "HD"|"Nov 15 2016 2:18AM"|"0000000019" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" I want to list the... (6 Replies)
Discussion started by: Prasannag87
6 Replies

4. Shell Programming and Scripting

Sorting file based on a numeric column

Hi, I use UBUNTU 12.04. I have a file with this structure: Name 2 1245787 A G 12 14 12 14 .... Name 1 1245789 C T 13 12 12 12..... I would like to sort my file based on the second column so to have this output for example: Name 1 1245789 C T 13 12 12 12..... Name 2 1245787 A G 12 14... (4 Replies)
Discussion started by: Homa
4 Replies

5. UNIX for Dummies Questions & Answers

Sorting a file based on the absolute value of a column

I would like to sort a tab delimited text file based on the absolute value of its second column. How do I go about doing that? Thanks! Example input: A -12 B 0 C -6 D 7 Output: A -12 D 7 C -6 B 0 (4 Replies)
Discussion started by: evelibertine
4 Replies

6. Shell Programming and Scripting

sorting based on a specified column in a text file

I have a tab delimited file with 5 columns 79 A B 20.2340 6.1488 8.5086 1.3838 87 A B 0.1310 0.0382 0.0054 0.1413 88 A B 46.1651 99.0000 21.8107 0.2203 89 A B 0.1400 0.1132 0.0151 0.1334 114 A B 0.1088 0.0522 0.0057 0.1083 115 A B... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

7. Shell Programming and Scripting

Sorting file based on names

Hi I have some files in directory and the names of files are like jnhld_15233_2010-11-23 jnhld_15233_2007-10-01 jnhld_15233_2001-05-04 jnhld_15233_2011-11-11 jnhld_15233_2005-06-07 jnhld_15233_2000-04-01 ..etc How can i sort these files based on the date in the file name so that ... (4 Replies)
Discussion started by: morbid_angel
4 Replies

8. UNIX for Dummies Questions & Answers

Sorting data in file based on field in another file

Hi, I have two files, one of which I would like to sort based on the order of the data in the second. I would like to do this using a simple unix statement. My two files as follows: File 1: 12345 1 2 2 2 0 0 12349 0 0 2 2 1 2 12350 1 2 1 2 2 2 . . . File2: 12350... (3 Replies)
Discussion started by: kasan0
3 Replies

9. Shell Programming and Scripting

sorting csv file based on column selected

Hi all, in my csv file it'll look like this, and of course it may have more columns US to UK;abc-hq-jcl;multimedia UK to CN;def-ny-jkl;standard DE to DM;abc-ab-klm;critical FD to YM;la-yr-tym;standard HY to MC;la-yr-ytm;multimedia GT to KJ;def-ny-jrt;critical I would like to group... (4 Replies)
Discussion started by: tententen
4 Replies

10. Shell Programming and Scripting

sorting file based on two or more columns

Hi gang. I'm using a unix/mac system and i'm trying to sort a file (more than 1,000,000 lines). chr1 100000965 100001001 - chr1 100002155 100002191 + chr1 100002165 100002201 + chr1 100002525 100002561 - chr1 10000364 ... (2 Replies)
Discussion started by: labrazil
2 Replies
Login or Register to Ask a Question