Find pattern and append in sed

元气小坏坏 提交于 2021-01-28 12:06:42

问题


I am having difficulty to find a pattern and append line before the pattern in sed

Suppose i want to find the following pattern with sed

"stackov/er;flo.w users are great"

Note:The pattern to be search contain double quotes , /, ; and dot And then append this line just before pattern

proud sta{ckov,er member

My attempts

1.sed "|"stackov/er;flo.w users are great"|i\proud sta{ckov,er member" file

2.

sed "/stackov/er;flo.w users are great|i\proud sta{ckov,er member" file

I tried to replace the / delimiter by | and then escaping / in pattern with \ but it didn't work.

Any help appreciated


回答1:


This might work for you (GNU sed):

sed '/"stackov\/er;flo\.w users are great"/i\proud sta{ckov,er member' file



回答2:


Unless you're on Windows (in which case I don't know), you basically want the entire sed script in single quotes, not double.

sed '\|"stackov/er;flo.w users are great"|i\proud sta{ckov,er member' file

You can make it work with double quotes as well, but unless you specifically require double quotes, you should default to single quotes. Read up on the shell's quoting behavior if you want to use it in the future as well.



来源:https://stackoverflow.com/questions/18169984/find-pattern-and-append-in-sed

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