【发布时间】:2010-12-19 07:25:37
【问题描述】:
我有两个文件我想合并到第三个文件中,但我需要查看它们何时共享一个公共字段以及它们的不同之处。由于其他字段存在细微差异,我无法使用差异工具,我想这可以用 awk 完成。
文件 1:
aWonderfulMachine 1 mlqsjflk
AnotherWonderfulMachine 2 mlksjf
YetAnother WonderfulMachine 3 sdg
TrashWeWon'tBuy 4 jhfgjh
MoreTrash 5 qsfqf
MiscelleneousStuff 6 qfsdf
MoreMiscelleneousStuff 7 qsfwsf
文件2:
aWonderfulMachine 22 dfhdhg
aWonderfulMachine 23 dfhh
aWonderfulMachine 24 qdgfqf
AnotherWonderfulMachine 25 qsfsq
AnotherWonderfulMachine 26 qfwdsf
MoreDifferentStuff 27 qsfsdf
StrangeStuffBought 28 qsfsdf
期望的输出:
aWonderfulMachine 1 mlqsjflk aWonderfulMachine 22 dfhdhg
aWonderfulMachine 23 dfhdhg
aWonderfulMachine 24 dfhh
AnotherWonderfulMachine 2 mlksjf AnotherWonderfulMachine 25 qfwdsf
AnotherWonderfulMachine 26 qfwdsf
File1
YetAnother WonderfulMachine 3 sdg
TrashWeWon'tBuy 4 jhfgjh
MoreTrash 5 qsfqf
MiscelleneousStuff 6 qfsdf
MoreMiscelleneousStuff 7 qsfwsf
File2
MoreDifferentStuff 27 qsfsdf
StrangeStuffBought 28 qsfsdf
我在这里和那里尝试了一些 awks 脚本,但它们要么仅基于两个字段,我不知道如何修改输出,要么它们仅基于两个字段删除重复项,等等(我我是新手,awk 语法很难)。 非常感谢您的帮助。
【问题讨论】:
-
文件 2 中的任何键都会在文件 1 中重复吗? IE。可能是多对多关系还是严格的一对多关系?在任何情况下,awk 可能不是用于此目的的最佳工具,它的数组运算符有点弱,您或多或少必须通过将两个文件读入大的关联数组然后对它们进行抨击来实现这一点。
-
是的,两个文件中可能有重复的键。
-
顺便说一句,如果您指定使用制表符来分隔输入和输出中的字段,则可以避免此问题引起的一定程度的混淆。