Sorting string with date and number

Hi,

We have files coming in the system and we want to sort it in ascending order with date and sequence.

The file pattern are inbound_crp_date_sequence.xml

example we have file as below:

inbound_crp_20100422_10.xml
inbound_crp_20100422_2.xml
inbound_crp_20100422_3.xml
inbound_crp_20100422_7.xml
inbound_crp_20100422_8.xml
inbound_crp_20100422_4.xml
inbound_crp_20100422_100.xml
inbound_crp_20100422_5.xml
inbound_crp_20100422_6.xml
inbound_crp_20100422_9.xml
inbound_crp_20100422_99.xml

I need the output as:

inbound_crp_20100422_2.xml
inbound_crp_20100422_3.xml
inbound_crp_20100422_4.xml
inbound_crp_20100422_5.xml
inbound_crp_20100422_6.xml
inbound_crp_20100422_7.xml
inbound_crp_20100422_8.xml
inbound_crp_20100422_9.xml
inbound_crp_20100422_10.xml
inbound_crp_20100422_99.xml
inbound_crp_20100422_100.xml

I am using code as

ls -1 * |sort -u

but I am getting wrong output as

inbound_crp_20100422_10.xml
inbound_crp_20100422_100.xml
inbound_crp_20100422_2.xml
inbound_crp_20100422_3.xml
inbound_crp_20100422_4.xml
inbound_crp_20100422_5.xml
inbound_crp_20100422_6.xml
inbound_crp_20100422_7.xml
inbound_crp_20100422_8.xml
inbound_crp_20100422_9.xml
inbound_crp_20100422_99.xml

Please advice.

I am also trying to do as ls -1 * | sort -k1.13n -k1.22n, please let me know is it correct

Thanks and regards,
Sreejit

Hi.

Try:

$ sort -t_ -nk3 -k4 < file1
inbound_crp_20100422_2.xml
inbound_crp_20100422_3.xml
inbound_crp_20100422_4.xml
inbound_crp_20100422_5.xml
inbound_crp_20100422_6.xml
inbound_crp_20100422_7.xml
inbound_crp_20100422_8.xml
inbound_crp_20100422_9.xml
inbound_crp_20100422_10.xml
inbound_crp_20100422_99.xml
inbound_crp_20100422_100.xml

Hi scottn

Thanks for reply it is working.

Can you briefly explain it.

-t_ is it checking for underscore ?
what -nk3 -k4 is doing?

Regards,
Sreejit

---------- Post updated at 05:27 PM ---------- Previous update was at 05:23 PM ----------

Ok I got it it is
-k3 looking for whole date
-k4 lokking for sequence

so Is it that first it will sort k3 in ascending and then k4 in ascending?

Regards,
Sreejit