[Python] Extracting lines from two files where there is a match of value in specific columns

Discussão em 'Python' iniciado por Stack, Junho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I have two tab-separated files (with thousand of lines each):

    File1:

    anno1.g20653.t1 anno1.g20674.t1 eud1g02416 eud1g02458 27 +
    anno2.g3796.t1 anno1.g20698.t1 eud1g02520 eud1g02556 28 +


    File2:

    scaffold_1 transcript 11256 13613 anno1.g20653.t1
    scaffold_1 transcript 25598 47989 anno1.g20066.t2


    I would like to do the following: If element in column 1 of File1 exist in column 5 of File2, print output File3 with the two matching lines combined (tab-separated), ignoring lines with no matches:

    File3:

    anno1.g20653.t1 anno1.g20674.t1 eud1g02416 eud1g02458 27 + scaffold_1 transcript 11256 13613 anno1.g20653.t1


    I have tried the following, but it only extracts the line from File2:

    awk 'FNR==NR {a[$1]; next} FNR> 1 && $5 in a' File1 File2 > File3


    Awk or python code would be useful. Thank you.

    Continue reading...

Compartilhe esta Página