Delete older lines from file

Hello,
I have File all_file.txt , which have information as below.

01/23/2014  11:09 PM  5,511 fr\my1       m102_al11234_sbl_20140124.zip 
03/23/2014  11:09 PM  5,511 fr\my1       m102_al15434_sbl_20140324.zip
03/24/2014  11:09 PM  5,511 fr\my1       m102_al99234_sbl_20140324.zip
03/25/2014  11:09 PM  5,511 fr\my1       m102_al43234_sbl_20140325.zip
03/26/2014  11:09 PM  5,511 fr\my1       m102_al00234_sbl_20140326.zip

I want to delete line older than 30 days.

Could some one help to write a code for this.

Looks like your on Fedora so you should have GNU date:

set -- $(date -d "-30 days" "+%Y %m %d %p %I %M")
awk -vY=$1 -vm=$2 -vd=$3 -vp=$4 -vI=$5 -vM=$6 '
$3>Y||
$3==Y&&$1>m||
$3==Y&&$1==m&&$2>d||
$3==Y&&$1==m&&$2==d&&$6>p||
$3==Y&&$1==m&&$2==d&&$6==p&&$4>I||
$3==Y&&$1==m&&$2==d&&$6==p&&$4==I&&$5>M' FS="[/: ]+" all_file.txt
1 Like

@Chubler, what about around year changes?

1 Like

Should all be good, comparison is Y M D AM H M in that order.

1 Like

I am using HP-UX and i think "-30 days" this will not work :frowning:

---------- Post updated at 03:51 AM ---------- Previous update was at 03:27 AM ----------

Hello,

#!/usr/bin/ksh
delete_date=$(perl -e 'use POSIX;print strftime "%m%d%Y\n",localtime time-2419200;')
echo $delete_date

I got something but need further help.

delete_date is in format 02272014.

can we convert it to 02/27/2014 format and sed the line from all_file.txt?

Try this:

set -- $(perl -MPOSIX -le'@a=localtime;$a[3]-=30;print strftime "%Y %m %d %p %I %M",@a')
awk -vY=$1 -vm=$2 -vd=$3 -vp=$4 -vI=$5 -vM=$6 '
$3>Y||
$3==Y&&$1>m||
$3==Y&&$1==m&&$2>d||
$3==Y&&$1==m&&$2==d&&$6>p||
$3==Y&&$1==m&&$2==d&&$6==p&&$4>I||
$3==Y&&$1==m&&$2==d&&$6==p&&$4==I&&$5>M' FS="[/: ]+" all_file.txt
1 Like

So my final code is

#!/usr/bin/ksh
set -- $(perl -MPOSIX -le'@a=localtime;$a[3]-=30;print strftime "%Y %m %d %p %I %M",@a')
awk -vY=$1 -vm=$2 -vd=$3 -vp=$4 -vI=$5 -vM=$6 '
$3>Y||
$3==Y&&$1>m||
$3==Y&&$1==m&&$2>d||
$3==Y&&$1==m&&$2==d&&$6>p||
$3==Y&&$1==m&&$2==d&&$6==p&&$4>I||
$3==Y&&$1==m&&$2==d&&$6==p&&$4==I&&$5>M' FS="[/: ]+" all_file.txt > all_file_imp.txt

mv all_file_imp.txt all_file.txt

Thanks a lot.

---------- Post updated at 07:32 AM ---------- Previous update was at 04:28 AM ----------

Just not to mess up with what we have done.
can you suggest how we get EOF or /n at end of or file_all.txt