I need to replace all occurrences and write back to the same file
I tried:
sed 's/one/two/g' file.txt
this print out but does not write to file
I also tried
sed 's/one/two/g' file.txt>file.txt
This results in empty file
sed -i 's/one/two/g' file.txt
gives error: sed: 1: "file.txt": invalid command code f
Any idea?
For Mac: Use -i.bak to add a backup file
sed -i.bak 's/one/two/g' file.txt
Or
sed -i '' 's/one/two/g' file.txt
For Linux:
Use sed -i on to do infile substitution
sed -i 's/one/two/g' file.txt
You can also do something like below using a tmp file:
sed 's/one/two/g' file.txt > tmp.txt && mv tmp.txt file.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With