Compare one of the columns of two seperate files and write the raw of the second file next to first file if match.
3
0
Entering edit mode
9.0 years ago
emre • 0

I have two files

The first file should remain as how it is and

If the 4th column of the second file is occurs in the 7th column of the first file -> write that row of the second file to the end of corresponding row of the first file

Could you please help on that.

Thanks

next-gen code • 2.5k views
ADD COMMENT
1
Entering edit mode

I suggest you http://stackoverflow.com/ for this kind of technical questions.
For other hand, when you ask this type of questions, it is highly recommended to add an example of input(s) and desired output file(s).

ADD REPLY
0
Entering edit mode

What have you tried?

ADD REPLY
0
Entering edit mode

can you provide a few line of your 2 files

ADD REPLY
0
Entering edit mode
9.0 years ago
venu 7.1k
​​awk 'FNR==NR{a[$7]=$0;next}{if(b=a[$4]){print a}}' file1 file2 > result

This should work if I understood correctly

ADD COMMENT
0
Entering edit mode
9.0 years ago
emre • 0

Thank you very much for your help, but it didn't work. What I want to do is that I have two files File1.txt and File2.txt. I want to compare third column in File1.txt and and second column in File2.txt. If found a same value write all raw in the first file next to corresponding raw.

Thanks again for your answers

File1.txt

1    2    A   3
4    5    A   6
7    8    B   9
10   11   C   12

File2.txt

13   A   16
14   B   17
15   D   18

Output.txt

1    2    A   3    13   A   16
4    5    A   6    13   A   16
7    8    B   9    14   B   17
10   11   C   12   .    .   .
ADD COMMENT
0
Entering edit mode

Please re-post this as an edit to your original answer. You can easily achieve wanted result using join (GNU) or merge (R), there are many examples of using those commands here on biostars.

ADD REPLY
0
Entering edit mode

Your original question - 'If the 4th column of the second file is occurs in the 7th column of the first file -> write that row of the second file to the end of corresponding row of the first file'.

If you have any edits please do them in original question.

ADD REPLY
0
Entering edit mode

Ok. I understand that. Thanks

ADD REPLY
0
Entering edit mode
9.0 years ago
iraun 6.2k

Try this:

awk 'FNR==NR{a[$2]=$0;next}{if($3 in a){print $0"\t"a[$3]}else{print $0}}' file2 file1
ADD COMMENT
0
Entering edit mode

Thank you so much. It worked.

ADD REPLY

Login before adding your answer.

Traffic: 2196 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6