[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

awk 'A[$2]++==0' inputfile

Posted on 2011-05-02
7
Medium Priority
?
245 Views
Last Modified: 2012-05-11
What does it mean
awk 'A[$2]++==0' inputfile

Open in new window


Thanks
0
Comment
Question by:zhshqzyc
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:JIEXA
ID: 35504762
It skips the lines from 'inputfile' that 2nd field of them appears for the first time.
0
 

Author Comment

by:zhshqzyc
ID: 35504799
Can you provide a link for that? Usually I saw $2 in awk but never saw A[$2].
0
 
LVL 8

Expert Comment

by:JIEXA
ID: 35506295
Which link?
The syntax is: expression1 { expression2; }
It means whether input matches expression1, do expression2.
The expression2 is empty (i.e. skip lines), and A[$2] is first undefined, and then incremented.
If the original value (since the increment is postfixed, the x++ returns the previous value of x, and then increments) was 0, the skip is done...

Which part is not understood? This is a kind of code that easy to write but takes more time to explain.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Expert Comment

by:JIEXA
ID: 35506481
The "A\[i\]" is associative array. "i" can be either number or any string.
0
 

Author Comment

by:zhshqzyc
ID: 35507072
Okay. I am not sure whether the answer is right or not. Could you please take a look?
0
 
LVL 8

Accepted Solution

by:
JIEXA earned 1000 total points
ID: 35507259
Sorry, I'm mistaken. That answer was correct; it removes the duplicates.

Before first occurrence of 2nd field, the A[$2] is 0.
At the first occurrence of 2nd field, the A[$2] becomes 1, but the "++" is postfix, so "A[$2]++" has old value, thus is 0, and it does the action in curly braces that is absent here, thus, by default, "print;".
At the next occurrence of 2nd field, the A[$2] becomes value greater than 1, and the equality is false, and no printing is done.
0
 

Author Closing Comment

by:zhshqzyc
ID: 35507592
Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question