Hi,
I have a csv file with old data..i need to have only last 30 days from the current dateof data in the file.The fourth field in the file is a date field.i need to write a script to delete the old data by comparing the the fourth field with the (current date -30).I need to delete the rows in red as they are very old in the month of feburary.Please Could you kindly let me know how to acheive this in unix scripting.
sample records
24,126,1,19/02/2011 03:44:15,1
24,126,2,19/02/2011 09:17:06,1
30,126,38,30/07/2011 13:06:35,1
30,126,39,30/07/2011 13:07:12,1
727,19,111,12/08/2011 15:43,1
727,126,94,12/08/2011 15:43,1
24,126,2,19/02/2011 09:17:06,1
If you have gawk or nawk:
gawk -v FS="," 'BEGIN { REF=systime() - (30 * 24 * 60 * 60); }
{
# Split DD/MM/YYYY HH:MM:SS into DD MM YYYY HH MM SS
split($4, a, " ");
split(a[1], da, "/");
split(a[2], ta, ":");
# add seconds if missing
if(!ta[3]) ta[3]="00";
# paste into YYYY MM DD HH MM SS and make timestamp
t=mktime(da[3] " " da[2] " " da[1] " " ta[1] " " ta[2] " " ta[3]);
# Only print when t is newer than 30 days ago
} (t>REF)' < input > output
In perl...
#!/usr/bin/perl
use Time::Local;
use Time::localtime;
$today = time();
$range = 30*24*60*60;
while (<ARGV>) {
@f = split /,/, $_;
@dt = split /[ :\/]/, $f[3];
$dy = $dt[0] ? $dt[0] : 0;
$mo = $dt[1] ? $dt[1] - 1 : 0;
$yr = $dt[2] ? $dt[2] - 1900 : 0;
$hr = $dt[3] ? $dt[3] : 0;
$mi = $dt[4] ? $dt[4] : 0;
$se = $dt[5] ? $dt[5] : 0;
$sfe = timelocal($se,$mi,$hr,$dy,$mo,$yr);
print if ($today - $sfe < $range);
}
Save the above perl script in a file and run it as...
perl_script input_file