简体   繁体   中英

How do I replace multiple text at once in a file on Linux or Mac?

So example, I have the file my.txt as below

This is my xzy.
My color is white. 

I want to replace "my" to our , and I also want to replace "xyz" to "abc" .

How can I do this using a one line command?

With sed

For GNU sed

sed -i -e 's/my/our/g' -e 's/xyz/abc/g' my.txt

In MacOS, -e option may not be available. -i option in MacOS needs an extension. Use something like this:

sed -i.bak 's/my/our/g;s/xyz/abc/g' my.txt

使用sed进行多次替换:

sed -i 's/my/our/g; s/xyz/abc/g' text.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM