how to replace all occurrences in 1 file with sed

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/88854335

For Mac: Use -i.bak to add a backup file

sed -i.bak 's/one/two/g' file.txt

generate file.bak file, this is the recommended way.

Or

sed -i '' 's/one/two/g' file.txt

note the '' after -i

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

 when replace in all same type of flies, replace file.txt to *.txt, it works in my Mac

references:

1. https://stackoverflow.com/questions/22122805/how-to-replace-all-occurrences-in-1-file-with-sed

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/88854335