You can't read from and write to the same file in a single shell command, because >
truncates the output file (i.e., in this case also the input file) before the command is executed.
The customary idiom in this case is to do something like:
... inputfile > /tmp/tmp$$ && mv /tmp/tmp$$ inputfile
In other words: output to an intermediate temporary file, and then, if the preceding command succeeded (&&
), replace the original file.
Note that there are pitfalls when you simply recreate a file with the same name as the original: if the original file was a symlink, the link is lost, if the original file had special permissions, they are lost, ...
Applied to the command in question, we get:
grep "ERROR" error | grep -v "Login errors" > /tmp/tmp$$ && mv /tmp/tmp$$ error