Computing.Net > Forums > Unix > How to delete rows based on date condition

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to delete rows based on date condition

Reply to Message Icon

Name: Max_2503
Date: October 3, 2009 at 06:55:00 Pacific
OS: HP-Unix
Subcategory: Software Problems
Comment:

I have a file delimited with '|' and want to remove all the records from the file if the date is greater than a year from sysdate. The layout of the file is as below -

00719210001800|002555|004027|2009-12-27-00:00| 000000000|N
00719210001800|002555|004027|2010-01-03-00:00| 000000000|N
00719210001800|002555|004027|2010-01-10-00:00| 000000000|N
00719210001800|002555|004027|2010-01-17-00:00| 000000000|N

As you can see above, we need to pick the year, month and day from the timestamp field from the file and do the comparison if if it greater than a year from the current date, then delete that record.

Any pointers/directions is greatly appreciated..

Thanks,
Max



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: October 3, 2009 at 11:29:42 Pacific
Reply:

Maybe this korn script will help:

#!/bin/ksh

# add 1 to this year
newyy=$(( $(date '+%Y') + 1 ))

mmdd=$(date '+%m-%d')

# build the date
chkdate="${newyy}-${mmdd}-00:00"

while IFS="|" read f1 f2 f3 fdate therest
do
   if [[ $fdate < $chkdate ]]
   then
      echo "${f1}|${f2}|${f3}|${fdate}|${therest}"
   fi
done < datafile.txt


0
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: How to delete rows based on date condition

renaming files based on date arguments www.computing.net/answers/unix/renaming-files-based-on-date-arguments/2499.html

Help to delete rows in a file www.computing.net/answers/unix/help-to-delete-rows-in-a-file/3751.html

How to delete duplicate lines ?? www.computing.net/answers/unix/how-to-delete-duplicate-lines-/8277.html