如何与终端逐行比较2个文件

时间:2019-07-17 16:05:12

标签: python linux bash perl terminal

我有2个文本文件,需要逐行比较。

我基本上是想根据每行的匹配输出“匹配”或“不匹配”。

我尝试阅读一些教程,并使用诸如diffdircmp之类的方法,但似乎找不到解决方法。我不在乎它是bash,perl,python等。这两个文件均为243行。

Linux中有可用的命令来执行此操作吗?

这是我正在寻找的示例...

文件1

Test
Hello
Example

文件2

Test
What
Example

我想输出以下内容:

matching
not matching
matching

3 个答案:

答案 0 :(得分:1)

在perl中:

#!/usr/bin/perl

use strict;
use File::Slurp;

my @file1 = read_file 'file1', { chomp => 1 };
my @file2 = read_file 'file2', { chomp => 1 };

foreach (@file1) {
  my $line = shift @file2;
  print $_ eq $line ? "not matching\n" : "matching\n";
}

答案 1 :(得分:0)

您需要的是以下格式的awk脚本:

$ awk '(NR==FNR){a[FNR]=$0;next}
       !(FNR in a) { print "file2 has more lines than file1"; exit 1 }
       { print (($0 == a[FNR]) ? "matching" : "not matching") }
       END { if (NR-FNR > FNR) print "file1 has more lines than file2"; exit 1}' file1 file2

答案 2 :(得分:0)

此脚本的工作原理是两个文件均为243行。您需要先对两个文件进行排序,然后再运行脚本sort file1.txt > file1.sorted.txt,而对另一个文件进行排序。

#!/bin/bash
while read file1 <&3 && read file2 <&4
  if [[ $file1 == $file2 ]]; then
    echo "matching" >> three.txt
  else
    echo "not matching" >> three.txt
  fi
done 3</path/to/file1.sorted.txt 4</path/to/file2.sorted.txt

上面的脚本将逐行读取每个文件,并使用if语句比较输入。如果两个字符串相同,则它将“匹配”写入three.txt,否则将“不匹配”写入同一文件。循环将遍历每一行。

您将必须对两个文件中的数据进行排序以进行比较。 我已经使用以下数据对其进行了测试:

one.sorted.txt

abc
cba
efg
gfe
xyz
zxy

two.sorted.txt

abc
cbd
efh
gfe
xyz
zmo

three.txt

matching
not matching
not matching
matching
matching
not matching
相关问题