Delete older lines from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete older lines from file
# 1  
Old 03-26-2014
RedHat Delete older lines from file

Hello,
I have File all_file.txt , which have information as below.
Code:
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.
# 2  
Old 03-26-2014
Looks like your on Fedora so you should have GNU date:

Code:
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

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 03-27-2014
@Chubler, what about around year changes?
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-27-2014
Should all be good, comparison is Y M D AM H M in that order.
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 03-27-2014
RedHat find and delete

Quote:
Originally Posted by Chubler_XL
Looks like your on Fedora so you should have GNU date:

Code:
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

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

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

Hello,

Code:
#!/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?
# 6  
Old 03-27-2014
Try this:

Code:
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

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 03-27-2014
RedHat minus 30 days form current date in Unix

So my final code is

Code:
#!/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 ----------

Quote:
Originally Posted by kumar30213
So my final code is

Code:
#!/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.

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filtering log file with lines older than 10 days.

Hi, I am trying to compare epoch time in a huge log file (2 million lines) with todays date. I have to create two files one which has lines older than 10 days and another file with less than 10 days. I am using while do but it takes forever to complete the script. It would be helpful if you can... (12 Replies)
Discussion started by: shunya
12 Replies

2. Red Hat

Delete files older than 1week(dates need to be calculate based on file name)

Objective: We have multiple files in a folder and we want to delete all files except for last 1 week files. Note: We are copying these files from original location to this temporary location. So time shown for these files are time when we copied to this location. Not that when file was created.... (2 Replies)
Discussion started by: Agoyals1986
2 Replies

3. Shell Programming and Scripting

Search 2 days older file and then delete last 10 lines

I want to search 2 day older file and then delete last 10 line of that file. (2 Replies)
Discussion started by: sonu pandey
2 Replies

4. Shell Programming and Scripting

List and Delete Files which are older than 7 days, but have white spaces in file name

I need to list and delete all files in current older which are olderthan 7 days. But my file names have white spaces. Before deleting I want to list all the files, so that I can verify.find . -type f -mtime +7 | xargs ls -l {} But the ls command is the working on the files which have white... (16 Replies)
Discussion started by: karumudi7
16 Replies

5. Red Hat

Need Script to ZIP/SAVE & then DELETE Log file & DELETE ZIPS older than 12 months

ENVIROMENT Linux: Fedora Core release 1 (Yarrow) iPlanet: iPlanet-WebServer-Enterprise/6.0SP1 Log Path: /usr/iplanet/servers/https-company/logs I have iPlanet log rotation enabled rotating files on a daily basis. The rotated logs are NOT compressed & are taking up too much space. I... (7 Replies)
Discussion started by: zachs
7 Replies

6. UNIX for Dummies Questions & Answers

Delete file older than three days

I am using SFTP to transmit files from the Mainframe to an UNIX server. I am looking for some kind of script that runs with SFTP to delete tranmitted files older than 3 days. Can this be done in a SFTP transmission batch job? (5 Replies)
Discussion started by: Steve Carlson
5 Replies

7. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

8. Shell Programming and Scripting

Delete files older than 3 months.(read date from the name of the file)

Guys, My log files stored in the date format format below(log_20080714072942): TIMESTAMP=`date +%Y%m%d%H%M%S` LOG=/log/log_${TIMESTAMP}.log I'm looking for a shell script which deletes all files which is older than 3 months from today. Regards, Bhagat (3 Replies)
Discussion started by: bhagat.singh-j
3 Replies

9. Shell Programming and Scripting

delete file older than N days

Hi, All, I'd like to delete files older than 1 day. I thought the following command find /your_directory -mtime +1-exec rm -f {} \; will do the work, but not so, it seems like it won't delete files unless it is 2 days old or older. the files between 1 day and 2 days old does not... (7 Replies)
Discussion started by: ericaworld
7 Replies

10. UNIX for Advanced & Expert Users

Delete user file(s) older then 'X' days ??

I want to delete any file in unix file system which is older then a week. Those files should not be unix system file..means it should be user created file. Any clue to this ?? ASAP. Thanks. (2 Replies)
Discussion started by: varungupta
2 Replies
Login or Register to Ask a Question