searching for lines > 45 days

searching for lines > 45 days

am 08.01.2008 00:32:58 von Peter

I have a field in a text file 11:39:16 2007-10-07. Is there anyway to
use grep or a similiar tool to extract all lines over 45 days to
another file and remove them from the original file ?

Thanks,
Pete

Re: searching for lines > 45 days

am 08.01.2008 05:25:17 von Ed Morton

Peter wrote:
> I have a field in a text file 11:39:16 2007-10-07. Is there anyway to
> use grep or a similiar tool to extract all lines over 45 days to
> another file and remove them from the original file ?
>
> Thanks,
> Pete

With GNU awk (untested):

mv file backup &&
gawk 'BEGIN{ FS="[- :]"; now = systime(); age = 60*60*24*45 }
{ then = mktime($4" "$5" "$6" "$1" "$2" "$3)
print > ((now - then) > age ? "old" : "file")
}' backup &&
rm backup

Obviously you may want to leave "backup" around until you verify the
script does what you want.

Regards,

Ed.

Re: searching for lines > 45 days

am 08.01.2008 08:18:17 von mik3l3374

On Jan 8, 7:32 am, Peter wrote:
> I have a field in a text file 11:39:16 2007-10-07. Is there anyway to
> use grep or a similiar tool to extract all lines over 45 days to
> another file and remove them from the original file ?
>
> Thanks,
> Pete

#!/bin/sh

t=`echo "11:39:16 2007-10-07" | sed 's/[:-]/ /g'`
set -- $t
string="$4$5$6$1$2"
touch tempfile
touch -t $string tempfile
result=`find . -mtime +45 -ls |grep tempfile > /dev/null`
if [ "$result" -eq 0 ];then
echo "Found line"
rm tempfile
fi

Re: searching for lines > 45 days

am 09.01.2008 00:34:14 von William James

On Jan 7, 5:32 pm, Peter wrote:
> I have a field in a text file 11:39:16 2007-10-07. Is there anyway to
> use grep or a similiar tool to extract all lines over 45 days to
> another file and remove them from the original file ?
>
> Thanks,
> Pete

#!ruby
require 'date'
File.open('old','w'){|old| File.open('new','w'){|new|
ARGF.each{|s|
(Date.today - Date.parse(s[/\d{4}-\d\d-\d\d/]) < 45 ? new : old).
print s
}
}}