how do I use a conditional in this one liner?

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.

Answers


For one thing, the postfix if $a{$1} > 0 expression gets evaluated first, as if you said

if ($a{$1} > 0) {
    /^"[0-9]+","[0-9]+","(.+)"/ && ++$a{$1} && print "$_ $a{$1}\n"
}

but $a{...} (and $1) will only get updated inside the if block, so the if statement is never true.

For another thing, your regex has double quote characters but your sample input doesn't. Was that a typo?


Need Your Help

notifyDataSetChanged() doesn't work with my custom adapter

android android-fragments android-adapter android-recyclerview

I've created a simple RecyclerView.Adapter and it displays the content properly, but when I call notifyDataSetChanged() on it - it doesn't work.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.