Comparing two files and list the differences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing two files and list the differences
# 1  
Old 11-29-2011
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 lib
1024 Jun 13 17:49 checksum.txt
*3768008 Oct 19 11:59 reports
2644541 Oct 19 22:07 feed.log
3768008 Oct 19 22.51 quest
0 Nov 29 10:34 textfile.txt
*
cat file2.txt
474742 Apr 18* 2010 sample.log
134282 Apr 18* 2010 lib
135098 May 2010 Testfile
4068258 May 23* 2011 release.txt
*4068258 May 23* 2011 deploy.log
3768008 Oct 19 11:59 reports
2644541 Oct 19 22:07 feed.log
10430 Nov 29 10:43 textfile.txt
*
Desired output:
Differences between two files:
*
file1:
*135098 Apr 18* 2010 Testfile
1024 Jun 13 17:49 checksum.txt
3768008 Oct 19 22.51 quest
0 Nov 29 10:34 textfile.txt
*
file2:
135098 May 2010 Testfile
4068258 May 23* 2011 release.txt
*4068258 May 23* 2011 deploy.log
10430 Nov 29 10:43 textfile.txt
*
I tried using diff but did not the appropriate output. Please help me out.

Thanks & Regards
Sendhil

---------- Post updated at 02:48 PM ---------- Previous update was at 02:45 PM ----------

Please ignore * . typo for space. Where Everett there is * its actually a blank space.
# 2  
Old 11-29-2011
Please use code tags to preserve spaces in your output. Check the forums tutorial on how to use them:https://www.unix.com/how-post-unix-li...code-tags.html
# 3  
Old 11-29-2011
If you posted using code tags, you wouldn't need to use * for space.

Please post in code tags so we can see what your code actually looks like.
# 4  
Old 11-29-2011
Using grep:
Code:
~/unix.com$ grep -Fvf file2 file1

Also, please use CODE tags so you won't get annoyed with blank spaces and/or tabs
# 5  
Old 11-30-2011
Apologize folks, earlier i posted through mobile lot of typos and auto corrections.

Here is my scenario. I need to write a script to get the differences between two files (file1. txt and file2.txt) in the desired format as mentioned. I would really appreciate your help. Thank you all in advance.

Code:
cat file1.txt
474742 Apr 18 2010 sample.log
135098 Apr 18 2010 Testfile
134282 Apr 18 2010 lib
1024 Jun 13 17:49 checksum.txt
3768008 Oct 19 11:59 reports
2644541 Oct 19 22:07 feed.log
3768008 Oct 19 22.51 quest
0 Nov 29 10:34 textfile.txt

cat file2.txt
474742 Apr 18 2010 sample.log
134282 Apr 18 2010 lib
135098 May 2010 Testfile
4068258 May 23 2011 release.txt
4068258 May 23 2011 deploy.log
3768008 Oct 19 11:59 reports
2644541 Oct 19 22:07 feed.log
10430 Nov 29 10:43 textfile.txt

Desired output:
Differences between two files:

file1:
135098 Apr 18 2010 Testfile
1024 Jun 13 17:49 checksum.txt
3768008 Oct 19 22.51 quest
0 Nov 29 10:34 textfile.txt

file2:
135098 May 2010 Testfile
4068258 May 23 2011 release.txt
4068258 May 23 2011 deploy.log
10430 Nov 29 10:43 textfile.txt

---End---

# 6  
Old 11-30-2011
For File1 output
Code:
$ fgrep -vf file2 file1

For File2 output
Code:
$ fgrep -vf file1 file2

# 7  
Old 11-30-2011
Try this,
Code:
awk 'BEGIN{print "File1"}  NR==FNR{a[$0]++;next} {if($0 in a){a[$0]="-1"}else{print}} END {print "\nFile2";for(i in a) { if(a[i] != "-1" ){print i}}}' file2 file1

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 two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

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

3. Shell Programming and Scripting

Need to compare the two files and list out differences between the two

Hi, I need to compare the two files and list out difference between the two. Please assist. Best regards, Vishal (2 Replies)
Discussion started by: Vishal_dba
2 Replies

4. Shell Programming and Scripting

Comparing list of files

Hi All, I have a list of files and want to find the difference between each file if there is difference then i want the file name to be printed and log the duplicate files For example Size Filename 23 a1 23 a2 23 a3 45 a4 If i diff a1 and a2 and find... (3 Replies)
Discussion started by: wedng.bell
3 Replies

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

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

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

Differences between 2 Flat Files and process the differences

Hi Hope you are having a great weeknd !! I had a question and need your expertise for this : I have 2 files File1 & File2(of same structure) which I need to compare on some columns. I need to find the values which are there in File2 but not in File 1 and put the Differences in another file... (5 Replies)
Discussion started by: newbie_8398
5 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. Shell Programming and Scripting

comparing file content differences

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 file1... (4 Replies)
Discussion started by: RianTan
4 Replies
Login or Register to Ask a Question