Selecting first nth rows by groups using AWK?

The key to this awk program is to pass the input file in twice: Once to count how many rows you want and once to print them.

The key to this awk program is to pass the input file in twice: Once to count how many rows you want and once to print them. Awk ' NR == FNR {wanted_rows$2 += 3*$4; next} --wanted_rows$2 >= 0 {print} ' input_file. Txt input_file.txt.

Dear Glenn- Wow, I just test your programme , it works fine. I have a much larger file to test about 1.8M rows and will keep you posted. - Thank you so much - BTW, This is the first time I find the same input file is passed twice.

Never thought about this idea – Tony Dec 2 '10 at 15:43 Both answers are valid. They both exhibit their elegance in their own right. – Tony Dec 2 '10 at 16:05.

! /usr/bin/awk -f # by Dennis Williamson - 2010-12-02 # for stackoverflow.com/questions/4334167/sele... $2 == prev { count += $4 groupcount++ arrayidx++ = $0 } $2! = prev { if (NR > 1) { for (i=0; i.

Dear Dennis- Many thanks for your effort. I just tested the programme, but it did not produce the expected output. The output has 37 lines and the rows are out of order.

Any possible modification? Cheers. Tony – Tony Dec 2 '10 at 14:07 @Tony: Using the sample data you provided, its output is exactly the same as your sample output.Do you get the correct results from the sample data?

Can you be more specific about how it's not working? – Dennis Williamson Dec 2 '10 at 15:13 Dennis- First, I got 37 rows and the first few lines, I obtained are1 1 TN1148 1 2 1 S52689 0 3 2 TA2081 1 1 1 TN1148 1 2 1 S52689 0 ... – Tony Dec 2 '10 at 15:28 Dennis- First, I got 37 rows and the first few rows are shown below: 1 1 TN1148 1 2 1 S52689 0 3 2 TA2081 1 1 1 TN1148 1 2 1 S52689 0... 16 3 TC3094 0 17 3 TD0106 0 18 3 TD1146 0 I do not know where I did wrong. I saved the programme as select.

Script and then to run it awk -f select. Script input. Txt > out.

Txt - Your help is much appreciated. – Tony Dec 2 '10 at 15:36 @Tony: So it repeats the first couple of rows? What version of AWK?

I'm using gawk 3.1.6, but it runs correctly for me even with the --traditional or --posix switches. Be sure you didn't get an extra print statement in there somehow. – Dennis Williamson Dec 2 '10 at 15:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions