简体   繁体   English

使用perl从两个文本文件中删除重复项(这两个文本文件是另一个perl脚本的输出)

[英]Removing duplicates from two textfiles using perl (this 2 text files are output of the other perl script)

have written one perl script which gives two textfile as output. 已经编写了一个perl脚本,该脚本提供了两个文本文件作为输出。 The first text file contains value like below, 0900 0915 0930 0945 1000 1015 1030 1045 1100 ~ and the second text file contains values like this, 0900 0915 0930 1000 1015 1030 1100 I want to compare two files and remove the match values and display the mismatch value in a textfile.The output should be 0945 1045 I tried with the below code, 第一个文本文件包含如下值0900 0915 0930 0945 1000 1015 1030 1045 1100〜,第二个文本文件包含以下值0900 0915 0930 1000 1015 1030 1100我想比较两个文件并删除匹配值并显示文本文件中的值不匹配。输出应为0945 1045,我尝试使用以下代码,

#!/usr/bin/perl
use strict;
use warnings;

$\="\n";

open my $fh1, '<', 'textfile1.txt' or die $!;
open my $fh2, '<', 'textfile2.txt' or die $!;
open my $out, '>', 'output.txt' or die $!;

chomp(my @arr1=<$fh1>);
chomp(my @arr2=<$fh2>);

foreach my $x (@arr1){
        print $out $x if (!grep (/^\Q$x\E$/,@arr2));
}

close $fh1;
close $fh2;
close $out;

But Im getting textfile1 contents as output which is not expected output. 但是我正在获取textfile1内容作为输出,这不是预期的输出。 If I create a new textfiles with the same values manually and executes it works fine. 如果我手动创建一个具有相同值的新文本文件并执行,则效果很好。 Since this text files are output of my perl script. 由于此文本文件是我的perl脚本的输出。 I think its not taking this as input properly. 我认为它没有适当地将此作为输入。 Could someone please help me on this? 有人可以帮我吗?

It seems your input file doesn't contain those numbers in separate lines, otherwise your code works just fine. 看来您的输入文件中没有单独包含这些数字,否则您的代码就可以正常工作。

Try adding following lines before foreach loop 尝试在foreach循环之前添加以下行

@arr1 = split (' ', join (' ', @arr1) );
@arr2 = split (' ', join (' ', @arr2) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM