我有两个文件k1和k2,我正在尝试根据公共密钥“Book of Book”合并这两个文件。
K1
John | Dreaming of Day | IEEEJournal
Akon | Dreaming of Night | ACMJournal
K2
Dreaming of Day | Fiction
Dreaming of Night | Non-Fiction
kout:应根据书名Dreaming of Day
和Dreaming of Night
John | Fiction | IEEEJournal
Akon | Non-Fiction | ACMJournal
我写了下面的awk脚本:
awk -F"|" 'BEGIN{OFS="|"}FNR==NR{a[$1$2]=$3;next}($1$2 in a && $3=$3"|"a[$1$2])' k1 k2
但它似乎不起作用。
答案 0 :(得分:2)
您可以使用
构建sed
个参数
sed 's/\(.*\)|\(.*\)/s# \1#\2 #/' k2
这些参数可以在
的另一个sed命令中使用sed -f <(sed 's/\(.*\)|\(.*\)/s# \1#\2 #/' k2) k1
答案 1 :(得分:1)
使用数组数组的gawk
解决方案
gawk 'BEGIN{FS=" \\| "; OFS=" | "}
FNR==NR{d[$2][1] = $1; d[$2][3] = $3; next}
$1 in d{print d[$1][1], $2, d[$1][3]}
' k1 k2
使用awk
和sed
join
解决方案
join <(sed 's/ | /|/g' k1) <(sed 's/ | /|/g' k2) -1 2 -2 1 -t'|' -o '1.1 2.2 1.3' | sed 's/|/ | /g'
你得到了
John | Fiction | IEEEJournal
Akon | Non-Fiction | ACMJournal
编辑AWK VERSION
awk 'BEGIN{FS=" [|] "; OFS=" | "}
FNR==NR{d1[$2] = $1; d3[$2] = $3; next}
$1 in d1{print d1[$1], $2, d3[$1]}
' k1 k2
答案 2 :(得分:1)
join -12 -21 -t\| k1 k2
Dreaming of Day |John | IEEEJournal| Fiction
Dreaming of Night |Akon | ACMJournal| Non-Fiction
注意事项:
从输出中省略键
join -12 -21 -t\| k1 k2 | cut -f2- -d\|
John | IEEEJournal| Fiction
Akon | ACMJournal| Non-Fiction