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

Hi all. I have a file with two columns of data, x and y. If both are equal i need the line number that this occurs on.
file 1 2 2 2 3 1
i.e print line number 2 i guess i could use a counter and go through the file but any other suggestions? Thanks

Replies are listed 'Best First'.
Re: printing line number
by VSarkiss (Monsignor) on Sep 16, 2004 at 15:09 UTC

    Look in perlvar for $., the current input line number. Presuming it's on STDIN and the columns are separated by white space:

    my @columns = split /\s+/; print $., "\n" if $column[0] == $column[1];

      thanks for that, i guess therefore it is possible to use % with $. to print lines based on %
Re: printing line number
by sweetblood (Prior) on Sep 16, 2004 at 15:11 UTC
    Sounds like homework so I'll just give you a hint:
    check out $. perldoc perlvar

    Sweetblood

Re: printing line number
by Random_Walk (Prior) on Sep 16, 2004 at 15:19 UTC

    First will print line No if numericaly equal or string equal, second only for lines with equal numbers

    perl -ne 'print $. if /^(.+)\s+\1$/' perl -ne 'print $. if /^(\d+)\s+\1$/'

    Cheers,
    R.

Re: printing line number
by graff (Chancellor) on Sep 17, 2004 at 08:37 UTC
    perl -lane 'print $. if $F[0]==$F[1]' file
    Replace '==' with ' eq ' if you would prefer a string comparison rather than numeric (and use double quotes instead of single-quotes if you're using a DOS shell).