使用bash从文本文件中删除保护字符之间的字符串

使用bash从文本文件中删除保护字符之间的字符串,bash,Bash,我有一个很长的文本文件,如下所示: where the last element of :math:`\pmb{x}_i\in\mathbf{R}^{p}` is 1 and the first :math:`p-1` elements of :math:`\pmb{x_i}` and 我想用空格替换:math:和“`”之间的所有字符串。例如,上面的文本应为: where the last element of is 1 and the first elements of and 我试过

我有一个很长的文本文件,如下所示:

where the last element of :math:`\pmb{x}_i\in\mathbf{R}^{p}` is 1 and
the first :math:`p-1` elements of :math:`\pmb{x_i}` and
我想用空格替换
:math:
和“`”之间的所有字符串。例如,上面的文本应为:

where the last element of is 1 and
the first  elements of  and
我试过这个:

sed $'/end/ {r exceptions\n} ; /:math:/,/`/ {d}' input_text.text > output_text.text 
但这会删除包含保护字符串的整条线。我只想删除保护字符串之间的内容。

尝试以下操作:

sed-E的/:数学:`[^`]*`//g'

给定你的输入,作为我得到的输出

其中的最后一个元素为1,第一个元素为和


值得注意的是,这假设`字符不能在:math:标记中使用。

OP想要删除
:math:
;我想应该是
s/:math:
[^
]*
//g`我们也可以删除保护字符吗??我的意思是,也许可以使用第二个命令来去掉很多“:math:“`”left?这也假设构造不能跨越多行。另外,对于GNU sed,您需要
-r
。请编辑答案,抱歉。要替换的:math:块应位于sed命令中的第二个
/
之间;基本上,格式是
s/find/replace/g
g
是“查找行中出现的次数)。要使其处理多行:数学:4ae1e1之类的表达式建议,您可以轻松地执行一些可怕的操作,如
tr'\n''sed'.''tr'\n'
,其中
\uu
可以替换为您知道文档中不存在的字符。这不是最干净的操作方式,但如果有人拿枪指着您的头,它应该会成功的。