开发者

Searching and replacing a line in linux?

How can I find and replace every instance of a {% extends "a/b/c/d" %} with another {% extends "e/f/g/h" %} in every file in a directory? Also, if anything comes after the statement on the same line I would like to keep it.

I've looked over using perl like the following:

perl -pi -e 's/find/replace/g' *.html

But I don't know per开发者_如何转开发l and don't know the best way to go about escaping all of the characters in my find/replace strings (ie {. /, ", etc).

Any easier solution?


You can use some character other than / in perl's s/find/replace/g syntax so you don't have to escape all the forward slashes in a/b/c/d:

perl -pi -e 's|{% extends "a/b/c/d" %}|{% extends "e/f/g/h" %}|g' *.html


sed -i -e 's/{% extends "\/a\/b\/c\/d"/{% extends "\/e\/f\/g\/h/g"' *.html

or

sed -i -e 's|{% extends "a/b/c/d"|{% extends "e/f/g/h"|g' *.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜