如何基于一个合并两个数据

时间:2018-01-09 03:53:44

标签: bash unix

我在文件夹

中保存了两个数据.txt

数据1称为数据1包括一个列数据如下

from
A0A0A6YXQ7
A0A0A6YXS5
A0A0A6YXW8
A0A0A6YXX6
A0A0A6YXZ1
A0A0A6YY28
A0A0A6YY43
A0A0A6YY47
A0A0A6YY78
A0A0A6YY89
A0A0A6YY91
A0A0A7NQN9

,第二个数据有两列fromto

from    to
A0A0A6YXQ7  Myo1f
A0A0A6YXW8  Pak2
A0A0A6YXX6  Arhgap15
A0A0A6YXZ1  Igtp
A0A0A6YY28  pol
A0A0A6YY47  MumuTL
A0A0A6YY78  MumuTL
A0A0A6YY78  MumuTLM
A0A0A6YY91  MumuTL
A0A0A6YY91  MumuTLM

data1和data2有一个名为from的列 data1中的所有字符串都应该在data2中。如果他们不是。

我想加载这两个数据,如果data2中不存在任何字符串,我想把它作为data1放在那里

例如,在data2中缺少以下字符串

A0A0A6YXS5和A0A0A6YY43以及A0A0A6YY89和A0A0A7NQN9

所以输出看起来像这样

From       To
A0A0A6YXQ7  Myo1f
A0A0A6YXS5  -
A0A0A6YXW8  Pak2
A0A0A6YXX6  Arhgap15
A0A0A6YXZ1  Igtp
A0A0A6YY28  pol
A0A0A6YY43  -
A0A0A6YY47  MumuTL
A0A0A6YY78  MumuTL;MumuTLM
A0A0A6YY89  -
A0A0A6YY91  MumuTL;MumuTLM
A0A0A7NQN9  -

1 个答案:

答案 0 :(得分:0)

怎么样:

#!/bin/bash

declare -A hash

# scan in file2 and make a key-value(s) table
while read line; do
    set -- $line
    if [ -z ${hash[$1]} ]; then
        hash[$1]=$2
    else
        hash[$1]="${hash[$1]};$2"
    fi
done < data2

# read file1 as keys and print appropriate value(s)
while read line; do
    if [ -z ${hash[$line]} ]; then
        echo $line "-"
    else
        echo $line ${hash[$line]}
    fi
done < data1

请注意,“from”和“to”对会被意外正确处理 希望这会有所帮助。