perl对比两个文件的行

perl对比两个文件的行

对比两个文件的各行,得到A与B相同的行/A与B不相同的行

主要功能


  • 得到相同行
  • 得到A中包含,B不包含的行
  • 得到B中包含,A中不包含的行

具体执行情况


Perl代码


#!/usr/bin/perl
#----------------------------------------------------------------------
#
#      Filename: constrast_line.pl
#   Description: file function description
#
#        Author: 
#       Version: 1.0.0
#        Create: 2017-12-06 08:15:33
# Last Modified: 2017-12-06 08:15:33
#       History: Modify the history
#----------------------------------------------------------------------

use warnings;
use strict;
use List::MoreUtils qw/uniq/;

###########################################
# initial
###########################################

my $Has_Help = ""; 
my ($a_file, $b_file, , @a_lines, @b_lines, @in_a_not_in_b, @in_a_add_in_b, @in_b_not_in_a);

if( $#ARGV < 0 ) {
    &print_usage;
    exit;
}
&parse_argv;

if( $Has_Help =~ /TRUE/ )  #show help
{
    &print_usage;
    exit;
}


###########################################
# read A and B file
###########################################
open(REF_FLIE, "$a_file") || die ("Could not open file $a_file ! 
");
while (my $line = <REF_FLIE>){
     chomp($line);
     push(@a_lines,$line);
}
close REF_FLIE;

open(ANAL_FLIE, "$b_file") || die ("Could not open file $b_file ! 
");
while (my $line = <ANAL_FLIE>){
     chomp($line);
     push(@b_lines,$line);
}
close ANAL_FLIE;

###########################################
# handle
###########################################
foreach my $line (@a_lines){
    next if($line =~ /^s*$/);

    if($line ~~ @b_lines){
        #print "$line
";
        push(@in_a_add_in_b,$line);
    }
    else{
        #print "$line
";
        push(@in_a_not_in_b,$line);
    }
}

foreach my $line (@b_lines){
    next if($line =~ /^s*$/);

    if($line ~~ @a_lines){
        #print "$line
";
    }
    else{
        #print "$line
";
        push(@in_b_not_in_a,$line);
    }
}

print ("
");
print("analys results:
");
print ("-"x100,"
");
print("The following $#in_a_add_in_b cases in $a_file add in $b_file:
");
print ("-"x100,"
");
foreach my $line (@in_a_add_in_b){
    print("$line
");
}

print ("

");
print ("-"x100,"
");
print("The following $#in_a_not_in_b cases in $a_file but not in $b_file:
");
print ("-"x100,"
");
foreach my $line (@in_a_not_in_b){
    print("$line
");
}

print ("

");
print ("-"x100,"
");
print("New Line
");
print("The following $#in_b_not_in_a cases in $b_file but not in $a_file:
");
print ("-"x100,"
");
foreach my $line (@in_b_not_in_a){
    print("$line
");
}


print ("

");



#################################################
# Sub-routine: print_usage()   
#################################################
sub print_usage {
    print "
Usage: perl $0 -r <reference file> -a <analysis file>\
";  
    print "                [-l <testcase_all_lst> -d <logdir>] \
";
    print "                [-h] 

";
    print "For example:
";
    print "    perl $0 -r reference -a analysis
"; 
    print "    perl $0 -h 
"; 
    print "
";
}

#################################################
# Sub-routine : parse_argv()   
#################################################
sub parse_argv {
    if($#ARGV != 3){
        $Has_Help = "TRUE";
        return;
    }
    for(my $i=0; $i<=$#ARGV; $i++) {
        if( $ARGV[$i] =~ /-r/ ) {
            $i++;
            if(!defined $ARGV[$i])
            {
                $Has_Help = "TRUE";
            }
            $a_file      = $ARGV[$i];
        }
        elsif( $ARGV[$i] =~ /-a/ ) {
            $i++;
            if(!defined $ARGV[$i])
            {
                $Has_Help = "TRUE";
            }
            $b_file     = $ARGV[$i];
        }
        elsif( $ARGV[$i] =~ /-h/ ) {
            $Has_Help = "TRUE";
        }
        else { ### other options
            $Has_Help = "TRUE";
        }
    }
}
原文地址:https://www.cnblogs.com/OneFri/p/7991526.html