Merging and differentiating 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging and differentiating 2 files
# 1  
Old 06-10-2013
Merging and differentiating 2 files

I have 2 csv files say file1 and file2. Based on 2 columns, I want to check if the values of file1 is present in file 2 or not. If it's not present then it should create a file with the values which are not present. Basically I want the minus between 2 files but based on 2 columns but the output should have all columns.

Sample Files:
File1.csv
Code:
000003887345CNY0 |$1|$CNY|$IB        |$IP        |$          |$Y|$049681709830CNY0|$ILC       |$Oct 10 2008  4:29:31:213PM
000009446109CNY0 |$1|$CNY|$EB        |$EP        |$          |$Y|$894672402839CNY0|$ELC       |$Nov 16 2010  6:58:20:653PM
000009446109CNY0 |$2|$CNY|$EB        |$EP        |$          |$Y|$894672402839CNY0|$ELC       |$Oct 12 2010  4:03:43:513PM

File2.csv
Code:
000013948767CNY0 |$2|$CNY|$NLC       |$NLC       |$21        |$N|$000013948767CNY0|$NLC       |$Jan 21 2009  1:14:21:560PM
000009446109CNY0 |$1|$CNY|$AB        |$AP        |$          |$N|$748474602839CNY0|$APC       |$Dec 16 2010  6:58:20:653PM

Now since File1 already has record 000009446109CNY0 |$1| from File2 so the output file should have just one record from File2 which is not present in File1. Note the other fields may not be similar. I just need to compare using first 2 fields
Code:
000013948767CNY0 |$2|$CNY|$NLC       |$NLC       |$21        |$N|$000013948767CNY0|$NLC       |$Jan 21 2009  1:14:21:560PM

Hope the question is clear. Thanks.
# 2  
Old 06-10-2013
Code:
awk -F\| 'NR==FNR{A[$1,$2];next}!(($1,$2) in A)' File1.csv File2.csv

This User Gave Thanks to Yoda For This Post:
# 3  
Old 06-10-2013
Quote:
Originally Posted by Yoda
Code:
awk -F\| 'NR==FNR{A[$1,$2];next}!(($1,$2) in A)' File1.csv File2.csv


That's simply awesome. It worked as needed. Thanks much for the help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with merging 2 files into 1

::::::::: ::FileA:: ::::::::: A1-------A2--------A3---A4---A5-- ================================= AC5VXVLT-XX---------------------- B57E434--XXXX1-----MMMM-ZZZ--111- C325G20--XXXXX3----CCCC------3332 DC35S51--XXXXY1----DDDD------44X- DC35S52--XXXXY2----DDDD------44Y-... (5 Replies)
Discussion started by: lordsmiter
5 Replies

2. Shell Programming and Scripting

Merging files

I have two files file 1 containing x rows and 1 column file 2 containing x rows and 1 column I want to merge both the files and add a comma between the two eg plz guide (1 Reply)
Discussion started by: test_user
1 Replies

3. Shell Programming and Scripting

differentiating PIDs under 200

Hey, So I'm new to shell scripting, and I'm trying to write one for my lab that will keep down the work load by deleting processes that are left over from previous sessions. Basically I want it to do three things. 1) Check the processes running 2) See if that person is logged on. 3) if... (2 Replies)
Discussion started by: prgoodwin
2 Replies

4. UNIX for Dummies Questions & Answers

Merging two files

Hi, I have two files a.txt and b.txt. a.txt 1 2 3 4 b.txt a b c d e I want to generate a file c.txt by merging these two file and the resultant file would contain c.txt 1 (4 Replies)
Discussion started by: siba.s.nayak
4 Replies

5. UNIX for Dummies Questions & Answers

Parameters to check while differentiating two servers

Hi All, I have two solaris servers. Please tell me what all parameters i can check to find out the difference between two servers. how to differentiate based on H/W,S/W etc like i have two servers spdwa013 $ uname -an SunOS spdwa013 5.8 Generic_117350-61 sun4u sparc SUNW,Sun-Fire-480R ... (1 Reply)
Discussion started by: usha rao
1 Replies

6. Shell Programming and Scripting

differentiating two sets for filenames????

set 1 ./abc@@/main/61 ./def.cpp@@/main/13 ./fgh.cpp@@/main/16 ./ijk.cpp@@/main/12 ./mln.cpp@@/main/9 ./uvw.cpp@@/main/30 set2 ./eww@@/main/61 ./def.cpp@@/main/13 ./xxx.cpp@@/main/26 ./kkk.cpp@@/main/72 ./qqq.cpp@@/main/19 ./fgh.cpp@@/main/16 I have two sets with filenames in... (13 Replies)
Discussion started by: skyineyes
13 Replies

7. Shell Programming and Scripting

differentiating two sets

Hi Suppose i have a set of files like this set1 a.cpp@@main/5 b.cpp@@main/6 set 2 m.cpp@@main/51 n.hpp@@main/51 a.cpp@@main/15 b.cpp@@main/2 there may be files with same name in 2 sets. i need to list the files in set1 which have last numeric field less than the same file in... (15 Replies)
Discussion started by: skyineyes
15 Replies

8. Shell Programming and Scripting

Merging 2 files

Hi, I have got two files 1.txt 1111|apple| 2222|orange| 2.txt 1111|1234|000000000004356| 1111|1234|000000001111| 1111|1234|002000011112| 2222|5678|000000002222| 2222|9102|000000002222| I need to merge these two so that my out put looks like below: Search code being used should be... (4 Replies)
Discussion started by: jisha
4 Replies

9. Shell Programming and Scripting

merging two files

Friends, os: redhat enterprise linux/SCO UNIX5.0 I have two files and I would like to merge on given key value. Now I have tried with join commd but it does not supporte multiple delimiters. and if records length is not fixed. join -a1 5 -a2 1 -t -o file1 file2 > outname Can any... (7 Replies)
Discussion started by: vakharia Mahesh
7 Replies

10. Shell Programming and Scripting

merging files

Thanks in advance I have 2 files having key field in each.I would like to join both on common key.I have used join but not sucessful. The files are attached here . what i Want in the output is on the key field SLS OFFR . I have used join commd but not successful. File one ======= SNO ... (6 Replies)
Discussion started by: vakharia Mahesh
6 Replies
Login or Register to Ask a Question