简体   繁体   English

sed将此模式替换为null

[英]Sed to replace this pattern with null

A string with this format 具有这种格式的字符串

aaa,bbb,ccc,ddd,pattern:[[xxxxxxxxxxxx],[yyyyyyyyyyyyyyy]],eee,fff,pattern:[[eeeeeeeeee],[fffffffff]]

How can I use sed to replace pattern: [[ ???????????? ]] 我如何使用sed替换模式: [[ ???????????? ]] [[ ???????????? ]] with null [[ ???????????? ]]为空

Example

sed s/pattern:[[ ????????? ]]//g

I tried sed s/pattern:\\[\\[\\([^]]*\\)\\]\\]//g but it does not work. 我试过sed s/pattern:\\[\\[\\([^]]*\\)\\]\\]//g但不起作用。

You can use perl as sed doesn't support non-greedy matching: 您可以使用perl因为sed不支持非贪婪匹配:

s='aaa,bbb,ccc,ddd,pattern:[[xxxxxxxxxxxx],[yyyyyyyyyyyyyyy]],eee,fff,pattern:[[eeeeeeeeee],[fffffffff]]'

perl -pe 's/pattern:\[\[.*?\]\]//g' <<< "$s"

aaa,bbb,ccc,ddd,,eee,fff,

Using this as a test file: 使用它作为测试文件:

$ cat file
aaa,bbb,ccc,ddd,pattern:[[xxxxxxxxxxxx],[yyyyyyyyyyyyyyy]],eee,fff,pattern:[[eeeeeeeeee],[fffffffff]]

Using sed: 使用sed:

$ sed 's/\]\]/\n/g; s/pattern:\[\[[^\n]*\n//g; s/\n/]]/g' file
aaa,bbb,ccc,ddd,,eee,fff,

How it works 这个怎么运作

Because sed does not have non-greedy regular expressions, we have to use a trick: 因为sed没有非贪婪的正则表达式,所以我们必须使用一个技巧:

  • s/\\]\\]/\\n/g

    This replaces all occurrences of ]] with a newline. 这会用换行符替换所有出现的]]

  • s/pattern:\\[\\[[^\\n]*\\n//g

    This replaces all occurrences of pattern:[[ followed by any non-newline characters, [^\\n]* , followed by a newline, \\n , with an empty string. 这会用空字符串替换所有出现的pattern:[[后跟任何非换行符[^\\n]* ,后跟换行\\n

  • s/\\n/]]/g

    If any newline characters, \\n , happen to remain, this changes them back to ]] . 如果有任何换行符\\n保留,这会将它们改回]]

With awk : awk

awk -v RS='pattern:\\[\\[|\\]\\]' 'NR%2' ORS= s1 file
  • RS='pattern:\\\\[\\\\[|\\\\]\\\\]' - treating pattern:[[ and ]] as complex record separator (based on regex alternation group ...|... ) RS='pattern:\\\\[\\\\[|\\\\]\\\\]' -将pattern:[[]]视为复杂的记录分隔符(基于正则表达式替代组...|...

The output: 输出:

aaa,bbb,ccc,ddd,,eee,fff,

If you can guarantee that it always looks has sets of square brackets like 如果可以保证它看起来总是有方括号

pattern:[[abc],[def],...,[stu],[vxy]] 

then you could do something like this: 那么您可以执行以下操作:

sed 's/pattern:\[\{2\}[^]]*\(\],\[[^]]*\)*\]\{2\}//g'

That is a pattern starting with 2 '[' followed by anything that is not a closing bracket followed by optionally '],[' and anything that is not a closing bracket and finishing with ']]' 这是一种模式,以2'['开头,后跟不是右括号的任何内容,然后是可选的'],['和不是右括号的任何内容,并以']]'结尾

Update: 更新:

With the revised requirements to support the following 3 types of pattern: 修改后的要求可以支持以下3种类型的模式:

  • pattern:[[aaa],[bbb],...,[xxx],[yyy]] 模式:[[aaa],[bbb],...,[xxx],[yyy]]
  • pattern:[[aaaaa]] 模式:[[aaaaa]]
  • pattern:[] 图案:[]

the solution is: 解决方案是:

sed 's/pattern:\[\{1,2\}[^]]*\(\],\[[^]]*\)*\]\{1,2\}//g'

This might work for you (GNU sed): 这可能对您有用(GNU sed):

sed 's/\[\[[^]]*\]\(,\[[^]]*\]\)*\]//g' file

The regexp follows the pattern (re Friedl): regexp遵循以下模式(re Friedl):

sed 's/normal(special normal)*/replacement/g' file 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM