Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Delete last line of file with regex

by Anonymous Monk
on Jul 23, 2020 at 13:50 UTC ( #11119706=note: print w/replies, xml ) Need Help??


in reply to Re^2: Delete last line of file with regex
in thread Delete last line of file with regex

In that case you could seek to a position close to the end-of-file, walk backward until you find a newline, use a regex to see if the last line matches, and if so, remove it. Then, truncate the file at the current position and append the modified string to it. This would work on a file of any size while using a minimal amount of memory.
  • Comment on Re^3: Delete last line of file with regex

Replies are listed 'Best First'.
Re^4: Delete last line of file with regex
by Anonymous Monk on Aug 06, 2020 at 16:40 UTC

    You've got the right idea but your algorithm is shittily inefficient. The classical (tried and true) solution is to load blocks, one at a time, starting with the last, and search backwards in the block until the marker is found. This solution involves doing some math (to calculate pos from block size and count), but it's much more efficient than using seek.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2021-05-07 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (91 votes). Check out past polls.

    Notices?