首页 > 解决方案 > 如何在 MacOS 上使用 sed 删除包括反斜杠在内的模式?

问题描述

我正在尝试使用 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。

标签: sedbackslash

解决方案


}inside[]不需要转义,所以:

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

简单有效。

你可以看到规则re_format(7)

... 除了这些和一些使用 `[' 的组合(参见下一段)之外,所有其他特殊字符,包括 `\',在括号表达式中都失去了它们的特殊意义。


推荐阅读