Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Merge the difference between two files

by NetWallah (Canon)
on Jun 16, 2017 at 06:23 UTC ( [id://1192911]=note: print w/replies, xml ) Need Help??


in reply to Merge the difference between two files

I did not notice afoken's note until after I wrote this code, so here it is.
To do this right, you need a slightly complicated structure - a hash-of-arrays.
#!/bin/perl -w use strict; use warnings; my @filenames = ("file1.txt", "file2.txt"); my %result; # Hash of arrays for my $fname(@filenames){ my $currentheader = ""; open my $f, "<", $fname or die "Cannot open '$fname' : $!"; while (my $line = <$f>){ chomp $line; next unless length($line); # Skip blank lines if ($line=~/NAME/){ $currentheader = $line; next; } push @{ $result{$currentheader} }, $line; } } # Print results for my $currentheader(sort keys %result){ print "\n$currentheader\n"; print "$_\n" for @{ $result{$currentheader} }; }

                Once it hits the fan, the only rational choice is to sweep it up, package it, and sell it as fertilizer.

Replies are listed 'Best First'.
Re^2: Merge the difference between two files
by hopper (Novice) on Jun 20, 2017 at 04:08 UTC

    The code is merging the headers of both files if they are different. However, I don't want it compares the headers, just want it compares and merge the if the line start with "NAME". Can anyone please help me with this task? Thanks so much in inadvance.

Re^2: Merge the difference between two files
by hopper (Novice) on Jun 16, 2017 at 18:07 UTC
    Hi NetWallah, I really like the code that you wrote. However It is not printout out the way I want.

    Currently, it finds the line "NAME" if match then merge the content. however, I want to separate two files and just merge unmatched

    line/content "NAME" that is in file 2 to file#1. If the line "NAME" match ignore the content of the lines. can I print the result to a

    file and not on the display only?

    For example:(ignore the content, if line "NAME, VAR1, VAR2" match)

    File 1: NAME, VAR1, VAR2 apple, mange File 2: NAME, VAR1, VAR2 jack fruit, banana

      Try this, still using hash-of-arrays

      #!/bin/perl -w use strict; use warnings; # input my %hash=(); my @filename = ('File1.txt','File2.txt'); for my $n (0..1){ parse($n,$filename[$n]); }; output('output.txt'); # parse sub parse { my ($n,$filename) = @_; open my $fh,'<',$filename or die "$!"; my $key; while (<$fh>){ if (/^NAME/){ $key = $_; $hash{$key}[$n] = ''; } else { $hash{$key}[$n] .= $_ if $key; } } close $fh; } # merge sub output { my ($filename) = @_; open my $fh,'>',$filename or die "$!"; for my $key (sort keys %hash){ if (defined $hash{$key}[0]){ print $fh $key.$hash{$key}[0]; } else { print $fh $key.$hash{$key}[1]; } } close $fh; }
      poj
      To achieve this, after the line
      $currentheader = $line;
      add this:
      if (exists $result{$currentheader}){ $currentheader="RECYCLE_BIN"; }
      I will leave it as an exercise for you to figure hou how to avoid printing the "RECYCLE_BIN" item.

                      Once it hits the fan, the only rational choice is to sweep it up, package it, and sell it as fertilizer.

        I was able to print the unmatched contents to a file. could you please help me edit the code to do just compare the content of the line "NAME" and print out the header of the file A. Don't compare the headers. I tried to figure out but so far I have no luck. Thanks so much in advance

      can I print the result to a file and not on the display only?

      Did you even try to figure this out by yourself or are you just looking for someone to do all your work for you?

      https://perldoc.perl.org/functions/select.html
      select FILEHANDLE

      If FILEHANDLE is supplied, sets the new current default filehandle for output. ... , a write or a print without a filehandle default to this FILEHANDLE.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1192911]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-20 00:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found