Visit The New, Modern Unix Linux Community

Sorting dates in chronological order

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting dates in chronological order
# 1  
Sorting dates in chronological order

Hi forum.

I'm hoping someone can help me out with this problem. I tried to search online but couldn't come up with an exact solution.

I have the following data file:


I need to sort the file by the 2nd field in chronological order.

# 2  
sort -Mt"-" -k2,2 file

# 3  
sort -t"|" -k 2.8,2.11 -k 2.4,2.6M -k 2.1,2.2n

---------- Post updated at 08:05 AM ---------- Previous update was at 08:03 AM ----------

Originally Posted by bartus11
sort -Mt"-" -k2,2 file

Hello Bartus11,

It won't work if the data have "cahnge in year".


cat input_file
sort -Mt"-" -k2,2 input_file

# 4  
Thanks guys for your quick responses.

My sort utility doesn't somehow recognize the "M" option.
sort -t"|" -k 2.8,2.11 -k 2.4,2.6M -k 2.1,2.2n test_file
Usage:   sort   [-Abcdfimnru] [-T Directory] [-t Character] [-o File]
                [-y[Kilobytes]] [-z Recordsize] [-k Keydefinition]...
                [[+Position1][-Position2]]... [File]...

Any other way to resolve this?

Last edited by Franklin52; 07-14-2011 at 05:09 PM.. Reason: Code tags
# 5  
My sort utility doesn't somehow recognize the "M" option.
What operating system are you on?
# 6  
AIX newdwdev 3 5 0007DA12D900
# 7  
Write your own sort in awk using the second field as the key unless you know perl which has a builting sort function...

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #497
Difficulty: Medium
A computer compiler or interpreter is just as smart as a human.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort in chronological order

I am trying to sort a log file in chronological order to identify which ones did not process and still have an old (probably yesterday's) date. This is a sample of the file:flatf 010140 flatf Thu May 10 22:22:11 CST 2018 flats finished flatf 010142 flatf Thu May 10 22:31:25 CST 2018 flats... (4 Replies)
Discussion started by: wbport
4 Replies

2. Shell Programming and Scripting

Sorting strings in reverse order

Hello, I have a large database of words and would like them sorted in reverse order i.e. from the end up. An example will make this clear: I have tried to write a program in Perl which basically takes the string from the end and tries to sort from that end but it does not seem... (5 Replies)
Discussion started by: gimley
5 Replies

3. UNIX for Dummies Questions & Answers

sorting sequences in ascending order

Hi, I have this single file with a number of sequence inside it of format >string1 data >string100 data >string10 ..... >string5 ... >string67 ...... the dots represent data. I wanted to get the sequences arranged in ascending order like >string1 data >string5 (5 Replies)
Discussion started by: sonia102
5 Replies

4. UNIX for Dummies Questions & Answers

Any way to get find command o/p in chronological order?

Hi friends, I am using below script to gzip files after naming them in a particular order. but I intend to name them in numerical order as per their timings(earlier updated fle with a smaller numeric extension than later updated),but this script is not working as planned. please help with... (7 Replies)
Discussion started by: Jcpratap
7 Replies

5. Shell Programming and Scripting

Sorting dates in Perl

I have a directory of backup files. named like this: (6 Replies)
Discussion started by: robsonde
6 Replies

6. UNIX for Dummies Questions & Answers

sorting PID in decreasing order using ps?

I am trying to sort the output in decreasing order of the process ID while using the ps command. I am having trouble doing this using the --sort part of ps. Also I was wondering if anyone knows what the "S" stands for under the process's status code? (3 Replies)
Discussion started by: crimputt
3 Replies

7. Shell Programming and Scripting

multiple sorting with different order

Hi Guys, I have data like this HOS05 23/12/2008 10AM HOS06 15/12/2008 2PM HOS62 29/12/2008 10AM HOS64 23/12/2008 2PM HOS70 26/12/2008 10AM ZFT01 06/12/2008 10AM HOS73 11/12/2008 2PM MHOS0 05/12/2008 10AM MHOS0 20/12/2008 2PM MHOS0 27/12/2010 2PM MHOS0 11/12/2008 10AM MHOS0 30/12/2009... (1 Reply)
Discussion started by: ckarunprakash
1 Replies

8. UNIX for Advanced & Expert Users

Uploading files in chronological order

Thanks for your help. (3 Replies)
Discussion started by: circuit.muni
3 Replies

9. UNIX for Dummies Questions & Answers

Order sorting

How do you sort text in order using sed? :confused: For example 01 B D A C to 01 ABCD (3 Replies)
Discussion started by: evoGage
3 Replies

10. UNIX for Advanced & Expert Users

Sorting filenames by order in another file

I would like to arrange /sort filenames ending with suffix like ".00XXXX". where X is a digit. However the order of arrangement is in a text file and is 'harpharzard'. e.g the text file may be like 002345 009807 001145 I wanted to avoid doing this using sql and exporting the text file back to... (4 Replies)
Discussion started by: samudimu
4 Replies

Featured Tech Videos