• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

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

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?

1 Solution
nemws1Database AdministratorCommented:
This is what I whipped up:

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


Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now