How to delete a line in a file?

I have a file where each line in a file is a valid JSON document.
I’m developing a command-line tool that operates over a file that contains valid JSON documents.

I’m developing a command that looks like delete {name: "foo"} It means Now I have to match the documents in a file with {name: "foo"} and delete all those lines.

I’m able to filter those lines by streaming file and decoding the line and matching with passed string but unable to delete that matched line from a file.

Without being specific, I would write the filtered content to a temp file and then replace the source file with the temp file.


Don’t try to modify a file in-place. Indeed, as @l00ker said, write the updated contents to a new file and then replace the original file with the new file.

1 Like