Note that if you want to sort by date and time, with the data you have the following sort command will work:
Note that this sort uses the default whitespace field delimiter, instead of a period because the time stamps aren't delimited by a period. Note also that since your data has fixed-wdith date and time fields with leading zeros, you can use either an alphanumeric sort or a numeric sort for the date fields. Using an alphanumeric sort for the time stamp allows us to use one key-k2,2instead of three keys-k2.1,2.2 -k2.4,2.5 -k2.7,2.8 to sort the time. (A numeric sort-k2n,2would only sort on the hour since ":" is not a numeric character.)
The sort keys (in order) are from the 7th character in the 1st field to the end of the 1st field (year), from the 4th character through the 5th character in the 1st field (month), from the 1st character through the 2nd character in the 1st field (day), and from the start of the 2nd field to the end of the 2nd field (time).
Since your sample input data only had one time stamp per day, I tested the above command using the following input (which duplicates the input you provided but changes the date stamps on the 2nd occurrence of each entry to verify that differences in hours, minutes, and seconds all sort correctly:
With a slight overhead (about double run time) you could use dateutils dconv tool to normalise the data, sort them, and convert them back:
gives you:
It's slightly more robust than pure sort solutions, see Don's comment for instance.
I guess I don't know what you mean by "more robust".
The sort utility is available on any UNIX system and on any Linux system; the dconv utility is not available on many of those systems. (For example, dconv is not available on OS X.)
The sort command I provided will work for any dates from year 0001 through 9999 as long as all of the dates are in this format. I don't have access to dconv, but I'm guessing that if you add the line:
to your input file, you'll find that it ends up last on the output rather than first.
(And, yes I know that fixing it for this example only involves adding 3 characters to your command line. ) However, if you happen to be using a programming environment with a 32-bit time_t, your date range is much more limited.
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
The utility msort is available in many repositories. Here's an example solution using it on Don Cragun's recent demonstration data:
producing:
See URL mentioned in script comments if msort is not in your repository.
I guess I don't know what you mean by "more robust".
I mean that dconv is actually made to parse and operate on dates, sort is not.
Quote:
Originally Posted by Don Cragun
The sort utility is available on any UNIX system and on any Linux system; the dconv utility is not available on many of those systems. (For example, dconv is not available on OS X.)
True.
Quote:
Originally Posted by Don Cragun
The sort command I provided will work for any dates from year 0001 through 9999 as long as all of the dates are in this format. I don't have access to dconv, but I'm guessing that if you add the line:
to your input file, you'll find that it ends up last on the output rather than first.
(And, yes I know that fixing it for this example only involves adding 3 characters to your command line. ) However, if you happen to be using a programming environment with a 32-bit time_t, your date range is much more limited.
You guessed wrong. And I'm curious as to what 3 characters you meant? Also, dconv doesn't depend on time_t at all, so it will work exactly the same on 16-bit, 32-bit and 64-bit systems (all tested).
I mean that dconv is actually made to parse and operate on dates, sort is not.
OK. I'll take your word for it. My system doesn't have dconv and there are no dconv man pages in the sets of man pages provided here on unix.com.
Quote:
You guessed wrong. And I'm curious as to what 3 characters you meant? Also, dconv doesn't depend on time_t at all, so it will work exactly the same on 16-bit, 32-bit and 64-bit systems (all tested).
Without access to a dconv man page I (apparently incorrectly) assumed that dconv -S -i '%d.%m.%Y %T' used the strptime() format string argument to convert the text date string into a seconds since the Epoch value, sort sorted the seconds since the Epoch values, and dconv -f '%d.%m.%Y %T' -S converted seconds since the Epoch back to the original text string format using an strftme() date format string. Since the number of seconds since the Epoch in 2012 is represented as a ten digit decimal string in the range 1325xxxxxx-1357xxxxxx and the number of seconds since the Epoch in 2000 was a nine digit string in the range 946xxxxxx-978xxxxxx, I expected the alphanumeric sort specified by sort to sort "9" after "1". If my assumption had been correct, it could have been fixed by telling sort to do a numeric sort sort -n instead of an alphanumeric sort sort.
OK. I'll take your word for it. My system doesn't have dconv and there are no dconv man pages in the sets of man pages provided here on unix.com.
Without access to a dconv man page I (apparently incorrectly) assumed that dconv -S -i '%d.%m.%Y %T' used the strptime() format string argument to convert the text date string into a seconds since the Epoch value, sort sorted the seconds since the Epoch values, and dconv -f '%d.%m.%Y %T' -S converted seconds since the Epoch back to the original text string format using an strftme() date format string. Since the number of seconds since the Epoch in 2012 is represented as a ten digit decimal string in the range 1325xxxxxx-1357xxxxxx and the number of seconds since the Epoch in 2000 was a nine digit string in the range 946xxxxxx-978xxxxxx, I expected the alphanumeric sort specified by sort to sort "9" after "1". If my assumption had been correct, it could have been fixed by telling sort to do a numeric sort sort -n instead of an alphanumeric sort sort.
Ah I see. No, what happens is dconv with no output format specifiers will return ISO 8601 dates (aka 2012-10-31T17:44:00) which are sortable just like that, UNLESS of course you go before the year 1000 or after the year 9999, but ISO isn't defined for those dates anyway .
Hi,
i had a data block (coming from pipe from other codes) as:
H YF_CO.dat 77164 11/17/2013 04:00:02 731374590.96 1 1 731374590.96 76586 77164 578 2988 Y
H YF_CO.dat 77164 11/17/2013 04:00:07 731374590.96 1 4 731374590.96 76586 77164 578 2988 Y
H YF_CO.dat 77178 ... (5 Replies)
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)
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)
Input file:
100%ABC2 3.44E-12 USA
A2M%H02579 0E0 UK
100%ABC2 5.34E-8 UK
100%ABC2 3.25E-12 USA
A2M%H02579 5E-45 UK
Output file:
100%ABC2 3.44E-12 USA
100%ABC2 3.25E-12 USA
100%ABC2 5.34E-8 UK
A2M%H02579 0E0 UK
A2M%H02579 5E-45 UK
Code try:
sort -k1,1 -g -k2 -r input.txt... (2 Replies)
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)
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)
I'd like to convert a date string in the form of sun aug 19 09:03:10 EDT 2012, to unixtime timestamp using awk.
I tried
This is how each line of the file looks like, different date and time in this format
Sun Aug 19 08:33:45 EDT 2012, user1(108.6.217.236) all: test on the 17th
... (2 Replies)
Hello All,
Need a shell script to sort entries in a file by date and time. Below are the entries in the file, i need to sort it first by the date and then time
Note :- Date is in MM/DD/YY format and date comes as the 6th & time comes on 7th coloumns respectively.
150 pbnawldb001-b... (10 Replies)
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)
Hi ,
I would like to list or sort by date and time (the files are named in day and time format) where the latest file will be placed at the bottom and the earliest file be placed at the top. Can anybody help me?
My files are named in the following manner.
EG: abc_071128_144121_data
"... (21 Replies)