Compare these two lines and delete the old entry


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Compare these two lines and delete the old entry
# 1  
Old 04-08-2009
Compare these two lines and delete the old entry

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.
# 2  
Old 04-08-2009
assuming the fields 6 and 7 can be used as 'key' fields uniquely identifying records/lines...

nawk -f par.awk myFile

par.awk:
Code:
{
  split($5, datA, "/")
  dat=datA[3] datA[1] datA[2]
  idx = $6 SUBSEP $7

  if (dat > arrD[idx]) {
     arrD[idx]=dat
     arrV[idx]=$0
  }
}
END {
  for (i in arrV)
    print arrV[i]
}

Or if you don't care about uniqueness and keys and just one to get ONE line:
Code:
{
  split($5, datA, "/")
  dat=datA[3] datA[1] datA[2]
  idx = $6 SUBSEP $7

  if (dat > maxD) {
     maxD=dat
     line=$0
  }
}
END {
  print line
}


Last edited by vgersh99; 04-08-2009 at 07:23 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two files and lines which are the same just delete it

I am having a two files and different days, and this is example: file1: 06.09.2017. abcd 123 file2: 07.09.2017. abcd 1234 So what I want is that file2 with today's date contains only 1234, so where is a problem you would ask? Problem is here that I put these commands into routers,. and... (3 Replies)
Discussion started by: tomislav91
3 Replies

2. How to Post in the The UNIX and Linux Forums

Help me, write a bash script to delete parent entry with all their child entry in LDAP UNIX server

Hi All, Please help me and guide me to write a bash/shell script on Linux box to delete parent entry with all their child entries. example: Parent is : ---------- dn: email=yogesh.kumar@wipro.com, o=wipro, o=in child is: ---------- dn: cn: yogesh kumar, email=yogesh.kumar@wipro.com,... (1 Reply)
Discussion started by: Chand
1 Replies

3. UNIX for Dummies Questions & Answers

Search for string in a file then compare it with excel files entry

All, i have a file text.log: cover6 cover3 cover2 cover4 other file is abc.log as : 0 0 1 0 Then I have a excel file result.xls that contains: Name Path Pass cover2 cover3 cover6 cover4 (1 Reply)
Discussion started by: Anamika08
1 Replies

4. Shell Programming and Scripting

Perl: How to delete an entry from hash of lists?

I've got a hash of lists such as below: %lists = ( "111" => , "222" => , "333" => , ); How do I remove a particular entry from the list? For example if I want to remove the entry with value "10" in the "222" list? Thanks, //Juha (2 Replies)
Discussion started by: Juha
2 Replies

5. UNIX for Dummies Questions & Answers

Not able to delete this file/directory/entry

Hello UNIX gurus, I noticed this file or whatever in one of our directories, and somehow I am not able to proceed with my work, without deleting this one. .insert--- 1 siebload intrface 0 Feb 22 01:25 Testfile I am confused, as it doesnt appear to be a file, and on doing any... (2 Replies)
Discussion started by: ppathak1234
2 Replies

6. Shell Programming and Scripting

Need to delete duplicate lease entry

Hi *, I need to delete duplicate lease entries in file according to MAC/IP. I'm having tempfile which contains many lease info and need to have one entry for each IP(not more than that), if it contains more than one entry for same set, need to be deleted that entry... EX: lease... (4 Replies)
Discussion started by: SMNK
4 Replies

7. Shell Programming and Scripting

delete entry from /etc/hosts file ?

Hi there I have a requirement where i have to globally remove a hosts file entry from all boxes e.g. 10.01.10.1 my_server1 normally for 'in-line' editing of files without passing it out to another fle and copying it back etc which is messy, Ive been using the fantastic "perl -pi... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

8. Shell Programming and Scripting

shell script to edit file and delete entry

Can anyone provide me a shell script to edit a xml file and delete one entry. To do manually i can edit(vi editor) the file and 'dd' will delete the file.But I wiluld to know if I can do with a script. Thanks in advance Tannu (6 Replies)
Discussion started by: tannu
6 Replies

9. Programming

userpw.h AIX ( delete entry from the shadow password database )

HI i need to delete an entry in /etc/security/passwd. can't find a way to do it with userpw.h api ( AIX ). the passwd file i delete like this. Write all entrys to passwd file except the one we are removing. can't find any function that works like getspent / getpwent do in AIX userpw api.... (4 Replies)
Discussion started by: nighter
4 Replies

10. Shell Programming and Scripting

delete dhcp.conf entry using sed

I am trying to use sed to remove entries from my dhcpd.conf file. The form of the file is: host foo { option 1 option 2 } host bar { option 1 option 2 } I was trying to use a label like: sed -e :a -e "s/^host bar {*//g;/{/N;//ba" /etc/dhcpd.conf... (2 Replies)
Discussion started by: tizatron
2 Replies
Login or Register to Ask a Question