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?
# 8  
Old 07-29-2014
If you want to compare every file to every other file just once, try (given you get rid of your find -cmin error):
Code:
find * -cmin -60 | while read FN1
   do find * -cmin -60 | tac | while read FN2
      do [ "$FN1" = "$FN2" ] && break
          diff  "$FN1"  "$FN2"
      done
   done

This User Gave Thanks to RudiC For This Post:
# 9  
Old 07-29-2014
Quote:
Originally Posted by Don Cragun
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
)

Hi Don Cragun
for my script, after finding the files, I need to output to tmp file then do something, then compare.
for below code, how can I put them in while loop.

Code:
 
find srch_dir -newer "$IAm.$$" -name 'abc2014*' | (
read file1
while read file2
do diff "$file1" "$file2"
done
)

thanks in advance
# 10  
Old 07-29-2014
Quote:
Originally Posted by ken6503
Hi Don Cragun
for my script, after finding the files, I need to output to tmp file then do something, then compare.
for below code, how can I put them in while loop.

Code:
 
find srch_dir -newer "$IAm.$$" -name 'abc2014*' | (
read file1
while read file2
do diff "$file1" "$file2"
done
)

thanks in advance
Can you be any more vague???
Try:
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	echo 'who knows what' > tmpfile	# output to temp file
		# do something else here
		diff "$file1" "$file2"	# compare
	done
)

This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 07-29-2014
So where does this bit come in if the find doesn't work?

Quote:
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

This User Gave Thanks to rbatte1 For This Post:
# 12  
Old 07-29-2014
Quote:
Originally Posted by Don Cragun
Can you be any more vague???
Try:
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    echo 'who knows what' > tmpfile    # output to temp file
        # do something else here
        diff "$file1" "$file2"    # compare
    done
)

Thanks for your reply.

sorry for the vague???

when first time running the script there is no any files in the directory. the find command only find one file, I don't need to compare. what i want to do is after finding the file(s), first I want to count how many files I found, if it is one, I don't compare; if it is two files, I will compare.

hopefully, this time is a little bit clear.

thanks in advance.
# 13  
Old 07-29-2014
Why do you want to count the files. The script I gave you won't do anything unless there are at least two files. If there are two or more files, it will execute the commands in the while loop once for each file except the 1st file. The name of the 1st file is preserved in the variable file1 and doesn't change while the name of each remaining file is stored in the variable file2 as it is processed in the loop.

You have said you want to create a temp file, but you haven't shown us what would go into that temp for nor what it would be used for. Then you said you want to do something else, but you haven't said what. Then, if there are two or more files, you said you want to compare the files; the script I gave you does that.

Did you try running the script I gave you when 0, 1, 2, and 3 files with names starting with abc2014 were present that had been created within the last hour? For each case, what did the script do wrong?
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