2

Suppose I have these 2 tab delimited files, where the second column in first file contains matching values from first column of the second file, I would like to get an output like this:

FileA:

1    A
2    B
3    C

FileB:

A    Apple
C    Cinnabon
B    Banana

I would like an output like this:

1   Apple
2   Banana
3   Cinnabon

I can write a script for this, but I would like to know how to make it in awk or perl in one line.

4

3 回答 3

2
awk 'BEGIN{FS=OFS="\t"}NR==FNR{a[$1]=$2;next}{$2=a[$2]}1' f2 f1
于 2013-06-14T02:37:05.440 回答
1

GNU sed oneliner

sed -r 's:\s*(\S+)\s+(\S+):/\\s*\\S\\+\\s\\+\1/s/\\(\\s*\\S\\+\\s\\+\\)\1/\\1\2/:' fileB | sed -f - fileA

..输出:

1 苹果
2 香蕉
3 肉桂
于 2013-06-14T15:17:24.100 回答
0

你想要的命令是这样的:

$ awk 'FNR==NR{a[$1]=$2 FS $3;next}{$2=a[$2]; print}' file2 file1
1 Apple
2 Banana
3 Cinnabon
于 2013-06-14T02:01:37.553 回答