This question prompted a thought I was not sure I understood well. I know that it is not possible or correct to use pipelines like cat myfile | grep -v mypattern > myfile
due to how file handles are set up. However, why can't we simply use cat myfile | grep -v mypattern| tee myfile>/dev/null
to modify the file in place? Are there any simple examples where it fails?
Specifically, does it lead to corruption or is it more of not being inplace edit, but rather overwrite?
Updating the question, I would appreciate if the answers would consider this too:
Is there problems with using cat myfile | grep -v mypattern| bash -c 'rm myfile; cat > myfile'
?
sed
orperl
's-i
options) don't actually edit the files in-place. Instead, they write to a temporary file andmv
it over the original when finished. or something very similar to that. Significantly, the temp file (and thus the original "in-place"-edited file) will have a new inode. So-i
etc are merely convenience features to save you the minor bother of doing it yourself. BTW, if you really want to keep the same inode, you could script your edits withed
. – cas May 01 '16 at 02:35