如何在 MacOS 上使用 sed 删除包括反斜杠在内的模式?

How to use sed to delete patterns including backslash on MacOS?

我正在尝试使用 sed 删除诸如“\subsection{sometext}”之类的模式,

所以来自:

Important astrophysical issues could be solved...

\subsection{ Introduction\good line}

\subsection{? apple $ fast burry }white

abc

我要:

Important astrophysical issues could be solved...

white

abc

我试过:

sed -E -i '' 's/\(section|subsection|subsubsection)\{([^\}]+)\}//g' test.tex

但结果是:

Important astrophysical issues could be solved...

\subsection{ Introduction\good line}

white

abc

似乎包含反斜杠的文本无法匹配。我使用 MacOS。

}里面的[]不需要转义,所以:

's/\(section|subsection|subsubsection)\{([^}]+)\}//g'

简单有效。

你可以在re_format(7)中看到规则:

... With the exception of these and some combinations using `[' (see next paragraphs), all other special characters, including `\', lose their special significance within a bracket expression.