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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two files with different number of records and output only the Extra records from file1
# 1  
Old 07-23-2012
Data Compare two files with different number of records and output only the Extra records from file1

Hi Freinds ,

I have 2 files .

File 1

Code:
|nag|HYd|1|Che
|esw|Gun|2|hyd
|pra|bhe|3|hyd
|omu|hei|4|bnsj
|uer|oeri|5|uery

File 2

Code:
|nag|HYd|1|Che
|esw|Gun|2|hyd
|uer|oi|3|uery

output :

Code:
|pra|bhe|3|hyd
|omu|hei|4|bnsj

file2 may contain some difference but the output should be only the lines which are present in file1 and not present in file2

Please help friends. I am very new to Shell scriting

I used
Code:
Comm -23

command but i didnt get the exact output.

Please help SmilieSmilie Smilie

Last edited by Scrutinizer; 07-23-2012 at 05:38 AM.. Reason: code tags
# 2  
Old 07-23-2012
Code:
fgrep -v -f file2 file1

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 07-23-2012
Hi itKamaraj,

Thanks so much for the quick reply. I tried your command and got he results as below.

file1.txt

Code:
|hyd|che|1|2
|jun|out|2|1
|lok|ter|3|6

file2.txt

Code:
|hyd|che|1|2
|jun|out|3|1

Actual Output :

Code:
|jun|out|2|1
|lok|ter|3|6

Expected Output:

Code:
|lok|ter|3|6


Here if i use frep -v -f i am getting the output where i have difference . The expected output should not contain the difference lines it should cntain only the extra lines

Thanks in advance. please help.

Last edited by Scrutinizer; 07-23-2012 at 05:38 AM.. Reason: code tags
# 4  
Old 07-23-2012
Hi

As per your requirement, the expected output should also contain "|uer|oeri|5|uery". comm -23 should get this for you.

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 07-23-2012
@OP: It seems to me that you are looking for a solution where not all columns are significant, but then you would need to tell us which columns determine whether records are "equal" .
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 07-23-2012
@Scrutinizer, sorry i think i have confused you. let me explain in detail .

Expected Output:

I want to compare 2 files let say file1.txt( record count 4) and file 2.txt ( record 3 count) .

file1 :

Code:
|hyd|che|1|2
|jun|out|2|1
|lok|ter|3|6
|ing|tut|4|8

file2.txt

Code:
|hyd|che|1|2
|jun|out|3|1
|ing|tut|4|8


i want to compare 2 files line by line. the Expected output should contain the lines which are present in file1.txt but not in file2.txt. if i use command comm -23

Code:
|jun|out|2|1
|lok|ter|3|6

I got the above output because there is a difference in line 2 and one more line is missing in file1.txt.

But i need to get only the |lok|ter|3|6 , where this line is present in file1.txt but not in file2.txt.

Hope this helps you . please look into it.. I am very new to Unix Smilie

Last edited by Scrutinizer; 07-23-2012 at 06:20 AM.. Reason: code tags
# 7  
Old 07-23-2012
The jun|out records are not the same, so that record gets printed. Yet you do not want it to get printed, so you need to tell us on which basis they should be considered equal.. Is it because of column 1, 2, 3? Or a combination?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script when scheduled in Crontab gives extra records in output

Hi, We have created a script that's checks the latency of IIDR subscription by fetching details from a config file (that contains subscription details) and running the CHCCLP command. The out put is then concatenated in a csv file. Once all subscription details are saved the script send a mail... (7 Replies)
Discussion started by: ab095
7 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

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

4. Shell Programming and Scripting

Compare and find records of file1 not in file2

hi.. i am using solaris system and ksh and using nawk to get records of file1 not in file2(not line by line comparison). code i am using is nawk 'NR==FNR{a++} !a {print"line:" FNR"->" $0} ' file2 file1 same command with awk runs perfectly on darwin kernel(mac) but in solaris it does line by... (2 Replies)
Discussion started by: Abhiraj Singh
2 Replies

5. Shell Programming and Scripting

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 : Aishvarya |1234... (4 Replies)
Discussion started by: aishvarya.singh
4 Replies

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

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

8. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies

9. Shell Programming and Scripting

I want records in file2 those are not exist in file1

I have two files - file1 and file2. Now I want records in file2 those are not exist in file1. How to grep this ? eg: file1 08941 08944 08945 08946 08947 file2 08942 08944 5 08942 08945 5 08942 08946 4 08942 08947 6 08942 08952 4 08942 08963 5 08942 ... (3 Replies)
Discussion started by: suresh3566
3 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