compare two columns of different files and print the matching second file..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare two columns of different files and print the matching second file..
# 1  
Old 10-06-2009
compare two columns of different files and print the matching second file..

Hi,

I have two tab separated files;

file1:

S.No ddi fi cu o/l t+ t-
1 0.5 0.6 o 0.1 0.2
2 0.2 0.3 l 0.3 0.4
3 0.5 0.8 l 0.1 0.6



file2:

S.No ddi fi cu o/l t+ t-
1 0.8 0.9 o 0.5 0.6
2 0.5 0.2 o 0 0
3 0.2 0.3 l 0 0
4 0.5 0.6 l 0 0
5 0.25 0.32 o 0.3 0.6



Desired output:
S.No ddi fi cu o/l t+ t-
1 0.8 0.9 o 0.5 0.6
2 0.5 0.6 o 0.1 0.2
3 0.2 0.3 l 0.3 0.4
4 0.5 0.8 l 0.1 0.6
5 0.25 0.32 o 0.3 0.6


That is the first file's second column(Tab as feild separator) needs to be compared with all second file's second column, if match found print the first file's line, if match not found print second file's line.

Thanks in Advance,
Vasanth

---------- Post updated at 08:49 AM ---------- Previous update was at 08:35 AM ----------

anybody there to help me..

thanks in advance
vasanth
# 2  
Old 10-06-2009
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.
# 3  
Old 10-06-2009
Ok noticed..

i will wait till anybody helps me..

Thanks,
Vasanth
# 4  
Old 10-06-2009
Code:
awk 'NR == FNR { A[$2]=$0; next } { if (A[$2]) print A[$2]; else print }' file1 file2
S.No ddi fi cu o/l t+ t-
1 0.8 0.9 o 0.5 0.6
3 0.5 0.8 l 0.1 0.6
2 0.2 0.3 l 0.3 0.4
3 0.5 0.8 l 0.1 0.6
5 0.25 0.32 o 0.3 0.6

# 5  
Old 10-06-2009
Shorter Smilie
Code:
awk 'NR==FNR{a[$2]=$0;next}{$0=a[$2]?a[$2]:$0}1' file1 file2

# 6  
Old 10-06-2009
Even shorter Smilie
Code:
awk 'NR==FNR{a[$2]=$0;next}a[$2]{$0=a[$2]}1' file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare & print content of file which is not matching

Hi All I want to compare 2 files using awk and get output of content which is not matching I have 2 files a.txt 123 456 780 143 b.txt A|B|C|167|D|E C|K|D|123|D|E A|B|D|789|G|F C|D|G|143|A|B Not matching line from b.txt O/P A|B|C|167|D|E A|B|D|789|G|F (3 Replies)
Discussion started by: aaysa123
3 Replies

2. Shell Programming and Scripting

[Solved] awk compare two different columns of two files and print all from both file

Hi, I want to compare two columns from file1 with another two column of file2 and print matched and unmatched column like this File1 1 rs1 abc 3 rs4 xyz 1 rs3 stu File2 1 kkk rs1 AA 10 1 aaa rs2 DD 20 1 ccc ... (2 Replies)
Discussion started by: justinjj
2 Replies

3. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

4. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

awk compare specific columns from 2 files, print new file

Hello. I have two files. FILE1 was extracted from FILE2 and modified thanks to help from this post. Now I need to replace the extracted, modified lines into the original file (FILE2) to produce the FILE3. FILE1 1466 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04 1467 55.27433... (1 Reply)
Discussion started by: jm4smtddd
1 Replies

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

8. Shell Programming and Scripting

Compare selected columns of two files and print whole line with mismatch

hi! i researched about comparing two columns here and got an answer. but after examining my two files, i found out that the first columns of the two files are not unique with each other. all i want to compare is the 2nd and 3rd column. FILE 1: ABS 456 315 EBS 923 163 JYQ3 654 237 FILE 2:... (1 Reply)
Discussion started by: engr.jay
1 Replies

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

10. 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
Login or Register to Ask a Question