comparing file content differences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing file content differences
# 1  
Old 08-05-2005
MySQL comparing differences of file content

I need to write a script to find out if there are any .c files created/removed from the last time i monitored the files available. i first created a file to contain all the .c files available on the system. (ls *.c > file1)
I created another file using the same command.
I used the comm [-123] file1 file2 command to compare the differences.

Now i need to display the result in user readable form
e.g. These files has bee removed
file1.c
file2.c
These files have been created
file3.c
file4.c
OR
No files has been created/removed at all

How am i able to do that?

Last edited by RianTan; 08-06-2005 at 01:03 PM..
# 2  
Old 08-08-2005
Try somothing like this :

Code:
comm -3 /path/to/previous_list /path/to/actual_list | \
awk '
$0 ~ "^\t" {
   created[++created_idx]=$1;
   next;
}
{
   removed[++removed_idx]=$1 ;
   next;
}
END {

   if (created_idx > 0) {
      print "These files have been created"
      for (idx=1; idx <= created_idx; idx++)
         print "  " created[idx];
   } else {
      print "No files have been created"
   }

   if (removed_idx > 0) {
      print "These files have been removed"
      for (idx=1; idx <= removed_idx; idx++)
         print "  " removed[idx];
   } else {
      print "No files have been removed"
   }

} '


Jean-Pierre.
# 3  
Old 08-11-2005
doesn't seems to work. read the manual and understood what the awk command does, but ain very sure how it works. also i don't quite understand the 3rd line. i see that you uses the command like print and the way you uses the for loop. just need to clarify, can i use these command in the shell script? cause usually i used the echo command to print a message on the screen. I am running under the bourne shell or tcsh.
# 4  
Old 08-11-2005
Take a look at this. Using diff and its <, > change properties.

Code:
#! /bin/sh
# rt.sh

if [[ $# -ne 2 ]] ; then
echo "Need just 2 files to compare"
exit 1
fi ;

# Take the diff by ignoring the blank and whitespaces.
diff -b -w ${1} ${2} > ${1}.diff
        if [[ $? -eq 0 ]] ; then
        echo "No files were added/removed"
        else
        echo "Files were added/removed"
        fi ;

# Process the diff file.
# A line might look like 
# < text-which-went-out
# > text-which-came-in

while read line
do
        if [[ ${line:0:2} == "< " ]] ; then
        echo ${line:2} is removed. 
        fi ;

        if [[ ${line:0:2} == "> " ]] ; then
        echo ${line:2} is added.  
        fi ;

done < ${1}.diff

rm ${1}.diff

Run it as ./rt.sh file1 file2

Remember the old file file1 should always be the first argument. Else, the logic would reverse.

vino
# 5  
Old 08-11-2005
$0 ~ "^\t"
Selects records starting with a tabulation character.
Those records contain lines that are in file 2 only.

All the code from awk ' to the last ' is awk code (perhaps nawk or gawk on your system).
You can't use these commands directly in a shell script.

An example of my script execution :

/path/to/previous_list
file1.c
file2.c
file3.c

/path/to/actual_list
file2.c
file3.c
file4.c
file5.c

Output
These files have been created
file4.c
file5.c
These files have been removed
file1.c

The two files /path/to/previous_list and /path/to/actual_list must be sorted (needs by the command comm)


Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing time differences between 2 Solaris servers

Good day to all. I'm relatively new in using the Sun Solaris OS. I would like to request your expertise in helping to solve a problem that I have at work. Not sure if this has been asked before but I have tried searching through the internet to no avail. Basically I have 2 sun solaris... (8 Replies)
Discussion started by: Fossil_84
8 Replies

2. Shell Programming and Scripting

Comparing file ownership/permission and content of files located on two different servers

Hi All, can some one suggest me a tool to compare file ownership/permission and contents of files located at two different unix servers? Thanks, Pranav (1 Reply)
Discussion started by: Pranav Bhasker
1 Replies

3. Shell Programming and Scripting

Comparing 2 xml files and print the differences only in output

Hi....I'm having 2 xml files, one is having some special characters and another is a clean xml file does not have any special characters. Now I need one audit kind of file which will show me only from which line the special characters have been removed and the special characters. Can you please... (1 Reply)
Discussion started by: Krishanu Saha
1 Replies

4. Shell Programming and Scripting

Help comparing 2 files and sending differences

I have 2 files that need to be compared. Email the differences if something is different and don't email if nothing is different. One or both of the files could be empty. One or both could have data in them. example files backup.doc.$(date +%y%m%d) file size is 0 backup.doc.$(TZ=CST+24... (4 Replies)
Discussion started by: jabbott3
4 Replies

5. Shell Programming and Scripting

comparing 2 files and creating third file with uncommon content

I want to compare 2 files and create third file with uncommon content. e.g. file1 ajay suhas tom nisha vijay mahish file2 ajay suhas tom nisha expected output file content vijay mahish Is it possible in single command ? Thanks, Ajay (6 Replies)
Discussion started by: ajaypatil_am
6 Replies

6. Shell Programming and Scripting

Comparing two files and list the differences

Hi * I have two text files which has the file size, timestamp and the file name. I need to compare these two files and get the differences in the output format. Can anyone help me out with this. * cat file1.txt *474742 Apr 18* 2010 sample.log *135098 Apr 18* 2010 Testfile 134282 Apr 18* 2010... (7 Replies)
Discussion started by: Sendhil.Kumaran
7 Replies

7. Shell Programming and Scripting

Perl: Comparing to two files and displaying the differences

Hi, I'm new to perl and i have to write a perl script that will compare to log/txt files and display the differences. Unfortunately I'm not allowed to use any complied binaries or applications like diff or comm. So far i've across a code like this: use strict; use warnings; my $list1;... (2 Replies)
Discussion started by: dont_be_hasty
2 Replies

8. Shell Programming and Scripting

Comparing content of two variables

I have very abstract need of "comparing two variables" and take subsequent actions. please refer to image below https://lh3.googleusercontent.com/-frNk5iA3q1c/TjI3lE0sWOI/AAAAAAAAAIE/fxzB1w07gas/script_block.JPG I have a part of script which reads a file and generates variables based on... (4 Replies)
Discussion started by: animesharma
4 Replies

9. Shell Programming and Scripting

Comparing files columnwise and print the differences in third file

Hello Everybody!!!!!!!!! Request you to help me with the below mentioned issue: I have 2 files say, File 1: a|4|7 b|3|2 c|8|8 d|8|9 File 2: a|4|6 b|2|2 c|8|8 d|9|8 The third file(output file) should have: Data mismatch in row 1 column 3 Data mismatch in row 2 coumn 2 Data... (3 Replies)
Discussion started by: abhijeet1409
3 Replies

10. UNIX for Dummies Questions & Answers

comparing the content of two directories

Hello I want to compare the content of two directories recursively to check if the two directories have the same files. How can I do that? (2 Replies)
Discussion started by: xyzt
2 Replies
Login or Register to Ask a Question