Comparing two test files and printing out the values that do not match


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Comparing two test files and printing out the values that do not match
# 1  
Old 10-18-2012
Comparing two test files and printing out the values that do not match

Hi,

I have two text files with matching first columns. Some of the values in the second column do not match. I want to write a script to print out the rows (only the first column) where the values in the second column do not match.
Example:

Input 1
Code:
A 1 
B 2
C 3 
D 4

Input 2
Code:
A 2
B 2
C 3
D 1

Output
Code:
A
D

Thanks!
# 2  
Old 10-18-2012
Code:
paste file1 file2 | awk '$2 != $4{print $1}'

A
D

This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 10-18-2012
Or

Code:
diff file1 file2 | awk '$1=="<" {print $2}'

# 4  
Old 11-04-2012
How can I modify the code if the files have 20 columns instead of 2 columns? The first column of the files still match perfectly. I want to print out the rows in which there is a perfect match between every single element between the two files?
# 5  
Old 11-04-2012
Hi

Code:
$ cat file1
A 1 a
B 2 b
C 3 c
D 4 d


Code:
$ cat file2
A 2 a
B 2 b
C 3 c
D 1 d

To find the lines with all matches:

Code:
$ comm -12 file1 file2
B 2 b
C 3 c

This User Gave Thanks to guruprasadpr For This Post:
# 6  
Old 11-04-2012
Thanks! What if I want to modify the script so that it prints out lines with at most 1 difference between file 1 and file 2 and so on?
# 7  
Old 11-04-2012
Hi
Please show us the script with what you have tried till now to get this done?

Guru.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing two columns in two files and printing a third based on a match

Hello all, First post here. I did not notice a previous post to help me down the right path. I am looking to compare a column in a CSV file against another file (which is not a column match one for one) but more or less when a match is made, I would like to append a third column that contains a... (17 Replies)
Discussion started by: dis0wned
17 Replies

2. Shell Programming and Scripting

Comparing two one-line files and selecting what does not match

I have two files. One is consisting of one line, with data separated by spaces and each number appearing only once. The other is consisting of one column and multiple lines which can have some numbers appearing more than once. It looks something like this: file 1: 20 700 15 30 file2: 10... (10 Replies)
Discussion started by: maya3
10 Replies

3. Shell Programming and Scripting

Comparing same column from two files, printing whole row with matching values

First I'd like to apologize if I opened a thread which is already open somewhere. I did a bit of searching but could quite find what I was looking for, so I will try to explaing what I need. I'm writing a script on our server, got to a point where I have two files with results. Example: File1... (6 Replies)
Discussion started by: mitabrev83
6 Replies

4. Shell Programming and Scripting

Awk: Comparing arguments with in line values of file and printing the result

I need to develop a script where I will take two date arguments as parameter date1 and date2 which will in format YYYYMM. Below is the input file say sample.txt. sample.txt will have certain blocks starting with P1. Each block will have a value 118,1:TIMESTAMP. I need to compare the... (7 Replies)
Discussion started by: garvit184
7 Replies

5. Shell Programming and Scripting

Comparing the values of two files

Hi Am trying to compare the values of two files.. One is a big file that has many values and the other is a small file.. The big file has all values present in small file.. # cat SmallFile 4456602 22347881 7471282 15859891 8257690 21954701 7078068 18219229 2883826 6094959 100000 ... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

6. UNIX for Dummies Questions & Answers

Comparing two text files by a column and printing values that do not match

I have two text files where the first three columns are exactly the same. I want to compare the fourth column of the text files and if the values are different, print that row into a new output file. How do I go about doing that? File 1: 100 rs3794811 0.01 0.3434 100 rs8066551 0.01... (8 Replies)
Discussion started by: evelibertine
8 Replies

7. Shell Programming and Scripting

Comparing two files and printing 2nd column if match found

Hi guys, I'm rather new at using UNIX based systems, and when it comes to scripting etc I'm even newer. I have two files which i need to compare. file1: (some random ID's) 451245 451288 136588 784522 file2: (random ID's + e-mail assigned to ID) 123888 xc@xc.com 451245 ... (21 Replies)
Discussion started by: spirm8
21 Replies

8. UNIX for Dummies Questions & Answers

Comparing a batch of files to a test file

Hi I am writing a script to run a loop through a directory and run a diff on each file against my test file. I then want to time how long each file to process (not sure how time works), as well as how long all the files took as a whole to process. Here is my code #!/bin/bash #Old... (1 Reply)
Discussion started by: ladyAnne
1 Replies

9. UNIX for Dummies Questions & Answers

Comparing two files and count number of lines that match

Hello all, I always found help for my problems using the search option, but this time my request is too specific. I have two files that I want to compare. File1 is the index and File2 contains the data: File1: chr1 protein_coding exon 500 600 . + . gene_id "20532";... (0 Replies)
Discussion started by: DerSeb
0 Replies

10. Shell Programming and Scripting

reading two files, comparing, printing when unmatched value is seen

Hello, I have two files: file1 1 2 3 4 5 file2 "a","b",,,,,"c","1",..... "s","d",,,,,"s","1",..... "a","c",,,,,"d","1",.... "f","v",,,,,,"f","2",..... etc I have to read "1" from file1 and grab all records in file2 (say column 6 in file2 is "1") until the column changes its value (... (6 Replies)
Discussion started by: i.scientist
6 Replies
Login or Register to Ask a Question