hopper has asked for the wisdom of the Perl Monks concerning the following question:
I am new to Perl and trying to compare two files and print out the differences of file #2 base from file 1. In other words, I want the keep the file 1 and delete the duplicate of file 2. Each file has sections and in each section contents different information. I want to diff the files. If there are sections. Here is my code
File1.txt
SECTION, ONE, 1, 4, YELLOW, HIGH, THIS IS COMMENT This is my line This is my page SECTION, THREE, 9, 4, RED, HIGH, THIS IS COMMENT This is a dog This is a cat
File2.txt
my output result:SECTION, ONE, 1, 4, YELLOW, HIGH, THIS IS COMMENT This is a cat This is not a cat SECTION, TWO, 2, 4, BLUE, HIGH, THIS IS COMMENT This is not a book This is a notebook
Output should be:SECTION, TWO, 2, 4, BLUE, HIGH, THIS IS COMMENT
Here is my code:SECTION, TWO, 2, 4, BLUE, HIGH, THIS IS COMMENT This is not a book This is a notebook
#!/bin/perl -w use strict; use warnings; use File::Copy; use Cwd; my $dir = cwd; main(); sub main { printf "\nStarting script\n"; printf "\nEnter the file 1: "; my $fh1 = <STDIN>; chomp $fh1; printf "\n"; printf "Enter the file 2: "; my $fh2 = <STDIN>; chomp $fh2; my $tempFile = "temp.txt"; my $nonMatch = "nonMatch.txt"; if(-e $fh1 and -e $fh2) { my %results = (); open (FILE1, "<$fh1") or die "Input file $fh1 not found.\n +"; while(my $line = < FILE1>) { if($line =~ /^Section/) { my ($sec, $first, $second, $third, $color, $mode, +$description_comments) = split(',', $line, 7); $results{$line}=1; } } close(FILE1); open (FILE, "<$fh2") or die "Input file $fh2 not found.\n" +; while(my $line = <>) { if($line =~ /^Section/) { my ($sec, $first, $second, $third, $color, $mode, +$description_comments) = split(',', $line, 7); $results{$line}++; } } close(FILE2); open (NONMATCH, ">$nonMatch") or die "Cannot open $nonMatc +h for writing \n"; foreach my $line (keys %results) { print NONMATCH " $results{$line} - $line" if $results{$li +ne} ==2; } close NONMATCH; } close FILE2; }
Back to
Seekers of Perl Wisdom