Compare two files and print list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two files and print list
# 1  
Old 10-18-2012
Compare two files and print list

Hi Gents,

I have 2 files as seen below.

File 1:
Code:
 9   1020
10   1001
11   1001
12   1002
13   1003
14   1004
15   1004

File 2:
Code:
 9  1000
11  1001
12  1002
13  1003
15  1004

I would like to the following

1. Filter the data taking in consideration the values in File2 - Colum1 as reference, and make two list:

- One with the values not present in File 2
Example
Code:
10   1001
14   1004

- Other list of the values which dont match in , in the list should be:
Expample
Code:
9 1020
9 1000

Thanks in advance
To anyone who can help me with this Smilie

Last edited by Franklin52; 10-18-2012 at 08:48 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 10-18-2012
Check your man page of grep. Does your version supports the -f and -v options?
# 3  
Old 10-18-2012
Hi Franklin
Yes grep have options -f & -v

---------- Post updated at 07:20 AM ---------- Previous update was at 07:20 AM ----------

Hi Franklin
Yes grep have options -f & -v
# 4  
Old 10-18-2012
Another way with sorted files:
Code:
join file1 file2 -a 1 |awk 'NF==2{print > "notpresent";next;}$2!=$3{print $1FS$2RS$1FS$3>"notmatch"}'

This User Gave Thanks to Klashxx For This Post:
# 5  
Old 10-18-2012
Quote:
Originally Posted by jiam912
Hi Franklin
Yes grep have options -f & -v
Have you give it a try?
# 6  
Old 10-18-2012
Another way with sorted files:
Code:
join file1 file2 -a 1 |awk 'NF==2{print > "notpresent";next;}$2!=$3{print $1FS$2RS$1FS$3>"notmatch"}'

Thanks a lot the code works perfect

Last edited by Franklin52; 10-19-2012 at 03:21 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two files and print output

Hi All, i am trying to compare two files in Centos 6. F1: /tmp/d21 NAME="xvda" TYPE="disk" SIZE="40G" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="" NAME="xvda1" TYPE="part" SIZE="500M" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="/boot" NAME="xvda2" TYPE="part"... (2 Replies)
Discussion started by: balu1234
2 Replies

2. Shell Programming and Scripting

Compare two files and print using awk

I have 2 files: email_1.out 1 abc@yahoo.com 2 abc_1@yahoo.com 3 abc_2@yahoo.com data_1.out <tr> 1 MAIL # 1 TO src_1 </tr> <tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> <tr><td... (1 Reply)
Discussion started by: sol_nov
1 Replies

3. Shell Programming and Scripting

Compare two text files and print matches

Hi, I am looking for a way to compare two text files and print the matches. For example; File1.txt 89473036 78474384 48948408 95754748 47849030 File2.txt 47849030 46730356 16734947 78474384 36340047 Output: (11 Replies)
Discussion started by: lewk
11 Replies

4. Shell Programming and Scripting

Compare columns 2 files and print

File 1 has 16 columns so does File 2 I want to remove all records from File 2 that column 1 and column 16 match between file 1 and file 2 delimter of files is ~ (10 Replies)
Discussion started by: sigh2010
10 Replies

5. Shell Programming and Scripting

Compare two columns in two files and print the difference

one file . . importing table employee 119 . . importing table jobs 1 2nd file . . importing table employee 120 . . importing table jobs 1 and would like... (2 Replies)
Discussion started by: jhonnyrip
2 Replies

6. Shell Programming and Scripting

Edited: compare two files and print mismatch

Using unix shell script, how to compare two files and print lines with mismatch? Below are the requirements: 1. The number of lines on the two files is not the same. 2. The difference/mismatch can be found on the second or third column. 3. The comparison is not between line 1 of file 1 and line... (16 Replies)
Discussion started by: kingpeejay
16 Replies

7. Shell Programming and Scripting

Compare two files and print the two lines with difference

I have two files like this: #FILE 1 ABCD 4322 26485 JMTJ 5311 97248 XMPJ 4321 58978 #FILE 2 ABCD 4321 26485 JMTJ 5311 97248 XMPJ 4321 68978 What to do: Compare the two files and find those lines that doesn't match. And have a new file like this: #FILE 3 "from file 1" ABCD 4322 26485... (11 Replies)
Discussion started by: kingpeejay
11 Replies

8. Shell Programming and Scripting

compare columns from seven files and print the output

Hi guys, I need some help to come out with a solution . I have seven such files but I am showing only three for convenience. filea a5 20 a8 16 fileb a3 42 a7 14 filec a5 23 a3 07 The output file shoud contain the data in table form showing first field of... (7 Replies)
Discussion started by: smriti_shridhar
7 Replies

9. Shell Programming and Scripting

compare two files and print the last row into first

suppose fileA vis vis gyh gye gyh fileB vis 23 gyh 21 gye 32 output shud be like in fileA ... vis 23 vis 23 gyh 21 gye 32 gyh 21 (1 Reply)
Discussion started by: cdfd123
1 Replies

10. Shell Programming and Scripting

to compare two files and to print the difference

suppose one file P1168S P2150L P85L Q597R R1097C Another file P2150L P85L Q597R R1097C R1379C R1587K Then output shud be R1379C R1587K thanks (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question