ajsenko

How to edit only a few lines of a large text file?

Recommended Posts

I'm trying to take a large file and edit only a few lines of a large text file. I know what line number they are, but when I try to edit them it seems to overwrite the entire file.

Share this post


Link to post
Share on other sites

Please post your script so I can see what you are doing.

In general, editing text files on the fly is pretty difficult to do unless the lines are fixed length.  Assuming it isn't fixed length lines, you would have to read through the file, counting the carriage returns until you got to the desired line, then write to the file.  But you can only write the exact same number of bytes that are there.  You cannot insert any new bytes.  In order to insert, you would have to read the whole file into memory and write to a new file (or essentially do the same thing in smaller chunks).

Fixed length lines are much easier, as you can calculate where you want to go and just jump there, but you still can't insert anything without segmenting the file first, likely writing to a temporary file while doing so.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now