I have an input file with 3 columns, and I wanted to print lines where the 3rd column hasn’t been duplicated. so if my input data looks like this:
0,1,abc
0,2,abc
0,5,xyz
I would print:
0,1,abc
0,5,xyz
I initially started with the below, but it didn’t produce any output. what am I doing wrong in this line?
perl -ne '/^[0-9]+,[0-9]+,(.+)/ && ++$a{$1} && print "$_ $a{$1}\n" if $a{$1}>0'
please note that I’m not looking for a solution to my problem, as I coded it in a different way. but I am interested in why the above line doesn’t do what I expect, as it exposes a gap in my perl understanding.
For one thing, the postfix
if $a{$1} > 0expression gets evaluated first, as if you saidbut
$a{...}(and$1) will only get updated inside theifblock, so theifstatement is never true.For another thing, your regex has double quote characters but your sample input doesn’t. Was that a typo?