0

I need help to cross reference two files.

file1

Aplastodiscus_albofrenatus_CFBH5051
Aplastodiscus_albosignatus_MZUSPfield1451
Aplastodiscus_arildae_USNM303022
Aplastodiscus_cavicola_MNRJ63689
Aplastodiscus_cochranae_CFBH3001

file2

>12sval16s_Aplastodiscus_albofrenatus_CFBH5051
>12sval16s_Aplastodiscus_albosignatus_MZUSPfield1451
>12sval16s_Aplastodiscus_arildae_USNM303022
>12sval16s_Boana_palaestes_MUBI6795
>12sval16s_Boana_gladiator_MUBI5391

I want to append strings in file2 without ">" only if a match occurs between strings in file1 and strings after the first "_" in file2. The strings from file1 and file2 must be separated using space. The output should be something like this:

Aplastodiscus_albofrenatus_CFBH5051 12sval16s_Aplastodiscus_albofrenatus_CFBH5051
Aplastodiscus_albosignatus_MZUSPfield1451 12sval16s_Aplastodiscus_albosignatus_MZUSPfield1451
Aplastodiscus_arildae_USNM303022 12sval16s_Aplastodiscus_arildae_USNM303022
Aplastodiscus_cavicola_MNRJ63689
Aplastodiscus_cochranae_CFBH3001

I wonder if awk would help. Thank you in advance.

:)

2
  • 1
    Is that really what your file2 looks like? You only have the fasta header and no sequence?
    – terdon
    Commented Sep 20, 2022 at 11:43
  • Yes, awk would help. You can learn about awk in the book Effective AWK Programming, 5th Edition, by Arnold Robbins.
    – Ed Morton
    Commented Sep 20, 2022 at 23:28

1 Answer 1

0

You can use GNU sed for this, but an awk solution would be more readable.

sed -rn '/^>/!{G;s:^([^\n]+)\n.*>([^_]+_)\1\.*:\1 \2\1:;P;d};H' FILE2 FILE1 > OUTPUT

The input order is important: FILE1 needs to be read last, because that's the one it is printed, with or without a match from the previously stored FILE2.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .