Sorting issue with directory and file name

Hi All,

My folder contains the files as:

$ ls -lrt
total 50
lms_prap_rf_20100422_99.xml
lms_prap_rf_20100422_9.xml
lms_prap_rf_20100422_100.xml
lms_prap_rf_20100422_10.xml
lms_prap_rf_20100426_1.xml

I need to get the sorting of the above file based on numbers. I need first file in ascending order.

I am using the code as

ls -l lms_prap_rf* | sort -t _ -nk4 -k5 | head -1

which gives me correct one as

 
lms_prap_rf_20100422_9.xml
 

but when I am doing it with the directory I am not getting what I want because my directories consist of underscores.

ls -1 /c09/oracle/XX_TT/12.0.0/TMP_IN/lms_prap_rf* | sort -t _ -nk4 -k5 | head -1

I am getting the wrong answer as

/c09/oracle/XX_TT/12.0.0/TMP_IN/lms_prap_rf_20100422_10.xml

My main code is looking for

in_file=`ls -1 $file_path/$file_name* | sort -t _ -nk4 -k5 | head -1`

where

file_path=]/c09/oracle/XX_TT/12.0.0/TMP_IN

and

file_name=lms_prap_rf

how to avoid the directory path while doing sort. Please not I need to run this script in 2 or 3 environment where the directory path may different and may have more underscores

Please advice.

Regards,
Sreejit

#!/bin/ksh
path_to_files=/c09/oracle/XX_TT/12.0.0/TMP_IN
ls ${path_to_files}/lms_prap_rf* | awk -F '[._]' '{print $NF-1, $0} |
    sort -n | head -1 | read dummy fname; basename $fname

change the path_to_files variable for each directory`

Thanks but still the code below

ls ${path_to_files}/lms_prap_rf* | nawk -F '[._]' '{print $NF-1, $0}' | sort -n | head -1 | read dummy fname; basename $fname

is giving wrong answer

lms_prap_rf_20100422_10.xml

the answer must be

lms_prap_rf_20100422_9.xml

because this is the smallest file as per the date 20100422 and the number 9.

the pattern is lms_prap_rf_{date YYYYMMDD}_{number}.xml

Regards,
Sreejit

I think the problem is that your path name contains "_" character. when you use ls path/file_prefix* or ls -l path/file_prefix*. Try to cd into that directory and then do "ls file_prefix*" instead.

Thanks Yes I do understand that the issue is with the underscore "_" in my path. But I am using this in my shell script and I don't want to use cd command in there.

Is there any way I can ls on the directory and remove the directory path from my result and do sort on it.?

Thanks and Regards,
Sreejit

---------- Post updated at 10:37 AM ---------- Previous update was at 09:59 AM ----------

Hi All-

I am trying the below, can anybody please let me know if I am doing anything wrong.

ls ${path_to_files}/lms_prap_rf* | nawk -F/ '{print $NF}' | sort -t _ -nk4 -k5 | head -1

Regards,
Sreejit

ls -1 /c09/oracle/XX_TT/12.0.0/TMP_IN/lms_prap_rf* |awk -F "/" '{print $2}'| sort -t _ -nk4 -k5