rororo
rororo

Reputation: 845

awk: two files are queried

I have two files

file1:

>string1<TAB>Name1
>string2<TAB>Name2
>string3<TAB>Name3

file2:

>string1<TAB>sequence1
>string2<TAB>sequence2

I want to use awk to compare column 1 of respective files. If both files share a column 1 value I want to print column 2 of file1 followed by column 2 of file2. For example, for the above files my expected output is:

Name1<TAB>sequence1
Name2<TAB>sequence2

this is my code:

awk 'BEGIN{FS=OFS="\t"} FNR == NR { a[$1] = $1; next } $1 in a { print a[$2], $2 }' file1 file2 >out

But the only thing I get is an empty first columnsequence

where is the error here?

Upvotes: 0

Views: 50

Answers (1)

karakfa
karakfa

Reputation: 67467

your assignment is not right.

$ awk 'BEGIN   {FS=OFS="\t"}
       NR==FNR {a[$1]=$2; next} 
       $1 in a {print a[$1],$2}' file1 file2

Name1 sequence1
Name2 sequence2

Upvotes: 2

Related Questions