I have a script to eliminate duplicates. here is the file that it reads from:
161248,/vol/filelist,CABINET 161200,/vol/filelist,INVENTORY 161400,/vol/filelist,INVENTORY
I'd like it to identify the duplicated line (/vol/filelist,INVENTORY) and print out this last line (which would correspond to the #161400 in the first column. This is what I have so far:
open my $FH2, '<', '/tmp/fileread' or die "unable to open file 'file' +for reading : $!"; open my $FH6, '>', '/tmp/tst.txt' or die "unable to open file 'file' f +or reading : $!"; my %duplicates; while (<$FH2>) { chomp; my ($column_1, $column_2, $column_3) = split /,/; print {$FH6} "$column_1\n" if defined $duplicates{$column_2} && + $duplicates{$column_3}; $duplicates{$column_2}++; } close $FH6; close $FH2; open my $fh, '<', '/tmp/tst.txt' or die "unable to open file 'file' fo +r reading : $!"; while (my $line = <$fh>) { print $line; } close $fh;
This doesn't work. I can get it to work somewhat if I leave:
my ($column_1, $column_2) = split /,/; print {$FH6} "$column_1\n" if defined duplicates{$column_2}; $duplicates{$column_2}++; But this only causes the last 2 lines of the file to be deleted, and I + want the last line deleted because on the last line both columns 2 a +nd 3 are the same. So what I am really looking for is to print the # in the first column +which corresponds to both column 2 and 3 being the same. 161248,/vol/filelist,CABINET 161200,/vol/filelist,INVENTORY 161400,/vol/filelist,INVENTORY In the last line, column 2 (/vol/filelis) and column3 (INVENTORY) are +the same. Does anyone have a suggestion?

In reply to printing the first column when both columns 2 and 3 are the same by novice2015

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.