tags:

views:

50

answers:

3

Dear all

I would like to print the first 4 lines by field 1 using awk

Input

111 1032192
111 2323476
111 1698881
111 2451712
111 2013780
112 2331004
112 1886376
112 1189765
112 1877267

Output

111 1032192
111 2323476
111 1698881
111 2451712
112 2331004
112 1886376
112 1189765
112 1877267

Thanks

Tony

+1  A: 
($1 != curkey) { curkey=$1; i=0; }
(i < 4)        { i++; print; }
larsmans
+2  A: 

awk '++count[$1] <= 4' input_file

glenn jackman
Nice one-liner!
larsmans
A: 

Dear Glen and Larsmans,

Thank you so much for your prompt response. I have not tried the former suggestion, but the latter (with awk) works like a charm.

Cheers,

Tony

Tony
This is not an answer and should have been posted as a comment or an edit to the original question.
Dennis Williamson
You should accept the answer you like best (click the checkmark)
glenn jackman
For which you should your original account. I see you have two of them.
larsmans
I attempted to vote, but the system would not allow me to do so. It displayed "vote up requires 15 reputation". My apology for mis-posting my reply. I only realised afterwards.
Tony