Hi! I need help in merging the difference between two files based on the same content within them and sorting and saving in the third file following are my example files:
File1: NAME, ID1, ID2 apple banana NAME, ID1, ID3 strawberry grape File 2: NAME, ID1, ID2 apple jackfruit NAME, ID1, ID4 banana grapes Desired output result: NAME, ID1, ID2 apple banana NAME, ID1, ID3 strawberry grape NAME, ID1, ID4 banana grapes However, this is what I get: NAME, ID1, ID2 NAME, ID1, ID3 NAME, ID1, ID4 #!/bin/perl -w use strict; use warnings; use File::Copy; use Cwd; my $dir = cwd; main(); sub main { printf "\nStarting script\n"; printf "\nEnter file 1: "; my $a = <STDIN>; chomp $a; printf "\n"; printf "Enter file 2: "; my $b = <STDIN>; chomp $b; my $output = "output.txt"; if(-e $a and -e $b) { my $counter = 0; my $counter2 = 0; my %results = (); open (FILEA, "<$a") or die "Input file $a not found.\n"; while(my $line = <FILEA>) { $counter++; if($line =~ /^NAME/) { my ($name, $variable1, $variable2) = split(',', $l +ine, 3); $results{$line}=1; } } close(FILEA); open (FILEB, "<$b") or die "Input file $b not found.\n"; while(my $line =<FILEB>) { if($line =~ /^NAME/) { my ($name, $variable1, $variable2) = split(',', $l +ine, 3); $results{$line}++; } } close(FILEB); open (OUTPUT, ">$output") or die "Cannot open $output for +writing \n"; foreach my $line (keys %results) { print OUTPUT "$line"; $counter = $counter if $counter != $counter2; } close OUTPUT; } }
I am new to perl so please someone could help me and direct me to the right direction.

Thanks so much in advance.


In reply to Merge the difference between two files 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.