sort date issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort date issue
# 1  
Old 09-14-2009
sort date issue

Hi Everyone,

Code:
[root@sl ~]# cat b
Sat 12 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 11:32:49 AM MYT;
Sat 13 Sep 2009 10:31:49 PM MYT;a;a;a;Mon 14 Sep 2009 10:31:49 PM MYT;
Sat 14 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 10:31:49 PM MYT;
[root@sl ~]# sort -t';' -k5 b
Sat 13 Sep 2009 10:31:49 PM MYT;a;a;a;Mon 14 Sep 2009 10:31:49 PM MYT;
Sat 14 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 10:31:49 PM MYT;
Sat 12 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 11:32:49 AM MYT;
[root@sl ~]# sort -t';' -nk5 b
Sat 12 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 11:32:49 AM MYT;
Sat 13 Sep 2009 10:31:49 PM MYT;a;a;a;Mon 14 Sep 2009 10:31:49 PM MYT;
Sat 14 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 10:31:49 PM MYT;
[root@sl ~]# sort -t';' -Mk5 b
Sat 12 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 11:32:49 AM MYT;
Sat 13 Sep 2009 10:31:49 PM MYT;a;a;a;Mon 14 Sep 2009 10:31:49 PM MYT;
Sat 14 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 10:31:49 PM MYT;
[root@sl ~]#

I tried few sort, but failed, the output should be
Sat 13 Sep 2009 10:31:49 PM MYT;a;a;a;Mon 14 Sep 2009 10:31:49 PM MYT;
Sat 12 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 11:32:49 AM MYT;
Sat 14 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 10:31:49 PM MYT;


Please advice. Thanks
# 2  
Old 09-14-2009
Can you explain about your expected output?
Which field/data is sorted?
huh! could not find out Smilie
# 3  
Old 09-14-2009
As I understand your problem, you want to sort the file on the colored date fields don't you?

Code:
Sat 12 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 11:32:49 AM MYT;
Sat 13 Sep 2009 10:31:49 PM MYT;a;a;a;Mon 14 Sep 2009 10:31:49 PM MYT;
Sat 14 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 10:31:49 PM MYT;

If this is the case, you are in trouble with the sort command as it doesn't sort on plain text dates AFAIK.

So, first you have to loop through your file and convert the date field into some sort of ISO date format like 2009-09-13 21:15:07 and than, pipe it into sort.

Or use awk with this suggestion:
Code:
# sortdate.awk
BEGIN{
	FS=OFS=";"
	months="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
	split(months, m, " ")
	for (i in m) mm[m[i]]=i
}
{
	split($5, dte, " ")
	split(dte[5], time, ":")
	dteKey=sprintf("%s%02d%s%s%s%s",dte[4],mm[dte[3]],dte[2],(dte[6]=="PM")?time[1]+12:time[1],time[2],time[3])
	record[dteKey$0]=$0
	sortKey[NR]=dteKey$0
}
END{
	n=asort(sortKey)
	for(i=1;i<=n;i++) print record[sortKey[i]]
}

Code:
$ cat file
Sat 12 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 11:32:49 AM MYT;
Sat 13 Sep 2009 10:31:49 PM MYT;a;a;a;Mon 14 Sep 2009 10:31:49 PM MYT;
Sat 14 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 10:31:49 PM MYT;

Code:
$ awk -f sortdate.awk file
Sat 12 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 11:32:49 AM MYT;
Sat 14 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 10:31:49 PM MYT;
Sat 13 Sep 2009 10:31:49 PM MYT;a;a;a;Mon 14 Sep 2009 10:31:49 PM MYT;


Last edited by ripat; 09-14-2009 at 05:29 AM.. Reason: logic error on the sort key
# 4  
Old 09-14-2009
I think the below command is enough for your requirement.

Code:
sort -rk 5 f1.txt

# 5  
Old 09-14-2009
Quote:
Originally Posted by siba.s.nayak
I think the below command is enough for your requirement.

Code:
sort -rk 5 f1.txt

I don't think so. Check on a input file like this:
Code:
$ cat file
Sat 12 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Nov 2009 11:32:49 AM MYT;
Sat 13 Sep 2009 10:31:49 PM MYT;a;a;a;Mon 14 Sep 2009 10:31:49 PM MYT;
Sat 14 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 10:31:49 PM MYT;

$ sort -rk 5 file
Sat 14 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 10:31:49 PM MYT;
Sat 12 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Nov 2009 11:32:49 AM MYT;
Sat 13 Sep 2009 10:31:49 PM MYT;a;a;a;Mon 14 Sep 2009 10:31:49 PM MYT;

# 6  
Old 09-14-2009
Ohh, I was lost some where. I forgot it's sorting date wise. Thanks a lot.....
# 7  
Old 09-14-2009
Just noticed the OP requested a descending order on date. Assuming the snippet will not be used past year 3000, just change these two lines:

Code:
	record[(3*10^13-dteKey)$0]=$0
	sortKey[NR]=(3*10^13-dteKey)$0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort by name and date

nawk '$1=="Date" {d=$(NF-2);next} $1=="Queue" {q=$NF;next} $1=="Forms"{print q, $NF, d}' OFS='|' printfile.log I have this script working. Please let me know how to sort by Queue and then Date. (4 Replies)
Discussion started by: Daniel Gate
4 Replies

2. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

3. Shell Programming and Scripting

sort the date

Hi All, Please help me to sort the date field which is in the format 2012-02-03 16:09:37.388... Platform: Red Hat linux Thanks in advance (2 Replies)
Discussion started by: jesu
2 Replies

4. Shell Programming and Scripting

Issue with Sort in Unix

Hi All, I am trying to sort the below data using sort command. temp.dat H|S1-511091486889|27-Jul-2011 00:00:00 H|S1-511091486823|27-Jul-2011 00:00:00 H|S1-511091486757|27-Jul-2011 00:00:00 L|S1-511091486889|1 L|S1-511091486823|1 L|S1-511091486757|1 sort -t "|" -k2 -k1 temp.dat My... (5 Replies)
Discussion started by: deepaknbk
5 Replies

5. UNIX for Dummies Questions & Answers

how to combine du -h and sort by date

The problem: I have a task to give a my boss: 1. the files and directory sizes in human readable form: du -h 2. He also wants me to give him the date stamp on the files and directories. something like ls -R or sort by date Does anyone have a script that can do this efficiently (I am... (2 Replies)
Discussion started by: obology
2 Replies

6. Shell Programming and Scripting

Sort Issue

these are two records(adr.txt)trying to sort with the the code expl below. 5423|336110|2730 Pierce St|Ste 300|Sioux City|IA|Woodbury|51104|3765||42518651|96405013|A|2|3|U|12/08/2009 5423|14462335|624 JONES ST|STE 5400|Sioux City|IA|Woodbury|51101|||42496648|96400644|A|8|2|U |12/24/2009 nawk... (3 Replies)
Discussion started by: greenworld
3 Replies

7. UNIX for Dummies Questions & Answers

sort issue

Hi Guys , Please help me I am having a requirement as below: $ ls -i log* | head 66486532 log1 66486662 log10 66486663 log11 66486664 log12 66486665 log13 66486533 log2 66486534 log3 66486535 log4 66486584 log5 66486590 log6 This id listing the first 10 files starting with log. ... (4 Replies)
Discussion started by: mraghunandanan
4 Replies

8. Shell Programming and Scripting

how to sort by the date

Hello World~ Please Help Me(BASH) input: dde,2007.8.25,891 dde,2007.8.23,356 dfe,2007.10.12,341 cba,2005.12.5,342 I wanna know how to sort by the date(2005.12.5) output: cba,2005.12.5,342 dde,2007.8.23,356 dde,2007.8.25,891 dfe,2007.10.12,341 Thanks in advance (3 Replies)
Discussion started by: lifegeek
3 Replies

9. UNIX for Advanced & Expert Users

date issue-find prevoius date in a patricular format

Hi , I have written a shell script that takes the current date on the server and stores it in a file. echo get /usr/home/data-`date '+%Y%d'`.xml> /usr/local/sandeep/GetFILE.ini I call this GetFILE.ini file from an sftp program to fetch a file from /usr/home/ as location. The file is in... (3 Replies)
Discussion started by: bsandeep_80
3 Replies

10. Shell Programming and Scripting

Sort by Date

I'm looking to edit a file which contains various data including date.(ddmmyyyy) I want to sort by date and then count the number of different dates found Any ideas how to acheive this Thanks in advance. (2 Replies)
Discussion started by: Mudshark
2 Replies
Login or Register to Ask a Question