我有一个包含很多 TeX 标记的文件,如下所示:
The fish ate 20\percent more food than the bear.
The fish ate \the\number\percent more food than the bear.
我想删除 TeX 代码文本,例如 \percent
并将其替换为空格。
- 标记始终以
\
开头。 - 标记后面始终跟有空格、
{
或另一个\
。 \
绝不会在文件的其他情况下使用。
如何删除这些以 \
开头、后跟空格、{
或另一个 \
的项目的外观>?
最佳答案
您可以使用此 perl 命令:
perl -pe 's#\\[^ \\{]+# #g' file.txt
或使用 sed:
sed -E 's#\\[^ \\{]+# #g' file.txt
或者如果不支持 -E 则:
sed -r 's#\\[^ \\{]+# #g' file.txt
输出
The fish ate 20 more food than the bear.
The fish ate more food than the bear.
关于bash - 如何删除以反斜杠开头的任何字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10559821/