Compare contents of two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare contents of two files
# 1  
Old 06-06-2011
Compare contents of two files

Hello,

I have two files containing both two columns:

Code:
$ cat file1
col1 nbr1
col2 nbr2
col3 nbr3
...

Code:
$ cat file2
val1 ri1
val2 ri2
val3 ri3

...

I need to compare every nbr (nbr1, nbr2,...) in the second column of file1 with every ri (ri1, ri2,...) in the second column of file2. And when nbr=ri ===> write in the output file : [(val - col) nbr]

The output file shoud look like :

Code:
$ cat file3
(val1 - col1) nbr1    (if nbr1=ri1)
(val3 - col3) nbr3    (if nbr3=ri3)

Thank you,
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 06-06-2011 at 06:45 PM.. Reason: code tags, please!
# 2  
Old 06-06-2011
Does this work for you?
Code:
paste file1 file2 | awk '$2==$4{print "("$3" - "$1") "$2}' > file3

# 3  
Old 06-06-2011
Thank you for your reply, but it does not fullfil the need.

Here is my exp :

Code:
 
$ cat file1
3 22
4 33
6 77
$

Code:
$ cat file2
1 22
5 77
$

Code:
$ cat file3
(3 - 1) 22
$

But i need the the output file to be like :
$ cat file3
2 22 =========> (3 - 1) = 2
1 77 =========> (6 - 5) = 1

Can we do it with loop :
for i in file1
for j in file2
if (i==j)
echo ...

?

Thank you

---------- Post updated at 01:19 AM ---------- Previous update was at 12:13 AM ----------

Can you please help me with my question as it's quit urgent ?

Thank you
# 4  
Old 06-06-2011
Code:
$ awk 'NR==FNR{a[$2]=$1;next} {print a[$2]-$1,$2}' file1 file2

2 22
1 77

# 5  
Old 06-06-2011
the command did 90% of the job, but it displays some lines that are in file2 but not present in file1.

Code:
 
$ cat file1
10 1111
20 1112
30 1113
40 1114
60 1116

Code:
 
$ cat file2
2 1111
4 1112
7 1114
5 1115

Code:
 
$ awk 'NR==FNR{a[$2]=$1;next} {print a[$2]-$1,$2}' file1 file2
8 1111
16 1112
33 1114
-5 1115

The last line of the result exists in file2 but not does not exist in file1.

The result i suspect is :

Code:
 
$ awk 'NR==FNR{a[$2]=$1;next} {print a[$2]-$1,$2}' file1 file2
8 1111
16 1112
33 1114

Can you please correct it ?
# 6  
Old 06-07-2011
Code:
awk 'NR==FNR{a[$2]=$1;next} 
    a[$2]!="" {print a[$2]-$1,$2}' file1 file2

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 two file contents which created within one hour?

Hi Gurus, I need to compare two file contents and log the difference. the detail as below: in current directory there is file abc20140728_1020.txt abc20140728_1110.txt I need to find the files which created within 60 minutes first then compare the contents. I am able to use find... (12 Replies)
Discussion started by: ken6503
12 Replies

2. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

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

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

5. Shell Programming and Scripting

Compare folder contents over network

I use diff -r dir1 dir2 to get comparison of two folders that are on same machine. Now I need the same thing but one of the folders is on a different machine. Currently I ftp the folder to a temp folder compare using above command and delete the temp folder. Is there any other better options?... (5 Replies)
Discussion started by: ke3kelly
5 Replies

6. Shell Programming and Scripting

compare the contents of two directories

i have been asked to write a bash shell script comparing two directories and sed or awk should not be used in this assignment. compdir will compare filenames in two directories, and list information about filenames that are in one directory but not the other. The information listed will be a long... (1 Reply)
Discussion started by: soccerball
1 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. Shell Programming and Scripting

How to compare contents of two CSV rows

I have been trying to find the answer to this but can't seem to get it. I have two rows, both are CSV files. I want to iteratively compare each field value to the corresponding value in the second file. Example: File 1.txt 1|hello|3.2|AMB||||B File 2.txt 1|hi|3,2|ABC||||C The... (1 Reply)
Discussion started by: axwack
1 Replies

9. Shell Programming and Scripting

Compare & replace contents within a file

I have 2 files file1 1 TMQUEUE QUE1 STMW633A 100 DMADM DOMGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633AA STMW633A 100 GWADM GWTGRPSTMW638A STMW638A 100 TMSYSEVT EVTGRPSTMW633A STMW633A 100 TMSYSEVT ... (2 Replies)
Discussion started by: kaustubh137
2 Replies

10. 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
Login or Register to Ask a Question