The applicable use of this is as follows. Let's say you have the following file.txt file:
1, 2, 6, 7, "p"
We want to replace "p" with 0.
sed 's/"p"/0/g' file.txt
Using the above just prints the output on the command line.
You think, why not just redirect this text back to the file as follows:
sed 's/"p"/0/g' file.txt > file.txt
Unfortunately, due to the nature of the redirection above, an empty file will simply be created.
Instead, a temporary file should be created for output, which later overwrites the original file like this:
sed 's/"p"/0/g' file.txt > tmp.txt && mv tmp.txt file.txt
Instead of making a long workaround above the sed edit in place option with -i, you can use a much simpler command:
sed -i 's/"p"/0/g' file.txt
source share