bash - 如何删除以反斜杠开头的任何字符串?

标签 bash sed

我有一个包含很多 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/

相关文章:

bash - 使用 grep 计算一个单词在文件中重复的次数

linux - 历史命令的Bash脚本重定向到Docker容器中的文件

linux - 将小时字段增加 5(UTC 到 EST 格式)

regex - sed:匹配一个词前的一组数字

mysql - Dockerfile RUN read 命令给我错误

linux - 2 while循环读取文件内容

linux - 管道 envsubst 输出到配置单元

html - sed HTML </>标签

regex - 使用grep/sed/awk提取特定字段对应的字符串

linux - 在linux中用\替换字符串