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:
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,
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.
IN9999
IN9998
IN9997
IN0000
IN0001
Please suggest ways to get the order as highlighted in red above.
ls | awk 'NR==1{x=substr($1,3);i=1;a=$1;i++;f=0;next}(f==0){y=substr($1,3);if(y-x>1){f=1}else{a=$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 ----------
Is the number of digits always 4? How many files will coexist in that directory? Is the number always incremented by one? The exercise will be to find the gap - if there is one - and start from above it, eventually using a solution like pamu's or raj_saini20's. I'm not sure if counting down from 9999 in a while loop is the best or most efficient solution, but it might do. You may want to check existence of 9999 and 0000 first as you can avoid the gap search if these don't coexist.
Easy solution if not more than a few file exist: ls -1 IN9*; ls -1 IN0*