Operating System - HP-UX
1752577 Members
4149 Online
108788 Solutions
New Discussion юеВ

!! How to compare between these two lines and get the latest one!!

 
SOLVED
Go to solution
pareshan
Regular Advisor

!! How to compare between these two lines and get the latest one!!

I have a unique situation here which looks easier at first but I am not able to solve it.

# SSort UNIX 10/14/2005 10/13/2010 "tox" "9000/800" 16 * * * V849-6-1
# SSort UNIX 11/31/1996 11/02/2010 "tox" "9000/800" 16 * * * W237-S-2


I have a text file with two or multiple values like this and what I am trying to do is compare these lines and just keep the line with the latest date on it.

Note: every line have two dates but the one we have to look at is the second date
10/13/2010 in first line and 11/02/2010
in second line.

Any way to do it using unix commands?

I can compare two dates with this command and print the required one
if the file name is tmp I can do this and get the latest date
cat tmp|awk '{print $5}'|sort -t '|' -k3n -k1n -k2n|sed '$!d'

but what I want is the entire line like this
# SSort UNIX 11/31/1996 11/02/2010 "tox" "9000/800" 16 * * * W237-S-2

not only date value.

could anyone help me how can I do it.

6 REPLIES 6
Dennis Handly
Acclaimed Contributor
Solution

Re: !! How to compare between these two lines and get the latest one!!

You want the one line out of the whole file that has the latest date?

sort -r -k5.7,5.10 -k5.4,5.5 -k5.1,5.2 tmp | head -1
James R. Ferguson
Acclaimed Contributor

Re: !! How to compare between these two lines and get the latest one!!

HI:

If you have multiple pairs of lines you could use 'awk' to read two lines at a time' split the line into its component fields; rejoin the date in a YYYYMMDD order and compare the records, printing the "larger" date.

Regards!

...JRF...
pareshan
Regular Advisor

Re: !! How to compare between these two lines and get the latest one!!

cat tmp
# SSort UNIX 10/14/2005 10/13/2010 "tox" "9000/800" 16 * * * V849-6-1
# SSort UNIX 11/31/1996 11/02/2010 "tox" "9000/800" 16 * * * W237-S-2

when I run your command i got this output
# SSort UNIX 12/14/2005 10/13/2010 "tox" "9000/800" 16 * * * V849-6-1

which is not right.
You are right I want the line with the latest date but the other one is latest
# SSort UNIX 11/31/1996 11/02/2010 "tox" "9000/800" 16 * * * W237-S-2
Note: I need to compare the second date field not the first

James R. Ferguson
Acclaimed Contributor

Re: !! How to compare between these two lines and get the latest one!!

Hi:

Dennis inadvertantly reversed the order of the sort. You want"

# sort -r -k5.7,5.10 -k5.1,5.2 -k5.4,5.5 tmp|head -1

...which sorts on the second date's year, then month, then day.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: !! How to compare between these two lines and get the latest one!!

>JRF: Dennis inadvertently reversed the order of the sort.

Oops, wrong field order. It would work for Europe. :-)
That's why dates should always be YYYY/MM/DD so you can sort on the whole field.
pareshan
Regular Advisor

Re: !! How to compare between these two lines and get the latest one!!

Thanks guys its working
Dennis you are right that would have worked in Europe but anyway its working in states too now heheh.

The solution Dennis gave was right but he just did it opposite which was corrected by james.
I appreciate the help
Cheers!!!