开发者

How can I say this in sed?

开发者 https://www.devze.com 2023-01-14 15:40 出处:网络
I have text text text [text text] \\n text text text [text text] \\n I want text text text \\n text text text开发者_开发知识库 \\n

I have

text text text [text text] \n
text text text [text text] \n

I want

text text text \n
text text text开发者_开发知识库 \n

What RE can I use?


The following works regardless of whether the \n is the end of line or a backslash and a letter 'n':

sed 's/\[.*] //' $file

The pattern looks for an open square bracket followed by any sequence of characters up to the last close square bracket and a following space (if there are several). You could refine the '.*' into '[^]]*' to match anything that is not a close square bracket; this only deletes to the first close square bracket.


/[^\[]*/ gets you most of the way. If nothing ever appears after [...], then it is sufficient.


you can use awk

$ more file
text [dont want text here ] text text [ dont want text] \n
text text text [ dont want text] \n

$ awk -vRS=']'  '/\[/{gsub(/\[.*/,"")}1' ORS="" file
text  text text  \n
text text text  \n
0

精彩评论

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