Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: Print last ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 9 of 9 Topic 2057 of 2378
Post > Topic >>

Re: Print last 5 lines

by Steffen Schuler <schuler.steffen@[EMAIL PROTECTED] > Nov 7, 2007 at 12:46 AM

On Wed, 07 Nov 2007 00:19:19 +0000, Steffen Schuler wrote:

> On Wed, 07 Nov 2007 00:12:08 +0000, Steffen Schuler wrote:
> 
>> Hi Minh, hello netlanders!
>> 
>> On Fri, 02 Nov 2007 10:54:19 -0700, Minh wrote:
>> 
>> <snip>
>>> How to get awk prints the last 5 lines of the file and one line before
>>> last 10 lines.
>> <snip>
>> 
>> Here an awk script which makes a single pass through the file by using
>> a ring-buffer:
>> 
>> { line[0 + n] = $0; n = f(n + 1) }
>> END {
>>   print line[n];
>>   stop = f(n)
>>   for (i = f(n + 6); i != stop; i = f(i + 1))
>>     print line[i]
>> }
>> function f(n) {
>>   return int(n % 11)
>> }
> <snip>
> 
> An even shorter form:
> 
> { line[0 + n] = $0; n = (n + 1) % 11 } END {
>   print line[n];
>   for (i = (n + 6) % 11; i != n; i = (i + 1) % 11)
>     print line[i]
> }
<snip>

Another version:

{ line[NR % 11] = $0 }
END { 
  print line[(stop = NR + 1) % 11]
  for (i = NR - 4; i != stop; ++i)
    print line[i % 11]
}

Enjoy it,

Steffen "goedel" Schuler
 




 9 Posts in Topic:
Print last 5 lines
Minh <minhcao123@[EMAI  2007-11-02 10:54:19 
Re: Print last 5 lines
Ed Morton <morton@[EMA  2007-11-02 13:03:59 
Re: Print last 5 lines
loki harfagr <loki@[EM  2007-11-05 18:29:45 
Re: Print last 5 lines
Tiago Peczenyj <tiago.  2007-11-05 20:37:36 
Re: Print last 5 lines
gazelle@[EMAIL PROTECTED]  2007-11-05 22:03:13 
Re: Print last 5 lines
loki harfagr <loki@[EM  2007-11-06 11:33:02 
Re: Print last 5 lines
Steffen Schuler <schul  2007-11-07 00:12:08 
Re: Print last 5 lines
Steffen Schuler <schul  2007-11-07 00:19:19 
Re: Print last 5 lines
Steffen Schuler <schul  2007-11-07 00:46:09 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 11:19:41 CDT 2008.