Date Compare Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date Compare Script
# 1  
Old 12-02-2011
Date Compare Script

Hi All,

I wil be having Files as below in SourceFile_Path DIR
Code:
AA.20110131
AA.20110228
AA.20110202
AA.20110330
BB.20091031

I want to Keep only monthly (20110131,20110228,20100229,20110330) in First SourceFile_Path, If it is not monthly file then want to Move files to some another DIR(Target_DIR)
I need to write logic in shell Script.
Please Suggest.
Code:
SourceFile_Path=${MDIR}/MG_SrcFiles
Segment_List="Seg_List.lst"
date_List="Date_Files.lst"
Target_DIR==${TDIR}/TG_SrcFiles
#Seg_List.lst Contained
#AA
#BB

while read filelist
do
  if test -s ${SourceFile_Path}/${date_List}
  then
    ls -r -1 AA.20110330| cut -d '.' -f2>>${SourceFile_Path}/${date_List}
  else
    ls -r -1 AA.20110330| cut -d '.' -f2>${SourceFile_Path}/${date_List}
  fi
done<${Segmentlist_Path}/${Segment_List}


Last edited by Scott; 12-02-2011 at 07:40 AM.. Reason: Code tags, please...
# 2  
Old 12-02-2011
So you decide which month to keep and move the files to the appropriate directory?

Last edited by kristinu; 12-02-2011 at 07:10 AM..
# 3  
Old 12-02-2011
Hi,

I want to Keep only monthly files in FIRST DIR..
If Files are of other date, I need to move that files to another DIR.

Please Suggest me Solution.


Thanks,
Sam
# 4  
Old 12-02-2011
Got it

---------- Post updated at 06:45 AM ---------- Previous update was at 06:28 AM ----------

Have a look at this, I just put them in a long string separating them by '-' and then I use awk to get you the file names matching your date.

Code:
echo "AA.20110131-AA.20110228-AA.20110202-AA.20110330-BB.20091031" | awk ' BEGIN{RS="-"} /201102/ {print}'

---------- Post updated at 06:49 AM ---------- Previous update was at 06:45 AM ----------

You can do something like this:

Code:
ls -lrt *AA* | awk ' /201102/ {print $NF}'


Having the files

Code:
AA.20110131
AA.20110202
AA.20110228
AA.20110330
BB.20091031

you will get

Code:
AA.20110228
AA.20110202

# 5  
Old 12-02-2011
Thanks a lot for Reply..

But in
ls -lrt *AA* | awk ' /201102/ {print $NF}'
201102 I can't Hardcode it. It should be dynamic.

After some time AA.20061111 files wil come, In that case need this also..

Thanks,
Sam
# 6  
Old 12-02-2011
If you want to keep files, you can use the following logic:

Code:
# In this case it will look for directories from 30 until 50 (30 + 20) days ago!
countValBack=20
initVal=30
countValues=`expr ${initVal} + ${countValBack}`

basePath="./"

while [ ${countValues} -ge ${initVal} ]
do
	currDate=`date -d "${countValues} days ago" +"%Y%m%d"`
	bpDate="${basePath}/${currDate}"
	if [ -d "${bpDate}" ]
	then
		echo "Removing directory: [${bpDate}] - [${countValues}] days ago..."
		# rm -rf "${bpDate}"
		if [ ${?} -eq 0 ]
		then
			echo "Removed successffully!"
		else
			echo "Failed to remove."
		fi
	fi
	
	countValues=`expr ${countValues} - 1`
done

This in in this post!

A few changes will suit your needs!

I hope it helps.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare Date to today's date in shell script

Hi Community! Following on from this code in another thread: #!/bin/bash file_string=`/bin/cat date.txt | /usr/bin/awk '{print $5,$4,$7,$6,$8}'` file_date=`/bin/date -d "$file_string"` file_epoch=`/bin/date -d "$file_string" +%s` now_epoch=`/bin/date +%s` if then #let... (2 Replies)
Discussion started by: Greenage
2 Replies

2. UNIX for Beginners Questions & Answers

Compare date bash script

I all I have written a bash script for compare two date. One of those is a result of query, and another is current date. I have a problem with the format, because the first is 09/12/19 18:50:30 but for having this result I have to do d1DB=$(date -d "$valData" +'%m/%d/%y %T') and the second... (9 Replies)
Discussion started by: rdie77
9 Replies

3. Answers to Frequently Asked Questions

Compare date in .txt with system date and remove if it's lesser than system date

I m working on shell scripting and I m stuck where in my .txt file there is column as expiry date and I need to compare that date with system date and need to remove all the rows where expiry date is less than system date and create a new .txt with update. (1 Reply)
Discussion started by: Stuti
1 Replies

4. UNIX for Beginners Questions & Answers

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
2 Replies

5. Shell Programming and Scripting

Compare the system date with date from a text file

I get the date that's inside a text file and assigned it to a variable. When I grep the date from the file, I get this, Not After : Jul 28 14:09:57 2017 GMT So I only crop out the date, with this command echo $dateFile | cut -d ':' -f 2,4The result would be Jul 28 14:57 2017 GMT How do I... (3 Replies)
Discussion started by: Loc
3 Replies

6. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

7. Shell Programming and Scripting

Adding days to system date then compare to a date

Hi! I am trying to read a file and every line has a specific date as one of its fields. I want to take that date and compare it to the date today plus 6 days. while read line do date=substr($line, $datepos, 8) #date is expected to be YYYYMMDD if ; then ...proceed commands ... (1 Reply)
Discussion started by: kokoro
1 Replies

8. Shell Programming and Scripting

ksh compare dates INSIDE a file (ie date A is > date B)

In KSH, I am pasting 2 almost identical files together and each one has a date and time on each line. I need to determine if the first instance of the date/time is greater than the 2nd instance of the date/time. If the first instance is greater, I just need to echo that line. I thought I would... (4 Replies)
Discussion started by: right_coaster
4 Replies

9. Shell Programming and Scripting

Shell Script to compare files, check current date and count

Hello - I have written the following basic shell script to count files, compare files and look for a particular strings in a file. Problem 1: How do I define more than 1 file location? #!/bin/bash #this is a test script FILES=$(ls /home/student/bin/dir1, home/student/bin/dir2)... (0 Replies)
Discussion started by: DallasT
0 Replies

10. Shell Programming and Scripting

Compare date from db2 table to yesterday's Unix system date

I am currently running the following Korn shell script which works fine: #!/usr/bin/ksh count=`db2 -x "select count(*) from schema.tablename"` echo "count" I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
Discussion started by: sasaliasim
9 Replies
Login or Register to Ask a Question