Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

difference between two files

by varalaxmibbnl (Acolyte)
on Dec 19, 2013 at 10:27 UTC ( [id://1067802]=perlquestion: print w/replies, xml ) Need Help??

varalaxmibbnl has asked for the wisdom of the Perl Monks concerning the following question:

hi monks.. i have two files..

file 1 =

hi 1 hw r u hi 2 hw r u

and file 2 =

hi 1 hw r u hi 2 hw r u hi 3 hw r u

i want to compare these two files and print the output in file 3 as

hi 3 hw r u

can you please help me out how to solve this problem....thanks in advance

Replies are listed 'Best First'.
Re: difference between two files
by Corion (Patriarch) on Dec 19, 2013 at 10:30 UTC

    What code have you written so far?

    This is a faq, see perlfaq4 on "duplicate".

    Alternatively, see Algorithm::Diff.

    Maybe you want to tell us in more detail what makes the last five lines special.

      i tried with this but i'm not getting the required output

      #!/usr/bin/perl open (FILE,"file1"); @cnt_file = <FILE>; $cnt = @cnt_file; print "$cnt\n"; open (MYFILE,"file2"); $line = <MYFILE>; foreach $line (<MYFILE>) { print "$line" if $. >= $cnt; }

        varalaxmibbnl:

        When you have a problem like this, you should add some print statements to show your variables, so you can verify that they hold what you're expecting. In this case, try adding $. to your existing print statement, like this:

        print "$., $line"

        When you do so, your output is:

        10 15: 1 15: hw 15: r 15: u 15: hi 15: 2 15: hw 15: r 15: u 15: hi 15: 3 15: hw 15: r 15: u

        So you can see that the line number is 15 for every iteration through your for loop. Why is that? It turns out that when you use foreach it wants a list in the parenthesis, so it reads the entire file at once. It then gives you one line at a time.

        Generally when processing a file and want to do it one line at a time, we do it like:

        while (my $line = <MYFILE>) { ... code in loop ... }

        There are some other issues in your code, but I'll let your instructor clarify that.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: difference between two files
by 2teez (Vicar) on Dec 19, 2013 at 14:04 UTC

    Hi varalaxmibbnl,
    Several good advice has been given, however comparing the content of the files you presented in your original post.
    I can see that file1 is contained in file2 in such a way that if the two files are saved in different arrays, one can use splice to get the difference of the files like:

    use warnings; use strict; use Inline::Files; my @file1 = <DATA1>; # updated my @file2 = <DATA2>; # updated print $_ for splice @file2, $#file1 - 1, $#file2; __DATA1__ hi 1 hw r u hi 2 hw r u __DATA2__ hi 1 hw r u hi 2 hw r u hi 3 hw r u
    NOTE:
    This might not work like you wanted, if the lines of the first file doesn't follow just like that of the second file.

    Updated: Change chomp(my @file1 = <DATA1>);
    chomp is really not needed here, as pointed out by hdb

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      If you remove the chomp statements, you can also remove "$_, $/ for" from the print statement.

        Yes Oooo, o yes, but don't you think one is cryptic enough with the given code already, than adding some more?
        I think it would be better to play clean a bit some of the time.
        Remove the newline character using the chomp and then re-insert when printing finally.

        hdb, on a second thought, you are right. chomp here is really not useful.
        Thanks, am updating my previous post.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me

      While not a bad solution (I gave it an upvote), here's a couple of points to consider:

      • You've added two blank lines between the __DATA1__ and __DATA2__ blocks. As a result, you've needed the "$#file1 - 1" calculation.
      • I think using splice here is overkill: a simple array slice would suffice.

      Putting those two points into effect:

      #!/usr/bin/env perl use strict; use warnings; use Inline::Files; my @file1 = <DATA1>; my @file2 = <DATA2>; print for @file2[@file1 .. $#file2]; __DATA1__ hi 1 hw r u hi 2 hw r u __DATA2__ hi 1 hw r u hi 2 hw r u hi 3 hw r u

      Output:

      hi 3 hw r u

      -- Ken

Re: difference between two files
by marto (Cardinal) on Dec 19, 2013 at 10:35 UTC

    What have you tried? What part are you having problems with? A Super Search, search engine would find you lots of examples of how to do this.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-18 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found