Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-10-2011, 04:02 PM   #1
LQ Newbie
Registered: May 2011
Posts: 2

Rep: Reputation: 0
Maximum Date Value from a file

Hi ..

I am facing a problem in finding out the maximum value of a field using awk .. The field is of date format i.e. (MM/DD/YYYY) ..

The contents of the file is :

2/2/2011 XYZ
2/2/2011 XYZ1
2/3/2011 ABC
2/4/2011 ABC
2/9/2011 XYZ
2/10/2011 XYZ
2/11/2011 XYZ
2/12/2011 XYZ

I need to find the maximum of the first column i.e. my output should be 2/12/2011 ..

I was using the below awk script :
awk 'n < $0 {n=$0} {print n}{print $0}' <source_file having the above data>

But the output I am getting is 2/9/2011 instead of 2/12/2011 ...

Can anyone please tell me as what could be reason for this output and how can I get my desired result ..?

Old 05-10-2011, 04:08 PM   #2
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112

One method to resolve this would be to re-format your date fields so they're padded with left zeroes, like so:

02/02/2011 XYZ
02/02/2011 XYZ1
02/03/2011 ABC
02/04/2011 ABC
02/09/2011 XYZ
02/10/2011 XYZ
02/11/2011 XYZ
02/12/2011 XYZ

And you should be good to go.

If you're not padding left zeroes, you're not truly in MM/DD/YYYY format.

The next option would be beyond my skill set... you'd have to figure out how to use the / character as a delimiter.
Old 05-10-2011, 06:00 PM   #3
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911
Just a word of caution; this will only work if there's always ever only
ONE months worth of dates from the SAME year in the file:
awk -F"[/ ]" 'BEGIN{oldd=""}{if(oldd<$2){p=$0;oldd=$2}}END{print p}' sourav
2/12/2011 XYZ


Last edited by Tinkster; 05-10-2011 at 06:04 PM.
Old 05-23-2011, 10:09 PM   #4
LQ Newbie
Registered: May 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for your replies ... Sorry for the delay in replying ...

To SL00b,
I finally thought of going to the route u suggested by having all the dates in format MM/DD/YYYY , it was working fine till I encountered the below dates in the file


I was expecting an O/P 01/01/2011 but the script gave 12/31/2010.. Can anyone please help?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Keep original mod date of a file and 'date' it back to same file? SilversleevesX Programming 4 07-16-2010 11:12 AM
[SOLVED] Use date with touch to create new file with date based name craigjl Linux - Newbie 5 03-12-2010 09:46 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
Maximum file size Hiran Joseph Programming 7 11-24-2006 07:54 AM
file-max maximum twk Linux - Server 2 09-14-2006 03:32 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:15 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration