Sed regex and substring negation

前端 未结 4 357
耶瑟儿~
耶瑟儿~ 2020-12-13 13:29

What is the correct syntax for finding a substring (a string which is preceded and followed by specific strings) which does not match a specific pattern?

4条回答
  •  时光说笑
    2020-12-13 14:20

    This might work for you:

    sed 'h;s/BEGIN_\(.*\)_END/(\1)/;/^(FOO)$/g' file
    

    This only works if there is only one string per line.

    For multiple strings per line:

    sed 's/BEGIN_\([^F][^_]*\|F[^O][^_]*\|FO[^O][^_]*\|FOO[^_]\+\)_END/\(\1\)/g' file
    

    Or the more easily understood:

    sed 's/\(BEGIN_\)FOO\(_END\)/\1\n\2/g;s/BEGIN_\([^\n_]*\)_END/(\1\)/g;s/\n/FOO/g' file
    

提交回复
热议问题