Compare 2 files having different number of columns and records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 2 files having different number of columns and records
# 1  
Old 05-22-2012
Compare 2 files having different number of columns and records

Hi ,

My requirement is to Compare 2 files having different number of columns and records and get the ouptut containing all the non-matching records from File A(with all column values ) .Example data below :

File A contains following :
Code:
Aishvarya |1234 |Bangalore|acct123456|2001-03-05-00.00.00.000000
Anil         |5678 |Meerut    |acct165432|2001-05-05-00.00.00.000000
BRAD       |3451 |Delhi       |acct154321|2001-06-05-00.00.00.000000
Chad       |9111 |Chennai   |acct179875|2001-07-05-00.00.00.000000

File B contains following :
Code:
acct111111
acct165432
acct234567
acct123456

File C(output) should contain following :
Code:
BRAD       |3451 |Delhi       |acct154321|2001-06-05-00.00.00.000000
Chad       |9111 |Chennai   |acct179875|2001-07-05-00.00.00.000000

This is my first post as I have recently joined ,forgive me if i am not clear in explaining my problem or if you guys have any concerns .


Thanks .

---------- Post updated at 04:36 AM ---------- Previous update was at 04:34 AM ----------

Forgot to mention that I want this on UNIX AIX server..solution can be in perl scripting or something like that .

Last edited by Franklin52; 05-22-2012 at 05:35 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 05-22-2012
Code:
grep -vf <file_b> <file_a>

# 3  
Old 05-22-2012
Code:
awk 'NR==FNR{a[$1];next}!($4 in a)' fileB FS="|" fileA

# 4  
Old 05-22-2012
Code:
grep -vf fileB fileA

# 5  
Old 05-22-2012
Thank You all for the solutions .All are working fine .
I have another doubt -- If my FILE A contains data whose single record is spread across several lines , then following is not working ,what are the modifications that we should make to this :

Code:
 
awk 'NR==FNR{a[$1];next}!($4 in a)' fileB FS="|" fileA


example data for single record from file A that i am talking about is (FILE B data remains same as posted earlier) :

Code:
 
Aishvarya |1234 |Bangalore|acct123456|2001-03-05-00.00.00.000000|MEDBUS|UNITED SS|0000000|0000000000|
000000000|AHM|0175

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding columns from 2 files with variable number of columns

I have two files, file1 and file2 who have identical number of rows and columns. However, the script is supposed to be used for for different files and I cannot know the format in advance. Also, the number of columns changes within the file, some rows have more and some less columns (they are... (13 Replies)
Discussion started by: maya3
13 Replies

2. Shell Programming and Scripting

Compare files to pull changed records only

Hi, I am using Sun Solaris - SunOS. I have two fixed width files shown below. I am trying to find the changes in the records in the Newfile.txt for the records where the key column matches. The first column is a key column (example: A123). If there are any new or deletion of records in the... (4 Replies)
Discussion started by: Saanvi1
4 Replies

3. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

4. Shell Programming and Scripting

awk - compare records of 1 file with 3 files

hi.. I want to compare records present in 1 file with those in 3 other files and print those records of file 1 which are not present in any of the files. for eg - file1 file2 file3 file4 1 1 5 7 2 2 6 9 3 4 5 6 7 8 9 ... (3 Replies)
Discussion started by: Abhiraj Singh
3 Replies

5. Shell Programming and Scripting

Two columns-Common records - 20 files

Hi Friends, I have an input file like this cat input1 x 1 y 2 z 3 a 2 b 4 c 6 d 9 cat input2 x 7 h 8 k 9 l 5 m 9 d 12 (5 Replies)
Discussion started by: jacobs.smith
5 Replies

6. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

7. Shell Programming and Scripting

Compare multiple files with multiple number of columns

Hi, input file1 abcd 123 198 xyz1:0909090-0909091 ghij 234 999 xyz2:987654:987655 kilo 7890 7990 xyz3:12345-12357 prem 9 112 xyz5:97-1134 input file2 abcd 123 198 xyz1:0909090-0909091 -9.122 0 abed 88 98 xyz1:98989-090808 -1.234 1.345 ghij 234 999 xyz2:987654:987655 -10.87090909 5... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

8. Shell Programming and Scripting

How to compare data from 2 zip files and capture the new records from file2 to a new file

I have 2 zip files which have about 20 million records in each file. file 2 will have additional records than file 1. I want to compare the records in both the files and capture the new records from file 2 into another file file3. Please help me with a command/script which provides me the desired... (8 Replies)
Discussion started by: koneru
8 Replies

9. Shell Programming and Scripting

Compare Records between to files and extract it

I am not an expert in awk, SED, etc... but I really hope there is a way to do this, because I don't want to have to right a program. I am using C shell. FILE 1 FILE 2 H0000000 H0000000 MA1 MA1 CA1DDDDDD CA1AAAAAA MA2 ... (2 Replies)
Discussion started by: jclanc8
2 Replies

10. Shell Programming and Scripting

Awk Compare Files w/Multiline Records

I'm trying to compare the first column values in two different files that use a numerical value as the key and output the more meaningful value found in the second column of file1 in front of the matching line(s) in file2. My problem is that file2 has multiple records. For example given: FILE1... (4 Replies)
Discussion started by: RacerX
4 Replies
Login or Register to Ask a Question