Vedh
Vedh

Reputation: 93

How to sort the date and print the sorted data in unix?

I have a log file a follows and I want to sort the data in it and print the data between the specified startime and endtime.

Logfile:
[10/04/16 02:07:20  BST] Data 1
[11/04/16 02:07:20  BST] Data 1
[10/05/16 04:11:09  BST] Data 2
[12/05/16 04:11:09  BST] Data 2
[11/06/16 06:22:35  BST] Data 3
[12/06/16 06:22:35  BST] Data 3
[11/07/16 11:07:30  BST] Data 3
[12/08/16 09:08:07  BST] Data 4
[22/08/16 09:08:07  BST] Data 4

I am using the following command.

sort -n -k 1.5 -k 1.3 -k 1 logfile | sed -n "/[10/04/16 02:07:20  BST]/,/[12/05/16 04:11:09  BST]/p";

But it is displaying all the data in the file.But I want to stop it at the endtime which I mentioned. Is there a better way? Thanks in Advance.

Upvotes: 0

Views: 38

Answers (1)

Marichyasana
Marichyasana

Reputation: 3154

You have to escape the brackets and also the slashes in the date part. Try this (works on my laptop):

sort -n -k 1.5 -k 1.3 -k 1 logfile | sed -n "/\[10\/04\/16 02:07:20  BST\]/,/\[12\/05\/16 04:11:09  BST\]/p"

output:

[10/04/16 02:07:20  BST] Data 1  
[11/04/16 02:07:20  BST] Data 1  
[10/05/16 04:11:09  BST] Data 2  
[12/05/16 04:11:09  BST] Data 2

Upvotes: 1

Related Questions