Sorting string with date and number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting string with date and number
# 1  
Old 04-28-2010
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:
Code:
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:
Code:
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
Code:
ls -1 * |sort -u

but I am getting wrong output as

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

Last edited by sreejitnair123; 04-28-2010 at 12:50 PM.. Reason: Added more code tags
# 2  
Old 04-28-2010
Hi.

Try:
Code:
$ 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


Last edited by Scott; 04-28-2010 at 12:47 PM..
# 3  
Old 04-28-2010
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting a column according to the number of names

Hey, So I'm having issues sorting a data set. The data set contains entries as such; # key: sex, time, athlete, athlete's nationality, date, city, country M, 2:30:57.6, Harry Payne, GBR, 1929-07-05, Stamford Bridge, England M, 2:5:42, Khalid Khannouchi, MAR, 1999-10-24, Chicago, USA M,... (1 Reply)
Discussion started by: DNM_UKN
1 Replies

2. Shell Programming and Scripting

Sorting by date

I am trying to sort by two columns. The first column in an ID, the second is a date in the form yyyy-mm-dd. I need to sort by the ID column, then in ascending order for the date column (earliest date to most recent date compared to today). Input data: 012-abc 2012-04-25 ... (3 Replies)
Discussion started by: mollydog11
3 Replies

3. Shell Programming and Scripting

Sorting on length with identification of number of characters

Hello, I am writing an open-source stemmer in Java for Indic languages which admit a large number of suffixes. The Java stemmer requires that each suffix string be sorted as per its length and that all strings of the same length are arranged in a single group, sorted alphabetically. Moreover as a... (3 Replies)
Discussion started by: gimley
3 Replies

4. Shell Programming and Scripting

Number of days between the current date and user defined date

I am trying to find out the number of days between the current date and user defined date. I took reference from here for the date2jd() function. Modified the function according to my requirement. But its not working properly. Original code from here is working fine. #!/bin/sh... (1 Reply)
Discussion started by: hiten.r.chauhan
1 Replies

5. Shell Programming and Scripting

Sorting the users logged in according to the number of processes

Is it possible to get a list of users sorted by the number of processes executed by each. I have a HP - UX server with 2800 processes running currently. And I want to know the number of processes owned by each person logged in to that server.something like below: user1 : 150 Processes user2 :... (2 Replies)
Discussion started by: engineer
2 Replies

6. Shell Programming and Scripting

Sorting name##.name by number only

How do I sort the output of ls rfc1.txt rfc2.txt rfc30.txt rfc17.txt rfc10.txt rfc3.txt rfc22.txt rfc44.txt rfc20.txt and put it in order like this….. rfc1.txt rfc2.txt rfc3.txt rfc10.txt rfc17.txt rfc20.txt rfc22.txt (2 Replies)
Discussion started by: fatman278
2 Replies

7. UNIX for Advanced & Expert Users

Sorting in unix using column number

Hi All, Can anyone tell me how to sort data in a file using the “sort” command? Note that the records in the data file are not separated into fields. I know only the column positions of the data on which sorting should be done. For example say I've to sort based on the data present in between... (1 Reply)
Discussion started by: swat
1 Replies

8. Shell Programming and Scripting

Date Sorting

Hi, I have a list of files that take on the format ABCDE_yymmdd and wish to sort them in ascending date order. I can't use the unix time stamp for the file as this could possibly be different from the date given in the file name. Does anyone know of any way this can be done using unix shell... (14 Replies)
Discussion started by: LiquidChild
14 Replies

9. UNIX for Dummies Questions & Answers

date sorting

Hi at all, I have to sort a log file on timestamp field. That's field is the third! a log file sample..... 1|EVTVOD-1-20060709_000614|2006/07/09-0:11:23|0.3.8 1|EVTVOD-1-20060709_000614|2006/07/09-0:11:16|0.3.8 1|EVTVOD-1-20060709_000614|2006/07/09-0:11:20|0.3.8... (3 Replies)
Discussion started by: nmilella
3 Replies

10. UNIX for Dummies Questions & Answers

sorting on date

I have a file where dates in the form mm/dd/yyyy is the first field. How do I sort the file by the date field? Thanks, Duckman (6 Replies)
Discussion started by: Duckman
6 Replies
Login or Register to Ask a Question