Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

How to sort a field in a file having date values

Shell Programming and Scripting


Tags
unix commands

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-04-2008
risshanth risshanth is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 27 November 2012, 11:52 AM EST
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
How to sort a field in a file having date values

Hi All,

I am having a pipe delimited file .In this file the 3rd column is having date values.I need to get the min date and max date from that file.

I have used
cut -d '|' test.dat -f 3|sort -u

But it is not sorting the date .How to sort the date column using unix commands

Thanks
Deepak
Sponsored Links
    #2  
Old Unix and Linux 06-04-2008
zaxxon's Unix or Linux Image
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 22 September 2017, 9:14 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,572
Thanks: 175
Thanked 560 Times in 481 Posts
Show us some lines of your delimeted file, please.
Sponsored Links
    #3  
Old Unix and Linux 06-04-2008
risshanth risshanth is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 27 November 2012, 11:52 AM EST
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
I have used a small file for testing

A line in the file looks like thsi
deepak|55445|12-02-2008|alappuzha
    #4  
Old Unix and Linux 06-04-2008
ripat ripat is offline Forum Advisor  
Registered User
 
Join Date: Oct 2006
Last Activity: 27 March 2017, 3:00 AM EDT
Location: Belgium
Posts: 544
Thanks: 5
Thanked 42 Times in 29 Posts
The problem with your file is that the date is not ISO i.e. it is not sortable as it is. You have to split it and sort it first on the YYYY, then on MM and, finaly on DD. That's exactly what the following command does. It sorts on the 3rd field from character 7 to character 10 then on char. 4 to 5 etc...


Code:
sort -u -t"|" -k 3.7,3.10n -k 3.4,3.5n -k 3.1,3.2n file


Last edited by ripat; 06-04-2008 at 04:19 AM..
Sponsored Links
    #5  
Old Unix and Linux 06-04-2008
risshanth risshanth is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 27 November 2012, 11:52 AM EST
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks a lot ...
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sorting Date Field with Sort -k :/ Glitch100 Shell Programming and Scripting 17 01-05-2012 12:09 PM
Sort field by date mm/dd/yyyy csierra UNIX for Dummies Questions & Answers 6 03-01-2010 09:11 PM
how to sort date in decimal values uptp two digits jojo123 Shell Programming and Scripting 3 11-04-2009 04:50 AM
Sort two columns in a field, one of them being a date sweta_doshi Shell Programming and Scripting 5 07-28-2009 06:15 AM
How to Sort files on date field shivaastrogun Shell Programming and Scripting 4 11-17-2008 06:38 AM



All times are GMT -4. The time now is 02:49 AM.