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

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
my output result:
SECTION, TWO, 2, 4, BLUE, HIGH, THIS IS COMMENT
Output should be:
SECTION, TWO, 2, 4, BLUE, HIGH, THIS IS COMMENT This is not a book This is a notebook
Here is my code:
#!/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; }

In reply to compare two files and print the differences of file 2 in a third file by hopper

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.