逐行比较4个文件以查看它们是否匹配 [英] Compare 4 files line by line to see if they match or don't match

查看:85
本文介绍了逐行比较4个文件以查看它们是否匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较4个文本文件中每一行的计数:

I'm trying to compare 4 text files for counts in each line:

file1.txt:
32
44
75
22
88

file2.txt
32
44
75
22
88

file3.txt
11
44
75
22
77

file4.txt
    32
    44
    75
    22
    88

每行代表一个标题

line1 = customerID count
line2 = employeeID count
line3 = active_users
line4 = inactive_users
line5 = deleted_users

我正在尝试将 file2.txt file3.txt file4.txt file1.txt ; file1.txt 将始终具有正确的计数.

I'm trying to compare file2.txt, file3.txt and file4.txt with file1.txt; file1.txt will always have the correct counts.

示例:由于在上面的示例中 file2.txt file1.txt 逐行精确匹配,因此我尝试输出"file2.txt很好" ,但是由于 file3.txt 第1行和第5行与 file1.txt 不匹配,因此我试图为文件3输出"customerID". txt与21条记录不匹配" ,( 32-11 = 21 )和file3.txt中的"deleted_users不匹配11条记录" ,( 88-77 = 11 ).

Example: Since file2.txt matches exactly line by line to file1.txt in the example above then i'm trying to output "file2.txt is good" but since file3.txt line1 and line5 do not match to file1.txt I'm trying to output "customerID for file3.txt does not match by 21 records", (i.e. 32 - 11 = 21), and "deleted_users in file3.txt does not match by 11 records", (88 - 77 = 11).

如果shell更容易,那也没关系.

If shell is easier then that is fine too.

推荐答案

一种通过行并行处理文件的方法

One way to process files by lines in parallel

use warnings;
use strict;
use feature 'say';

my @files = @ARGV;
#my @files = map { $_ . '.txt' } qw(f1 f2 f3 f4);  # my test files' names

# Open all files, filehandles in @fhs
my @fhs = map { open my $fh, '<', $_  or die "Can't open $_: $!"; $fh } @files;

# For reporting, enumerate file names
my %files = map { $_ => $files[$_] } 0..$#files;

# Process (compare) the same line from all files       
my $line_cnt;
LINE: while ( my @line = map { my $line = <$_>; $line } @fhs )
{
    defined || last LINE for @line;
    ++$line_cnt;
    s/(?:^\s+|\s+$)//g for @line;
    for my $i (1..$#line) {
        if ($line[0] != $line[$i]) { 
            say "File $files[$i] differs at line $line_cnt"; 
        }
    }
}

这将整条线==进行比较(在去除前导和尾随空格之后),因为假定每行都带有一个需要比较的数字.

This compares the whole line by == (after leading and trailing spaces are stripped), since it is a given that each line carries a single number which need be compared.

它以我的测试文件f1.txtf2.txt,...

It prints, with my test files named f1.txt, f2.txt, ...


File f3.txt differs at line 1
File f3.txt differs at line 5

这篇关于逐行比较4个文件以查看它们是否匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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