Solved

How to get the list of line numbers with the diff command in Perl?

Posted on 2013-05-29
1
343 Views
Last Modified: 2013-06-03
Hi,
I need to figure out which lines are different in two files. And I need the exact line numbers that are different then I want to put them in an array.

The sudo code will be like this:

do diff on two files (new file and old file).

get the line numbers that were edited (added or deleted) in the new file.

put the line numbers in an array

Open in new window


How can do it in Perl?

Thanks,
0
Comment
Question by:Tolgar
1 Comment
 
LVL 23

Accepted Solution

by:
nemws1 earned 500 total points
ID: 39206024
This is what I whipped up:

#!/usr/bin/perl
my $f1start, $f2start, $junk;
open(DIFF, "diff test1 test2 |");
while (<DIFF>) {
    chomp;
    if (/^\d/) {            # line numbers
        ($f1start, $f2start) = split('c', $_);
        if ($f1start =~ /,/) {
            ($f1start, $junk) = split(',', $f1start);
        }
        if ($f2start =~ /,/) {
            ($f2start, $junk) = split(',', $f2start);
        }
        next;
    }
    if (/^\<\s/) {          # only in first file
        print $f1start++;
        print " $_\n";
    }
    if (/^-+/) {
        next;
    }
    if (/^\>\s/) {          # only in second file
        print $f2start++;
        print " $_\n";
    }
}

close(DIFF);

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
crawling gofundme 4 127
Any syntax error for this clone.plscript 6 140
Using Perl DBI to query oracle 3 43
.properties file to call function/method 9 61
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question