2017-04-24 40 views
0

我有兩個文件AWK:兩個文件查詢

file1

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

file2

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

我想用awk來比較各個文件的第1列。如果兩個文件共享一個列1值,我想打印file1的第2列,然後是file2的第2列。例如,對於上面的文件我的預期成果是:

Name1<TAB>sequence1 
Name2<TAB>sequence2 

這是我的代碼:

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

但我唯一得到的是一個空的第一columnsequence

哪裏是錯誤這裏?

+1

'a [$ 1] = $ 1'應該是'a [$ 1] = $ 2' – karakfa

+0

歡迎使用Stack Overflow。 請注意,在這裏說'謝謝'的首選方式是通過 提高投票的好問題和有用的答案(一旦你有足夠的聲譽這樣做),並接受任何 問題最有用的答案,你問(這也給你一個小小的提升,以你的聲望 )。 請參閱[關於]頁面,以及[如何在此處提問 ?]和 [當有人回答我的 問題時,我該怎麼辦? ?](http://stackoverflow.com/help/someone-answers) –

回答

2

你的任務是不正確的。

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

Name1 sequence1 
Name2 sequence2 
+0

是的,就是這樣!儘管我需要'FS = OFS',因爲我的名字中有空格 – rororo