LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-04-2008, 04:36 PM   #1
MaureenT
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Rep: Reputation: 0
script help - pull date format out of file name and compare to today


I'm looking for some assistance in pulling a date format out of a file name and comparing it to the current date along with a retention value to determine whether or not to delete the file.

I'm reading 3 values from a .csv file
1. path to files
2. common naming convention of files
3. retention in days

.csv file would look like this:
/home/myhome/,logtype1_,4
/logs,logtype2_,20

filenames are in format logtype1_yymmdd.log
I want to pull out the yymmdd, compare it to todays date and the retention time and determine whether or not to delete it. I just need to strip off the common name which is defined and .log.

The values in the .csv file get read into following variables:
PTH
NAME
DAYS

I can easily find the files by that name in the path using this cmd:
find $PTH -name $NAME*

I need help using probably awk or sed to strip the date out of the filename, compare it and remove the file if it's older than the retention period.
it would be ideal if I could do it on 1 line with a find -exec cmd.

I don't want to simply compare dates using a find function on create or modify date for a whole folder. I want to be specific about the files being removed.

If you post code back could you try to provide a bit of an explanation as to how the code works. Thanks.
 
Old 11-04-2008, 05:45 PM   #2
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
Since you have a well defined filename, you can use parameter substitution to dig out the items you need right in a bash shell. See Parameter Expansion in man bash.

#!/bin/sh
# your_process FILENAME
FILENAME=${1%.*} # this is the filename without the .log
LOGTYPE=${1%_*} # this is everything up to the _ in the filename
DATE=${1: -10:6} # this is the six characters starting 10 in from the end

Dave
 
Old 11-04-2008, 07:02 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,344

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
This will give you today's date in same format:

today=`date +%y%m%d`

then all you need to do is compare (subtract) the 2 values
 
Old 11-06-2008, 09:33 AM   #4
MaureenT
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Still having issues

So I've tried to play around with your examples and do some reading but I'm not providing filenames as variables to a script.
I'm trying to do something like this:

For FILE in 'find $PTH - name $NAME*`; do
FDATE=method to strip yyyymmdd out of FILE
compare FDATE to TODAY
if diff greater than retention then
rm $FILE
done

I need a method to read FILE and strip out dates.
I'm looking at cut, sed, awk??
Maybe something like echo $FILE | sed -e s/^logname_//
Just trying to read up on sed and awk now.
I'm guessing I may also have to do something to convert FDATE from string to value for comparison to TODAY?
 
Old 11-06-2008, 10:22 AM   #5
MaureenT
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Get it

Sorry, I get your substitution example now. Found something similar that put it together for me.

Here is my test example that works:
for FILE in `find $PTH -name $NAME*`
do
FDATE=${FILE: -12:8}
echo $FILE
echo $FDATE
done

This will echo 8 characters starting 12 characters from end of file name so as long as your filenames are always something_yyyymmdd.log, it will pull out yyyymmdd portion. Thanks for assistance.

Last edited by MaureenT; 11-06-2008 at 10:30 AM. Reason: errors in what I had previously posted and this is more succinct
 
Old 11-06-2008, 11:14 AM   #6
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
Good. Now you also said you'd like a single command. After you get the script working, you can save it in a local location like /usr/local/bin/ and, if you use full path names (cron's environment is unique), you can then put a rotation routine like this in your crontab using a single line to call it.

Parameter substitution is seldom used but very powerful and works well in applications like yours. One of the good features of the bash shell, IMHO.

Dave
 
  


Reply

Tags
awk, find, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need assistance with shell script to pull MP3 file Cpare Linux - Newbie 5 06-28-2008 08:53 PM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
Recursive diff ( File compare ) script sharathkv25 Programming 5 02-22-2007 08:39 AM
Script to compare file size nazs Programming 6 05-24-2006 10:10 AM
Shell Script.. Date format handling C-RAF. Programming 2 02-14-2006 08:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration