Date and Time sorting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date and Time sorting
# 1  
Old 09-20-2011
Java Date and Time sorting

Hi Guys!

i have a problem of sorting column chronologically because the data i have in column is in the following format

Code:
06/Dec/2006:18:09:54

and need to be sorted in the following way (upto seconds)
Code:
06/Dec/2005:18:09:50
06/Dec/2005:18:09:51
31/Mar/2006:19:30:41
24/Oct/2006:19:16:19
25/Oct/2006:19:16:55

Code:
cut -f1 -d' ' infile.txt | sort -n

the above code is not working is there anyway to convert Jan, Feb into 01, 02 and so on

Regards,

Ewa

Last edited by radoulov; 09-20-2011 at 12:47 PM.. Reason: Code tags.
# 2  
Old 09-20-2011
There is indeed:

Code:
awk 'BEGIN {
        # Set up arrays for name-to-monthnumber
        split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec", MON, "|");
        for(N=1; N<=12; N++) MNUM[MON[N]]=sprintf("%02d", N); }
{
        # Split DD/MON/YYYY:HH:MM:SS into DD MON YYYY HH MM SS
        split($1, D, "[:/]");
        # Replace it with a sortable YYYY/MM/DD:HH:MM:SS
        $1=D[3] "/" MNUM[D[2]] "/" D[1] ":" D[4] ":" D[5] ":" D[6]
} 1' < datafile

YYYY/MM/DD:HH:MM:SS should be sortable alphabetically with no nonsense.

Replace (both) $1 with $2 for column 2, $3 for column 3, $NF for last column, $(NF-1) for last column minus one, etc.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-20-2011
Code:
sort -t/ -k3n -k2,2M -k1n infile


Last edited by radoulov; 09-20-2011 at 12:55 PM..
These 2 Users Gave Thanks to radoulov For This Post:
# 4  
Old 09-20-2011
Hi

thanks corona and radoulov... both solutions are perfect and solved my problem but can anyone explain how the following code works
Code:
sort -t/ -k3n -k2,2M -k1n in file

Best Regards,

Ewa

Last edited by Scott; 09-20-2011 at 07:46 PM.. Reason: Code tags
# 5  
Old 09-22-2011
Yes,

Code:
-t/ - fields are delimited by forward slash(es) `/'

then in order:

Code:
-k3n - sort numerically by the third field
-k2,2M - sort by month considering only the second field 
(not from the second field to the end of the line, 
perhaps this is unnecessary here): i.e. compare (unknown) < `JAN' < ... < `DEC'
- sort by the first field numerically

This may be enough:

Code:
sort -t/ -k3n -k2M -k1n infile

This User Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting data file by date and time

Hello. Sorting data file by date and time with the following issues: Date is in the following format m/d/yyyy, no leading zeros Time is in the following format h:m:s AM/PM, no leading zeros Any ideas on how to sort data when the above issues? Could the date/time be converted inline to... (5 Replies)
Discussion started by: JimBurns
5 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

Displaying current date time of EDT in IST time

Hi Folks, My server time is in EDT. And i am sending automated mails from that server in which i need to display the current date time as per IST (GMT+5:30). Please advice how to display the date time as per IST. IST time leads 9:30 mins to EDT. and i wrote something like below. ... (6 Replies)
Discussion started by: Showdown
6 Replies

4. Shell Programming and Scripting

Adding time to date time in UNIX shell scipting

I needed some help in adding a duration (in seconds) to a start time (in hhmmss format) and a start date (in mmddyy format) in order to get an end date and end time. The concept of a leap year is also to be considered while incrementing the day. The code/ function that I have formed so far is as... (3 Replies)
Discussion started by: codehelp04
3 Replies

5. Solaris

modifying date and time and time zone on solaris 5.10 with (redundant server) veritas

I have a cluster of two Solaris server (veritas cluster). one working and the other is standby I am going to change the date on them , and am looking for a secure solution as it is giving an important service. my opinion is that the active one doesn't need to be restarted (if I don't change the... (1 Reply)
Discussion started by: barry1946
1 Replies

6. Shell Programming and Scripting

Convert Epoch Time to Standard Date and Time & Vice Versa

Hi guys, I know that this topic has been discuss numerous times, and I have search the net and this forum for it. However, non able to address the problem I faced so far. I am on Solaris Platform and unable to install additional packages like the GNU date and gawk to make use of their... (5 Replies)
Discussion started by: DrivesMeCrazy
5 Replies

7. UNIX for Dummies Questions & Answers

Sorting by date and time

Hi guys... I've been trying to do this for ages. Maybe you can help. I have log files like the examples below and I have grepped out certain lines from the files so that I can get an idea of who is logging on and how. So now I have the information in a new file but it is now in a different order... (7 Replies)
Discussion started by: padmundo
7 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