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 ! \n");
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 ! \n");
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\n";
push(@in_a_add_in_b,$line);
}
else{
#print "$line\n";
push(@in_a_not_in_b,$line);
}
}
foreach my $line (@b_lines){
next if($line =~ /^\s*$/);
if($line ~~ @a_lines){
#print "$line\n";
}
else{
#print "$line\n";
push(@in_b_not_in_a,$line);
}
}
print ("\n");
print("analys results:\n");
print ("-"x100,"\n");
print("The following $#in_a_add_in_b cases in $a_file add in $b_file:\n");
print ("-"x100,"\n");
foreach my $line (@in_a_add_in_b){
print("$line\n");
}
print ("\n\n");
print ("-"x100,"\n");
print("The following $#in_a_not_in_b cases in $a_file but not in $b_file:\n");
print ("-"x100,"\n");
foreach my $line (@in_a_not_in_b){
print("$line\n");
}
print ("\n\n");
print ("-"x100,"\n");
print("New Line\n");
print("The following $#in_b_not_in_a cases in $b_file but not in $a_file:\n");
print ("-"x100,"\n");
foreach my $line (@in_b_not_in_a){
print("$line\n");
}
print ("\n\n");
#################################################
# Sub-routine: print_usage()
#################################################
sub print_usage {
print "\nUsage: perl $0 -r <reference file> -a <analysis file>\\\n";
print " [-l <testcase_all_lst> -d <logdir>] \\\n";
print " [-h] \n\n";
print "For example:\n";
print " perl $0 -r reference -a analysis\n";
print " perl $0 -h \n";
print "\n";
}
#################################################
# 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";
}
}
}
形而上者谓之道 形而下者谓之器。