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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to compare two files of todays date and yesterday's date
# 1  
Old 12-01-2014
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...?
# 2  
Old 12-01-2014
One way:

Code:
 
#!/bin/bash
 
file1=$(date '+%Y%m%d')
file2=$(date -d "1 day ago" '+%Y%m%d')
file1=$file1"_file.txt"
file2=$file2"_file.txt"
diff $file1 $file2

# 3  
Old 12-01-2014
Hello hemanthsaikumar,

If you are saying files are of same date or not we can use following, if you need to compare data of these files then
kindly let us know some input data on same so that we can help more.

Code:
cat compare_fles.ksh
A=$1
B=$2
awk -vC=`echo $A` -vD=`echo $B` 'BEGIN{gsub(/\_.*/,X,C);gsub(/\_.*/,X,D);if(C == D){print "Files are of same date."} else {print "Files are NOT same date."}}'

After running script we will get following output.
Code:
./compare_fles.ksh 20141201_file.txt 20141130_file2.txt

Output will be as follows.

Code:
Files are NOT same date.



Thanks,
R. Singh
# 4  
Old 12-01-2014
hi all,,
thanks for the reply... but i need to pass the date in the parameter so that it should compare with the previous date file ... for example

if my date is 20141201_file.txt then it should compare with 20141130_file.txt ,the date in the file name is getting generated through the parameter iam passing ..iam passing the date parameter in the below format 2014-12-01 to generate the file with name 20141201_file.txt
# 5  
Old 12-01-2014
Quote:
Originally Posted by hemanthsaikumar
hi all,,
thanks for the reply... but i need to pass the date in the parameter so that it should compare with the previous date file ... for example

if my date is 20141201_file.txt then it should compare with 20141130_file.txt ,the date in the file name is getting generated through the parameter iam passing ..iam passing the date parameter in the below format 2014-12-01 to generate the file with name 20141201_file.txt
Hello hemanthsaikumar,

Following may help you in same.
Code:
DATE1=$1
NAME1=`echo $DATE1 | awk '{gsub(/\-/,X,$0);print $0}'`
VALUE=`find -type f -name "$NAME1""_file.txt"`
if [[ -n $VALUE ]]
then
        echo "File named" "$NAME1""_file.txt" "has been found."
else
        echo "File named" "$NAME1""_file.txt" "has NOT been found."
fi

Run the script as follows.
Code:
./check_date12.ksh 2014-11-30

Output will be as follows.
Code:
File named 20141130_file.txt has been found.

Another example where file is not found in directory.
Code:
./check_date12.ksh 2014-15-30

Output will be as follows.
Code:
File named 20141530_file.txt has NOT been found.


Hope this helps.


EDIT: Sorry I didn't understand requirement before following may help.

Code:
DATE=`date`
DATE1=`date -d"1 day ago"`
value1=`find -type f -name"$DATE""file.txt"`
value=`find -type f -name"$DATE1""file.txt"`
if [[ -n $value && -n value1 ]]
then
 diff $value $value1
else
 echo "Either file named " "$value1" " OR file named " "$value" " has NOT been found."
fi


Thanks,
R. Singh

Last edited by RavinderSingh13; 12-01-2014 at 09:29 AM.. Reason: Added correct solution now
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. Shell Programming and Scripting

[Solved] Replace yesterday date with today's date except from the first line

Hello, I have a file like this: 2012112920121130 12345620121130msABowwiqiq 34477420121129amABamauee e7748420121130ehABeheheei in case the content of the file has the date of yesterday within the lines containing pattern AB this should be replaced by the current date. But if I use... (3 Replies)
Discussion started by: Lilu_CK
3 Replies

3. Shell Programming and Scripting

Need help in Shell Script comparing todays date with Yesterday date from Sysdate

Hi, I want to compare today's date(DDMMYYYY) with yesterday(DDMMYYYY) from system date,if (today month = yesterday month) then execute alter query else do nothing. The above requirement i want in Shell script(KSH)... Can any one please help me? Double post, continued here. (0 Replies)
Discussion started by: kumarmsk1331
0 Replies

4. Shell Programming and Scripting

Find files having todays date and hostname in its name.

Hello, I am quite new to unix/shell and want to write a script using bash which will process the files. Basically i want to search files having name as "date+hostname+somestring.out" i am using below variables and then will use them in find command :- TODAY_DATE=$('date +%d')... (5 Replies)
Discussion started by: apm
5 Replies

5. Shell Programming and Scripting

Getting yesterday's date in shell script

Im in EST, and im using the command CurrentDate=`TZ="EST+24" date +'%y%m%d'` to get the yesterday's date. Does this work perfectly for the boundary conditions of month end or year end(leap year) etc ? (2 Replies)
Discussion started by: prasperl
2 Replies

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

7. Shell Programming and Scripting

Problem to get yesterday's date flatfile with shell script

hi, i was required to write a shell script to get yesterday's date flatfile. but i only know how to get today's date flatfile. Please observed my below scripting: Please help! Thanks ================================================= #!/bin/sh HOST='192.168.1.200' USER='ftp1'... (19 Replies)
Discussion started by: lifeseries
19 Replies

8. Shell Programming and Scripting

Shell script help to eliminate files of todays date

Hi I am very new to shell scripting and have written a script (below). However the directory I am searching will contain a file with a .trn extension each day which I want to eliminate. Each day the file extension overnight will change to trx, if this fails I want to know. Basically what I... (2 Replies)
Discussion started by: richM
2 Replies

9. Linux

Comparing the file drop date with todays date

Daily one file will dropped into this directory. Directory: /opt/app/jt/drop File name: XXXX_<timestamp>.dat.gz I need to write a script which checks whether the file is dropped daily or not. Any idea in the script how can we compare timestamp of the file to today's date?? (3 Replies)
Discussion started by: Rajneel
3 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