2

Using awk, how to show the nth occurrence of a record and delete the other occurrence, for example"

Input File:

79933,2
79933,4
79933,5
55251,1
49494,4
49494,2
49494,2
49494,1

in this example, if the desired output is the 2nd occurrence, it is will be as follow:

79933,4
49494,2
4

POSIXly:

n=2 awk -F, '++a[$1]==ENVIRON["n"]' <file

Occurrence of 1st field was track by increasing each time it was seen, saving in array a. If it's 2nd occurrence, the condition became true, making awk to print $0.

  • thank you @cuonglm, but could you explain it please? – Eng7 Aug 2 '15 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.