How to compare two file contents which created within one hour?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare two file contents which created within one hour?
# 1  
Old 07-29-2014
How to compare two file contents which created within one hour?

Hi Gurus,

I need to compare two file contents and log the difference. the detail as below:
in current directory there is file
Code:
 
abc20140728_1020.txt
abc20140728_1110.txt

I need to find the files which created within 60 minutes first then compare the contents.
I am able to use find command find the files but I don't know how to compare these two file contents.

Code:
 
for filename in `find srch_dir -cmin -60 -name abc2014*`
do
here i don't know how to store the first file then use diff $filename1 $filename2
done

Please help me on this issue.

thanks in advance

Last edited by ken6503; 07-29-2014 at 01:18 PM..
# 2  
Old 07-29-2014
I must admit that I don't know either. Perhaps an alternate logic might help though:-
  • List all the files that match your criteria and write them to a file (listfile)
  • Get top line of listfile and set that as your main file (file1)
  • Get all other lines of your listfile and in a loop, compare them to file1
Would that be suitable?

In any case, if you have files a, b, c, d & e though, how will you decide which is the main file to be compared to, i.e. file1




Robin
# 3  
Old 07-29-2014
Assuming that any file is suitable as the file to be compared against all of the others, try something like:
Code:
find srch_dir -cmin -60 -name 'abc2014*' | (
	read file1
	while read file2
	do	diff "$file1" "$file2"
	done
)

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 07-29-2014
Quote:
Originally Posted by Don Cragun
Assuming that any file is suitable as the file to be compared against all of the others, try something like:
Code:
find srch_dir -cmin -60 -name 'abc2014*' | (
    read file1
    while read file2
    do    diff "$file1" "$file2"
    done
)

thanks Don Cragun

my unix is solaris. when I run
Code:
 
find srch_dir -cmin -60 -name

I got error.
any idea how to convert this to Solaris command.

thanks in advance.
# 5  
Old 07-29-2014
Dear ken6503,

It would be helpful if you posted the error.



Robin
# 6  
Old 07-29-2014
As long as you don't run it any day before 1am, you could try something like:
Code:
#!/bin/ksh
IAm=${0##*/}
read x y z <<-EOF
	$(date +'%m%d %H %M%n')
EOF
trap 'rm -f "$IAm.$$"' EXIT
touch -t $(printf '%s%02d%s' $x $((${y#0} - 1)) $z) "$IAm.$$"
find srch_dir -newer "$IAm.$$" -name 'abc2014*' | (
	read file1
	while read file2
	do	diff "$file1" "$file2"
	done
)


Last edited by Don Cragun; 07-29-2014 at 05:55 PM..
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 07-29-2014
Quote:
Originally Posted by rbatte1
Dear ken6503,

It would be helpful if you posted the error.



Robin
Hi Robin

the error as below:
Code:
 
 
find: bad option -cmin
find: [-H | -L] path-list predicate-list

thanks in advance
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. UNIX for Beginners Questions & Answers

View file contents created by dbCAmplicons as tab delimited

I have run the following command : od -c Results_May18.fixrank | head Here is the result. I wanted the results in tab delimited. Thanks $ od -c Results_May18.fixrank | head 0000000 M 0 1 6 0 1 : 1 2 9 : 0 0 0 0 0 0000020 0 0 0 0 - A T T D Y ... (2 Replies)
Discussion started by: Benard
2 Replies

3. Shell Programming and Scripting

How to find the files created within one hour in Solaris?

Hi Gurus, I want to find the file created within one hour in solaris. I have tried below command, but it is no lucky. $find . -mtime -1/24, -name "abc*" above command give me the file name which created two hours ago find . -cmin -60, -name "abc*" above command I got error as... (4 Replies)
Discussion started by: ken6503
4 Replies

4. Shell Programming and Scripting

shell script to compare file contents

Hello Has anyone got an example shell script that I can use to compare the contents of two files. The files should contain the same contents, eg. file1.txt apple pear grape file2.txt apple pear grape (2 Replies)
Discussion started by: deedaz
2 Replies

5. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

6. Shell Programming and Scripting

Compare file timestamp with current date. Diff must be 1 hour.

Hello, I've created the script below to compare the content of two files with a delay of an hour. After an hour, the lines that exist in both files, will be printed and executed. The script now uses a counter to countdown 50 minutes. But what I would prefer is to check the file timestamp of... (3 Replies)
Discussion started by: taipan
3 Replies

7. Shell Programming and Scripting

Compare two files and remove all the contents of one file from another

Hi, I have two files, in which the second file has exactly the same contents of the first file with some additional records. Now, if I want to remove those matching lines from file2 and print only the extra contents which the first file does not have, I could use the below unsophisticated... (3 Replies)
Discussion started by: royalibrahim
3 Replies

8. UNIX for Dummies Questions & Answers

compare array contents with file

I have an array "arrA" with the following contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf My data file "Files.dat" has the same contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf I have a script that compares... (0 Replies)
Discussion started by: orahi001
0 Replies

9. UNIX for Dummies Questions & Answers

To create alist of files created in the last 1 hour.

Hi, I need to create a script whcih ill run ever hour and will check all the files which are created in that hour for an error string. Need help in finding all the files which were created in the last 1 hour. Thanks in Advance. Asheesh (4 Replies)
Discussion started by: Asheesh
4 Replies

10. Shell Programming and Scripting

Need to process files created an hour ago

Hello all, I would like to ask for an advice on how to deal with the following scenario. Every now and then, our ERP system creates an interface text file with the following file format - XORD????.DLD where ???? is a sequence number. We can have 1 or more XORD files created in an hour. ... (9 Replies)
Discussion started by: negixx
9 Replies
Login or Register to Ask a Question