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


ajsenko
 Share

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.

Link to comment
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.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share