Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2023-02-06 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (34 votes). Check out past polls.

    Notices?