;) At the system call level this should be possible.A program can open your target file for writing without truncating it and start writing what it reads from stdin.
But you could see How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? This is a very nice thing if you're working on some rather large files.
If the files are small this may be overkill.' construct are to be edited in-place.
sorry, just one of the things that bug me like people spelling the two words "a lot" as one word "alot". I've never used sed before within a perl script. I suppose I could backtick the perl -p -e statement in the script too, but it's kind of ugly.
If you need to do this as part of a larger application then go on reading this article.
This means you should not corrupt your input with the new output.
However, finding a program that does this is the problem.
When reading EOF, the output file can be truncated.
Since you are filtering lines from the input, the output file write position should always be less than the read position.
My first take is to slurp all of the lines of the file into an array, edit the lines one by one, and then write out the array back to the file.
It seems to me that there should be a way to edit the file in place. You've reinvented one of the most common questions asked here.
And you've already implemented one of the most common solutions.