Hi,

Please help in the below task using perl script

compare file1 and file2 and print the updated line in file2, should print new line added in file2 and print missing line from file2

Dear Dev_9,

Welcome to Daniweb and we are glad to help.

To start with you will have to show what you have done and where you are have a problem or then show the error codes or statement you your program is displaying.

You may also like to provide some data examples, as used in your program and indicate how you want the final output to look like. Showing what and what you are comparing.

Doing the above makes it possible for us to help you quickly and give relevant suggestions instaed of broad assumed options.

Looking forward to hearing from you.

Hi,

For example,

file1 data:

qwer
asdf
zxcv
poiu
lkjh

file2 data:

qwer
asdf
zubncv
poiu

As per the above, in file2 last line is missing and that should be printed out.
And then in file2 3rd line is modified which should be printed(example: zxcv is modified as zubncv)
And if new line is added in file2, that should be printed.

Hi,

Please show what you have done. Where are you having issues in your code? If the code is written for you, you will not know how to do it yourself.

I will be more that willing to help if you show what you have done.

open my $fh, '<' 'file1' or die $1;
chomp (my @arr1 = <$fh>);

open my $fh1, '<' 'file2' or die $1;
chomp (my @arr2 = <$fh1>);

my (%m1, %m2);

@m1{@arr2}=();
@m2{@arr1}=();

delete @m1{@arr2};
delete @m2{@arr1};

print "$ is the missing line from file1\n" for grep {exists $m2{$}} @arr1;

print "$ is the updated line from file2\n" for grep {exists $m1{$}} @arr2;

I'm getting output like,

It's printing the missing line from form file2 along with the file1 data that is modified line in file2
And for updated line,

It's printing the updated lines from file2 along with the newly added line in file2...

I want all to be printed seperatly... Please help on this

Hi,

I really don't understand what you wanted. If you want to comapre and print out line in file1 that is missing in file2 then this is NOT how you want to do it.

To start with, your code will not run as it is.
So am suprised that you said you have an output in the first place. In the code above, the open function line. You are supposed to use 3 option arguments type of open, but you had 2.

open my $fh, '<' 'file1' or die $1; # Note: $fh, '<' (there should be a comma here) 'file1'

Sedondly, when you use die $1 it should be die $!. Big difference between 1 and !. The $! has a different meaning to $1 in perl.

Lastly, check your print line. Am sure you wanted to use $_, but what you had is $, which doesn't make sense in this line.

Check the code below if it works for you or you could make it into what you wanted

use warnings;
use strict;

my $file1 = 'file1.txt';
my $file2 = 'file2.txt';

open my $fh, '<', $file2 or die $!;
my $file = [<$fh>];

open $fh, '<', $file1 or die $!;
while(my $line = <$fh>) {
    chomp($line);
    my $status = 0;
    for (@{$file}) {
        chomp;
        if (/$line/) {
            $status = 1;
            last;
        }
    }
    print $line, $/ if $status == 0 
}

Hope this helps

Hi,

It prints only the missing line from file2. I also need updated lines and newly added lines if any from file2.

Thanks,

Hello,

It prints only the missing line from file2. I also need updated lines and newly added lines if any from file2.

How do you mean by updated lines and newly added lines? Show with an example.

Please find the below example,

If file1 has the below data

aimee.neece,aimee,neece,aimee.neece@faculty.asdf.org,aimee neece,aimee neece,"CN=aimee neece,OU=Imported,dc=Faculty,dc=asdf,dc=org"
alexis.andrews,alexis,andrews,alexis.andrews@faculty.asdf.org,alexis andrews,alexis andrews,"CN=alexis
alice.lee,alice,lee,alice.lee@faculty.asdf.org,alice lee,alice lee,"CN=alice lee,OU=Imported,dc=Faculty,dc=asdf,dc=org"
allyson.knanishu,allyson,knanishu,allyson.knanishu@faculty.asdf.org,allyson knanishu,allyson knanishu,"CN=allyson knanishu,OU=Imported,dc=Faculty,dc=asdf,dc=org"

If file2 contains below data

aimee.neece,aimee,neece,aimee.neece@faculty.asdf.org,aimee neece,aimee neece,"CN=aimee
alexis.andrews,alexis,andrews,alexis.andrews@faculty.asdf.org,alexis andrews,alexis andrews,"CN=alexis
alice.lee,alice,lee,alice.lee@faculty.asdf.org,alice lee,alice lee,"CN=alice lee,OU=Imported,dc=Faculty,dc=asdf,dc=org"
allyson.knanishu,allyson,knanishu,allyson.knanishu@faculty.asdf.org,allyson knanishu,allyson knanishu,"CN=James knanishu,OU=Imported,dc=Faculty,dc=asdf,dc=org"
andrews,OU=Imported,dc=Faculty,dc=asdf,dc=org"

From the file2,

2nd line ie, "neece,OU=Imported,dc=Faculty,dc=asdf,dc=org" is deleted. This should be printed in deleted list.
New line ie, "andrews,OU=Imported,dc=Faculty,dc=asdf,dc=org" is added in file2. This should be printed in new line added list.
allyson is updated to James in file2. Which should be printed in updated lines list.

Working out the difference between two files is trivial using (like I said earlier) diff and comm, both of which have existed since at least the 1970s.

The two provided files, diffed by word. Red bits have been removed, green added.

Screen_Shot_2018-02-15_at_16_18_03.png

Now, comm outputs three columns

  1. lines only in file 1
  2. lines only in file 2
  3. lines in both files

So by default, lines that were deleted in file 2 will be in column 1, lines that were added in file 2 will be in column 2, and lines that remain unchanged will be in column 3. You can surpress columns using -1, -2 or -3, so we can run the command multiple times to get a nice summary.

Lines deleted in f2:
deleted.png

Lines added in f2:
added.png

Lines unchanged:
unchanged.png

Thanks for the commands. Btw, am looking this concept in perl scripting.

Looks like you're actually going to have to write some code then!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.