Thursday, April 28, 2011

Awk - print up-to nth occurrence of pattern


Input file:
$ cat file.txt
FR 24
AA 33
AA 36
EE 34
BE 30
AA 31
DE 90
AL 10
AA 50
FR 67

Required: Print the portion of the above file up-to the 3rd occurrence of the pattern "AA" as the first field.
$ awk '
$1=="AA" {c++} {print $0}
c==3 {exit}
' file.txt

Output:
FR 24
AA 33
AA 36
EE 34
BE 30
AA 31

Related posts:
- Print first occurrence of a set using Awk

No comments:

© Jadu Saikia www.UNIXCL.com