Using Sed to replace a string with another string?

2019-08-08 01:17发布

I have tried this:

sed -i 's/'Twas/It certainly was/g' *.txt

any suggestions??

3条回答
萌系小妹纸
2楼-- · 2019-08-08 02:04

Try using following

grep -rl "Old string" directoryPath | xargs sed -i 's/oldString/new String/g'

Example :

grep -rl 10.113.1.115 matchdir | xargs sed -i 's/10.113.1.115/10.113.1.65/g'

查看更多
干净又极端
3楼-- · 2019-08-08 02:09

Use double quotes

sed -i "s/'Twas/It certainly was/g" *.txt
查看更多
霸刀☆藐视天下
4楼-- · 2019-08-08 02:12

The apostrophe in 'Twas is being interpreted as a close quote (by the shell, not sed), and then the subsequent single quote after /g is being interpreted as an open quote, which cheerfully gobbles all the way to the end of the script (or the command prompt, and then you get the mysterious > that means the shell thinks there's more to come). For this situation,

sed -i "s/'Twas/It certainly was/g" *.txt

should work; however, shell double-quoted strings do a lot of stuff you don't usually want with sed programs. If there were any regexp metacharacters at all in there I'd do instead

sed -i 's/'\''Twas/It certainly was/g' *.txt
查看更多
登录 后发表回答