【问题标题】:Comparing two files with multiple columns比较具有多列的两个文件
【发布时间】:2022-01-21 07:18:04
【问题描述】:

需要比较 File1 的第一列和 File2 的第一列。如果匹配,则比较两个文件的第二列。如果第二列不匹配,则从 File1 打印该行并将输出保存到另一个文件中。

files1.txt

80002288    b17
97380002001 b18
97380002220 b17
97380002233 b18
80002333    b17
16501111    b04
16505044    b04
16505042    b04
97316505030  b05
16505043    b04
16505048    b04

文件2.txt

97366630003 a01
97380002288 b17
97380002001 b17
97380002220 b17
97380002233 b17
97380002333 b17
97316501111 b04
97316505044 b04
97316505042 b04
97316505030 b04
97316505043 b04

期望的输出

97380002001 b17
97316505030 b04

【问题讨论】:

  • 我相信每个文件中只有 2 列,但由于文件的内容在一行中,所以问题中并不清楚。如果是这种情况,请更新问题。
  • 我们鼓励您展示您迄今为止所尝试的内容。
  • 您的输出似乎有来自 File2 的行,而不是来自 File1 的行。此外,97380002233 b18 行也应该包含在输出中,对吧?
  • 您想要的输出与问题描述不符。
  • 相对于您声明的要求和示例输入,您的预期输出没有意义。

标签: bash shell for-loop awk nested


【解决方案1】:

方法一:没有任何外部库

使用以下代码仅使用 python 获取输出

with open('files3.txt', 'w') as files3:
    with open('files1.txt') as files1:
        for line_a in files1.readlines():
            words_a = line_a.split()
            with open('files2.txt') as files2:
                for line_b in files2.readlines():
                    words_b = line_b.split()
                    if words_a[0] == words_b[0] and words_a[1] != words_b[1]:
                        diff_words = ' '.join(words_b)
                        files3.write(diff_words + '\n')
                        print(diff_words)

以上代码的输出

97380002001 b17
97380002233 b17
97316505030 b04

方法 2:使用 Pandas 库

您可以使用python的pandas库来实现这一点。 所以首先安装 pandas 库,如:

pip install pandas

然后运行下面的python代码来创建想要的文件

import pandas as pd

# you can replace files1.txt and files2.txt with the complete path if files aren't in the same folder
df1 = pd.read_csv("files1.txt", sep=r'\s+', names=['c1', 'c2'])
df2 = pd.read_csv("files2.txt", sep=r'\s+', names=['c1', 'c2'])

df3 = pd.merge(df1, df2, on='c1')
df3 = df3[(df3["c2_x"] != (df3["c2_y"]))]

# use below if you want to save values from file 2
print(df3[['c1', 'c2_y']].to_string(index=False, header=False))
df3[['c1', 'c2_y']].to_csv("files3.txt", sep=' ', index=False, header=False)

# use below if you want to save values from file 1
# print(df3[['c1', 'c2_x']].to_string(index=False, header=False))
# df3[['c1', 'c2_x']].to_csv("Files3.txt", sep=' ', index=False, header=False)

# use below code to save values from both files
# print(df3.to_string(index=False, header=False))
# df3.to_csv("Files3.txt", sep=' ', index=False, header=False)

以上代码的输出

97380002001 b17
97380002233 b17
97316505030 b04

【讨论】:

  • 我尝试关注但我得到了匹配的项目,没有得到不匹配的值。 awk 'FNR!=NR {a[$1]++;下一个} a[$1]' $input2 $input1
  • 我无法在该服务器上安装 pip。
  • @dev05btech 请检查更新的方法 1
【解决方案2】:

其中任何一个都可能是您想要的,但您发布的预期输出与您对需求的任何解释都不匹配。在每个 Unix 机器上的任何 shell 中使用任何 awk:

要打印文件 1 中的行:

$ awk 'NR==FNR{a[$1]=$2; next} ($1 in a) && (a[$1] != $2)' file2 file1
97380002001 b18
97380002233 b18
97316505030  b05

要打印 file2 中的行,只需交换输入文件名:

$ awk 'NR==FNR{a[$1]=$2; next} ($1 in a) && (a[$1] != $2)' file1 file2
97380002001 b17
97380002233 b17
97316505030 b04

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多