bash删除具有保留顺序的键值重复项 [英] bash remove duplicate of key values with preserving order

查看:51
本文介绍了bash删除具有保留顺序的键值重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件需要合并并生成第3个文件.请在下面找到示例

I have 2 files which I need to combine and generate a 3rd file. Please find the sample below,

文件1

xab=p11
aab=p12
aac=p23
xac=p15
yab=p16

文件2

aab=p17
xac=p25
yyc=p22

我想保留第一个文件的顺序并追加第二个文件.结果应该是:

I would like to preserve the order of the first file and append the second file. The result should be:

文件3

xab=p11
aab=p17
aac=p23
xac=p25
yab=p16
yyc=p22

我尝试了许多方法,但无法获得一个更简单,易于理解的解决方案.我在StackOverflow中找到的那个正在工作,但是很难理解和向第三方解释.我找到的解决方案是

I tried many ways, but not able to get a simpler, easy to understandable solution. The one I found in StackOverflow was working, but it is hard to understand and explain to a third person. The solution I found was

cat en_us.txt en_US2.txt | tr -s '\n' | awk -F= '!a[$1]{b[++i]=$1} {a[$1]=$0;} END{for(j=1;j<=i;j++){print a[b[j]]}}'

任何人都可以尝试这种方法并获得可读的解决方案(可能是不使用 awk 的解决方案)

Can anyone try this and get a readable solution (probably one not using awk)

推荐答案

$ cat tst.awk
BEGIN { FS=OFS="=" }
{ key=$1; val=$2 }
NR==FNR {
    keys[++numKeys] = key
    key2val[key] = val
    next
}
{
    if ( key in key2val ) {
        val = key2val[key]
        delete key2val[key]
    }
    print key, val
}
END {
    for (keyNr=1; keyNr<=numKeys; keyNr++) {
        key = keys[keyNr]
        if (key in key2val) {
            print key, key2val[key]
        }
    }
}

$ awk -f tst.awk file2 file1
xab=p11
aab=p17
aac=p23
xac=p25
yab=p16
yyc=p22

这篇关于bash删除具有保留顺序的键值重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆