sed wildcard search replace

偶尔善良 提交于 2019-12-11 19:31:04

问题


I'm using sed on Centos, bash.

I want to replace everything between \plain and }} with a space in the below line of text:

stuff here \plain \f2\fs20\cf2 4:21-23}} more stuff over here, could be anything.

The text between \plain and }} will vary (different numbers/numbers). How can I do a wildcard to include everything between \plain and }}.

I was hoping a simple * would grab everything between the two but the wildcard in shell doesn't seem to work like this:

s/\\plain *}}/ /g;

The answer may be something incorporating this? [a-zA-Z0-9.] but that doesn't account for the backslashes, colons, and dashes in the text.


回答1:


Just add dot before * to match everything.

i.e. s/\\plain .*}}/ /g should work.




回答2:


The following regex ...

^\\plain .*}}$

... will match lines beginning with \plain, having anything in the middle, and ending with }}.

If that's no use, instead of .* to match everything, use ^ (negation) to match everything that's not }}.



来源:https://stackoverflow.com/questions/5843870/sed-wildcard-search-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!