For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2006 > how do i traverse through a file from the reverse??









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how do i traverse through a file from the reverse??
siddarth.correa@gmail.com

2006-03-01, 3:55 am

I have to extract text contained in between a pattern from huge text
files (excess 10 gB) but want to find to find only the last instance.
Is there anyway i can reverse iterate throught the file..
if i iterate through the file normally it takes way too long...
eg i want to find the text between "log start ref no.xyz" and "log end
ref no.xyz". and any tips on how to speed up this??

Joe User

2006-03-01, 6:56 pm

On Tue, 28 Feb 2006 23:59:08 -0800, siddarth.correa wrote:

> I have to extract text contained in between a pattern from huge text files
> (excess 10 gB) but want to find to find only the last instance. Is there
> anyway i can reverse iterate throught the file.. if i iterate through the
> file normally it takes way too long... eg i want to find the text between
> "log start ref no.xyz" and "log end ref no.xyz". and any tips on how to
> speed up this??


Look at the 'tac' filter. It is surprisingly efficient at reversing the
order of lines in a file.

tac myfile | gawk '/log end/,/log start/ {process($0);}'



--
If organized religion is the opium of the masses,
then disorganized religion is the marijuana of
the lunatic fringe.

-- Kerry Thornley in the
introduction to the 5th edition of
Principia Discordia


Ed Morton

2006-03-02, 9:55 pm

Joe User wrote:
> On Tue, 28 Feb 2006 23:59:08 -0800, siddarth.correa wrote:
>
>
>
>
> Look at the 'tac' filter. It is surprisingly efficient at reversing the
> order of lines in a file.
>
> tac myfile | gawk '/log end/,/log start/ {process($0);}'


That would still parse the whole file and print the results in reverse.
You need to add an exit and a second tac, e.g.:

tac myfile | gawk '/log end/{f=1}f;f&&/log start/{exit}' | tac

Regards,

Ed.
Joe User

2006-03-02, 9:55 pm

On Thu, 02 Mar 2006 07:53:44 -0600, Ed Morton wrote:

> Joe User wrote:
>
> That would still parse the whole file and print the results in reverse.
> You need to add an exit and a second tac, e.g.:
>
> tac myfile | gawk '/log end/{f=1}f;f&&/log start/{exit}' | tac


I generally don't take the trouble to post complete, debugged solutions
here.

That's because I think the poster will have to change anything I
post here anyway, and to avoid the homework problem.

Most of what I post is just to give an idea to the poster. If he can't
take the hint, he is probably beyond help.

--
The Christian resolution to find the world ugly
and bad has made the world ugly and bad.

-- Nietzsche

siddarth.correa@gmail.com

2006-03-03, 3:55 am

i wud like to think that im not yet beyond help!!!! not yet neway!!
thanks guys!!!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com