Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • 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?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-26 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found