Script to delete old entries from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to delete old entries from a file
# 1  
Old 01-18-2013
Script to delete old entries from a file

I need to remove the file entries that are 30 days old. I have a file with the below data.
Code:
111.XX.XX.99 - - [13/Oct/2012:22:27:35 -0400] "GET /folder1/wp-content" 304
111.XX.XX.99 - - [04/Nov/2012:22:27:35 -0400] "GET /folder1/wp-content" 304
111.XX.XX.99 - - [01/Dec/2012:22:27:35 -0400] "GET /folder1/wp-content" 304
111.XX.XX.99 - - [01/Dec/2012:22:27:35 -0400] "GET /folder1/wp-content" 304
111.XX.XX.99 - - [16/Dec/2012:22:27:35 -0400] "GET /folder1/wp-content" 304
111.XX.XX.99 - - [17/Dec/2012:22:27:35 -0400] "GET /folder1/wp-content" 304
111.XX.XX.99 - - [22/Dec/2012:22:27:35 -0400] "GET /folder1/wp-content" 304
111.XX.XX.99 - - [17/Jan/2013:23:27:35 -0400] "GET /folder1/wp-content" 304
111.XX.XX.99 - - [17/Jan/2013:22:27:35 -0400] "GET /folder1/wp-content" 304

After applying the script to the file, the output should be as follows.

Code:
111.XX.XX.99 - - [17/Dec/2012:22:27:35 -0400] "GET /folder1/wp-content" 304
111.XX.XX.99 - - [22/Dec/2012:22:27:35 -0400] "GET /folder1/wp-content" 304
111.XX.XX.99 - - [17/Jan/2013:23:27:35 -0400] "GET /folder1/wp-content" 304
111.XX.XX.99 - - [17/Jan/2013:22:27:35 -0400] "GET /folder1/wp-content" 304

The lines which is old more than 30 days should be removed from the output.
# 2  
Old 01-18-2013
Why need a script? A "FIND" command is sufficient!

Code:
find <directory> -type f -mtime +30 -exec rm -f {} \;

You can build your command from the site BUILD FIND

Last edited by PikK45; 01-18-2013 at 04:12 AM.. Reason: Addition :)
# 3  
Old 01-18-2013
Quote:
Originally Posted by PikK45
Why need a script? A "FIND" command is sufficient!

Code:
find <directory> -type f -mtime +30 -exec rm -f {} \;

You can build your command from the site BUILD FIND
I dont need to delete files that are old, but the contents inside a file that are 30 days old. We can check the date and delete lines that are date pattern older than 30 days.
# 4  
Old 01-18-2013
is your file sorted according to date?
# 5  
Old 01-18-2013
Quote:
Originally Posted by PikK45
is your file sorted according to date?
Please read my first post clearly. I have expalined it clearly Smilie
# 6  
Old 01-18-2013
A simple algorithm would be following.

Get the current date store it in a variable
While you read a file using Readdir,
open a file handle
start reading a file using that file handle (read by line)
Now keep copying the content of file into another bkp file until you reach the day which is 30 days older from today (refer my second line)
once that is done close FH, and unlink the file (delete it) and name the bkp file as originial one.
End of while loop.

Let me know how that works out - if you didn't get it send me sample file I'll write a small script demonstrating how to do that.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with UNIX script --Read from one file and delete entries in other

Hi Guru's The script has to read an entry from one file and delete the set of lines form other file. Below is the format of the file. In the below example, script should read the entries from input file 2 and delete the entries from input file 1. Input file 1 cn: test@test1.com abc:... (7 Replies)
Discussion started by: Samingla
7 Replies

2. Shell Programming and Scripting

How to delete the lines from file using script?

Hi Iam having file like below 10.238.52.65 pun-ras-bng-mhs-01 server 10.238.52.65 pun-ras-bng-mhs-01 10.10.10.10 10.238.52.65 pun-ras-bng-mhs-01 10.10.20.10 10.238.54.1 enk-ras-bng-cse-01 server 10.238.54.1 enk-ras-bng-cse-01 10.10.30.10 10.238.54.1 enk-ras-bng-cse-01 10.10.10.10 ... (5 Replies)
Discussion started by: surender reddy
5 Replies

3. 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

4. Shell Programming and Scripting

Delete old date entries from a file

Hello, I have following log file. alog.log Oct 8 xxxxx Oct 9 xxxxx Oct 10 xxxxx Nov 8 xxxxx Nov 8 xxxxx . . . Jan 8 xxxxx I need to delete all the lines which are having date entries older than 60 days. So If the current date is Jan 9 2011 then it should delete all the lines upto... (3 Replies)
Discussion started by: Ravin83
3 Replies

5. Shell Programming and Scripting

Script that delete a File when a DEL File will be placed in same folder

Hi there, i have a question. I have a folder called /usr/test There is a file in it.... test.csv I need not a shell script that checks if there is a file called: test.del And if the file is in the same folder then the script should delete the test.csv and also the test.del. Hope... (9 Replies)
Discussion started by: Bjoern28
9 Replies

6. Shell Programming and Scripting

Logout script - delete a file

Good evening you scripting guru's! I have the following script to delete a file on logout of a Mac : find . –name com.apple.sidebarlists.plist –exec rm -f {} \; It works fine in terminal and when logging out. However, it takes forever to run the script at logout...A minute and a half. ... (12 Replies)
Discussion started by: AGGreenLinux
12 Replies

7. Shell Programming and Scripting

Delete content of file 1 in file 2 with shell script

OK, best is I explain what the operating enviroment is. Linux, but Motomagx. It is a Linux operated mobile phone, Motorola V8. I am writting a shell script, but got stuck. I have to delete the complete content of file 1 in file 2. I have attached the 2 files. You can see that the content of... (2 Replies)
Discussion started by: rasputin007
2 Replies

8. Shell Programming and Scripting

Need Script to delete multiple entries from a file

I am new to Linux and would appreciate some help. I need a script to do the following: My file contains the following entries more file 1 1 1 1 2 2 2 I want to delete multiple entries of 1 and 2 and just keep one of each. The output should look like more file 1 2 Thanks (1 Reply)
Discussion started by: sidewayz
1 Replies

9. UNIX for Dummies Questions & Answers

find and delete reccuring entries in a file

I have a file with multiple lines, lets say for example magazine1 magazine2 magazine3 magazine3 magazine4 magazine5 So i am trying to find a way to find and delete the duplicate entries. Any ideas? Thanks (3 Replies)
Discussion started by: greekozz
3 Replies

10. Shell Programming and Scripting

How to Delete all lines in a file from a script

I am trying to delete all the lines out a file from a unix script. Please help Platform is Sun (3 Replies)
Discussion started by: alnita
3 Replies
Login or Register to Ask a Question