Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: removing lines from a file

by g0n (Priest)
on Jun 22, 2005 at 17:30 UTC ( #469108=note: print w/replies, xml ) Need Help??

in reply to removing lines from a file

...if regex... seek(LOG,0,0) || die "Seek error: $!"; ...process line... truncate(LOG,tell(LOG))

Unless I'm missing something, you've moved the filehandle position to the beginning with seek. When you subsequently truncate to size tell(LOG) it truncates it to zero size, because 0 is where you've seeked to.

Try setting the position to a variable first:

...if regex... $pos = tell(LOG) seek(LOG,0,0) || die "Seek error: $!"; ...process line... truncate(LOG,$pos)

That corrects the immediate problem, but will still leave the line in question in place. If you set a variable to the string length of the line you've matched, and


that should get rid of your matched line.

Note: there may be better ways to do this.

Update: I also strongly agree with the first line of frodo72s response, especially as you've said that the file is growing quickly. I would be inclined to use two files, and write everything that doesn't match your regex out to the second file


g0n, backpropagated monk

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://469108]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2023-11-29 15:26 GMT
Find Nodes?
    Voting Booth?

    No recent polls found