Compare 1 column in 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 1 column in 2 files
# 1  
Old 11-06-2013
Compare 1 column in 2 files

Hi all,
I have two two-column tab-separated files with the following input:

inputA
Code:
dog A
dog B
cat A

....

inputB
Code:
dog C
mouse A

output
Code:
dog


I need to compare the 1st column of each file and output those shared items.

What is the best unix solution for that?

Last edited by owwow14; 11-06-2013 at 04:18 PM.. Reason: Please use CodeTags for commands and data
# 2  
Old 11-06-2013
Hello,

Kindly use the code tags for any Input data or output data. Also please give us the Input data and expected data fro same.

Thanks,
R. Singh
# 3  
Old 11-06-2013
Thanks for the reply. Please see the updated question
# 4  
Old 11-06-2013
try:
Code:
awk 'NR==FNR {a[$1]=$1; next} a[$1] && !a[$1]++ {print $1}' inputB inputA

This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 11-06-2013
Code:
awk 'NR==FNR {a[$1]; next} ($1 in a) {print $1; delete a[$1]}' inputB inputA


Last edited by MadeInGermany; 11-06-2013 at 04:58 PM.. Reason: had misunderstood the question
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 11-07-2013
for given input this also will work

Code:
$ cat file1
dog A
dog B
cat A

Code:
$ cat file2
dog C
mouse A

Code:
$ awk 'FNR==NR{A[$1];next}{$0=$1}($1 in A)' file1 file2
dog

OR

Code:
$ comm -12 <(cut -d ' ' -f1 file1 | sort ) <(cut -d ' ' -f1 file2 | sort) 
dog


Last edited by Akshay Hegde; 11-07-2013 at 07:22 AM.. Reason: to add some more to answer
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

Compare 3rd column in 2 files

I have the following 2 files. File 1 08FB,000192602673,10000000c9a6b240 0121,000192602673,20000025b550101f 0121,000192602673,20000025b550100f 08FA,000192602673,10000000c9a6b240 File 2 18F2,000195702363,10000000c9a6b240 18F3,000195702363,10000000c9a6b240... (2 Replies)
Discussion started by: kieranfoley
2 Replies

3. Shell Programming and Scripting

Compare two files based on column

Hi, I have two files roughly 1200 fields in length for each row, sorted on the 2nd field. I need to compare based on that 2nd column between file1 and file2 and print lines that exist in both files into separate files (I can't guarantee that every line in file1 is in file2). Example: File1: ... (1 Reply)
Discussion started by: origon
1 Replies

4. Shell Programming and Scripting

Compare first column of 2 files and replace

Hi All, I have 2 files in the following format : File 1 S00999999|BHANU|TEST|007 JOHN DOE APT 999||VENGA HIGHWAY|MA|09566|SCO DUAL|20140201|20140331|20140401|20140630|20140327| S00888888|BU|TES|009 JOHN DOE APT 909||SENGA HIGHWAY|MA|08566|SCO... (1 Reply)
Discussion started by: nua7
1 Replies

5. Shell Programming and Scripting

Compare two files with different column entries..:-(

Dear All, I would appreciate any help..At the moment, my work is stuck cos of my inability to resolve this issue. Which is following: I have two files with the arrngment like this file-1 190645 12 3596022 190645 12 3764915 190645 16 3803981 190645 12 3854102 190645 12 4324593 190645... (12 Replies)
Discussion started by: emily
12 Replies

6. Shell Programming and Scripting

How to compare 2 files column's more than 5?

Hi All I am just trying to compare 2 file using column information using following code awk ' NR==FNR {A=$9; next} {B=A; print $0,B""?B:" Not -In file" } ' OFS="\t" file1 file2if file1 matches with file2 then print $9 content in file 1 along with file2 $0 suppose if I keyed on only $1 in... (17 Replies)
Discussion started by: Akshay Hegde
17 Replies

7. Shell Programming and Scripting

Compare Two Files(Column By Column) In Perl or shell

Hi, I am writing a comparator script, which comapre two txt files(column by column) below are the precondition of this comparator 1)columns of file are not seperated Ex. file1.txt 8888812341181892 1243548895685687 8945896789897789 1111111111111111 file2.txt 9578956789567897... (2 Replies)
Discussion started by: kumar96877
2 Replies

8. Shell Programming and Scripting

Compare files column to column based on keys

Here is my situation. I need to compare two tab separated files (diff is not useful since there could be known difference between files). I have found similar posts , but not fully matching.I was thinking of writing a shell script using cut and grep and while loop but after going thru posts it... (2 Replies)
Discussion started by: blackjack101
2 Replies

9. Shell Programming and Scripting

column compare of files

Hi i want to compare files a.txt 12345,23 34567,76 65456,10 13467,01 b.txt 12346,23 34567,76 23333,90 65456,10 13467,03 i want o/p in 3 files common.txt both have (2 Replies)
Discussion started by: aaysa123
2 Replies

10. Shell Programming and Scripting

Compare Column value from Two Different Files

Hi, I need help to write a korn shell script to 1. Check and compare the first file contains single record from the /scp/inbox directory against the badpnt.dat file from the pnt/badfiles directory contains multiple records based on the fam_id column value start at position 38 to 47 from the... (7 Replies)
Discussion started by: hanie123
7 Replies
Login or Register to Ask a Question