Compare 2 files using third file as an exception


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 2 files using third file as an exception
# 1  
Old 02-13-2014
Compare 2 files using third file as an exception

Hi All,

Is it possible to compare 2 files using the contents of a third file as a guide using a bash script?

I have 2 files I want t compare (1 and 2 below) the difference between the two is the first line (a)

I want my script to ignore this difference if the difference is stored in file 3.

Is it possible to compare two files with the contents of the 3rd acting as an exception?

Quote:
File 1
a
b
c

File 2

b
c


File 3
a
Thanks
# 2  
Old 02-13-2014
The comm utility should be useful for this:

Code:
$ man comm
COMM(1)                          User Commands                         COMM(1)



NAME
       comm - compare two sorted files line by line

SYNOPSIS
       comm [OPTION]... FILE1 FILE2

DESCRIPTION
       Compare sorted files FILE1 and FILE2 line by line.

       With  no  options,  produce  three-column  output.  Column one contains
       lines unique to FILE1, column two contains lines unique to  FILE2,  and
       column three contains lines common to both files.

       -1     suppress column 1 (lines unique to FILE1)

       -2     suppress column 2 (lines unique to FILE2)

       -3     suppress column 3 (lines that appear in both files)
...

And you can use awk to check for exceptions.

So, comm -2 -3 file1 file2 | awk 'NR==FNR { A[$0]=1 ; next } !($0 in A)' file3 -
# 3  
Old 02-13-2014
Hi Corona whether its file3 - or - file3 ? I am bit confused.

OR
Code:
$ awk 'FNR==1{++i}i==1{A[$0];next}i==2 && ($0 in A){delete A[$0];next}!($0 in A)' file1 file2 file3

# 4  
Old 02-13-2014
Hi Guys,

Thanks for your quick responses.

Both of the above work as the files are now.

However if I make a change to file2 like remove b. I am returned with a blank line.

I should have mentioned above that if there is a difference and it is not noted in file3 I would like it to be returned if possible.

Thanks
# 5  
Old 02-13-2014
Quote:
Originally Posted by Akshay Hegde
Hi Corona whether its file3 - or - file3 ? I am bit confused.
You want it to read the list of rejects first -- only then can it tell which things from standard input to ignore. So it is file3 -.
# 6  
Old 02-13-2014
Thanks Corona... actually because of this I confused, trying to reset mind Smilie

Code:
$ echo "Pipe -- 1st" | awk '1' file3 -
a 
Pipe -- 1st

$ echo "Pipe -- 1st" | awk 'FNR==NR{print $0,"stdin";next}{print $0,FILENAME}' - file3
Pipe -- 1st stdin
a  3

$ echo "Pipe -- 1st" | awk 'FNR==NR{print $0,"stdin";next}{print $0,FILENAME}' file3 -
a  stdin
Pipe -- 1st -

---------- Post updated Feb 14th, 2014 at 12:14 AM ---------- Previous update was Feb 13th, 2014 at 11:47 PM ----------

@ nwalsh88 : show your real input as well as desired output (use codetags).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare 2 files and create a result file with unmatched lines from first file.?

HI, I have 2 text files. file1 and file2. file1.txt (There are no duplicates in this file) 1234 3232 4343 3435 6564 6767 1213 file2.txt 1234,wq,wewe,qwqw 1234,as,dfdf,dfdf 4343,asas,sdds,dsds 6767,asas,fdfd,fdffd I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Discussion started by: Little
6 Replies

2. UNIX for Dummies Questions & Answers

To compare two files,Output into a new file

Hi Please help me to compare two files and output into a new file file1.txt 15114933 |4001 15291649 |933502 15764675 |4316 15764678 |4316 15761974 |282501 15673104 |933505 15673577 |933505 15673098 |933505 15673096 |933505 15673092 |933505 15760705 ... (13 Replies)
Discussion started by: Ankita Talukdar
13 Replies

3. UNIX for Dummies Questions & Answers

Compare two files and output to new file

Hi, Please help How to compare two files- Any mismatches 2nd and 3rd column's values corresponding to 1st column. file1 15294024|Not Allowed|null 15291398|Not Allowed|null 15303292|Dropship (standard)|N 15303291|Dropship (standard)|N 15275561|Store Only|Y 15275560|Store Only|Y... (2 Replies)
Discussion started by: Ankita Talukdar
2 Replies

4. Shell Programming and Scripting

Monitor logs for exception and if exception come then sent an email

Hi Folks, please advise , I have logs generated on unix machine at location /ops/opt/aaa/bvg.log , now sometimes there come exception in these logs also, so I want to write such a script such that it should continuously monitor these logs and whenever any exception comes that is it try to find... (3 Replies)
Discussion started by: tuntun27272727
3 Replies

5. Shell Programming and Scripting

Disk Quota Exception while deleting the files

This is the full file name I get when I do ls -lt from my current directory. EXPORT_v1x0_20120811_11_T_065800_070000.dat.gz File names also consist of date as well. In the above file date is 20120811. So I am trying to delete all the files which starts with EXPORT_v1x0 and whose... (7 Replies)
Discussion started by: raihan26
7 Replies

6. Shell Programming and Scripting

Compare two files, then overwrite first file with only that in both files

I want to compare two files, and search for items that are in both. Then override the first file with that containing only elements which were in both files. I imagine something with diff, but not sure. File 1 One Two Three Four Five File 2 One Three Four Six Eight (2 Replies)
Discussion started by: castrojc
2 Replies

7. Shell Programming and Scripting

Compare two files and output in another file

I have two files ' 1st one ALIC-000352-B ALIC-000916-O DDS-STNGD FDH-PPO1-001 PFG-30601-001 2nd one 'ALIC-000352-B' 'ALIC-000916-O' 'DDS-STNGD' 'FDH-PPO1-001' (4 Replies)
Discussion started by: Pratik4891
4 Replies

8. UNIX for Dummies Questions & Answers

Compare 2 files print the lines of file 2 that contain a string from file 1

Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help File 1 123 232 W343... (6 Replies)
Discussion started by: KevinRidley
6 Replies

9. UNIX for Dummies Questions & Answers

Recursive remove files with exception

Hi, I want to remove a directory recursively except the inside directories calles .SYNC (designsync dirs) I am looking for something like: \rm -rf < find . * | grep -v .SYNC The find works ok but I do not know how to redirect it. Please help. Regards, Ziv (1 Reply)
Discussion started by: zivsegal
1 Replies

10. Shell Programming and Scripting

compare two files and make 1st file same as 2nd file

I am trying to compare two file and make changes where ever its different. for example: Contents of file1 IP=192.165.89.11 NM=255.255.0.0 GW=192.165.89.1 Contents of file2 IP=192.165.89.11 NM=255.255.255.255 GW=192.165.89.1 NOTE HERE THAT NM IS DIFFERENT So i want the changes... (6 Replies)
Discussion started by: pradeepreddy
6 Replies
Login or Register to Ask a Question