替换“#”、“$”、“%”、“&”和“_”与“\#”、“\$”、“\%”、“\&”和“\_”

发布于 2024-12-28 07:56:26 字数 615 浏览 5 评论 0原文

我有一个纯文本文档,我想在 LaTeX 中编译它。但是,有时它包含字符“#”、“$”、“%”、“&”和“_”。为了在 LaTeX 中正确编译,我必须首先用“#”、“\$”、“\%”、“\&”和“_”替换这些字符。我在 sed 中使用了这一行:

sed -i 's/\#/\\\#/g' ./file.txt
sed -i 's/\$/\\\$/g' ./file.txt
sed -i 's/\%/\\\%/g' ./file.txt
sed -i 's/\&/\\\&/g' ./file.txt
sed -i 's/\_/\\\_/g' ./file.txt

这是正确的吗?

不幸的是,该文件太大,无法在任何 GUI 软件中打开,因此使用文本编辑器检查我的 sed 行是否正确非常困难。我尝试使用 grep 进行搜索,但搜索未按预期工作(例如,下面,我搜索了包含“$”的任何行):

grep "\$" file.txt
  • 将“\”放在这些前面的最佳方法是什么人物?
  • 如何使用 grep 成功检查包含替换的行?

I have a plain text document, which I want to compile inside LaTeX. However, sometimes it has the characters, "#", "$", "%", "&", and "_". To compile properly in LaTeX, I must first replace these characters with "#", "\$", "\%", "\&", and "_". I have used this line in sed:

sed -i 's/\#/\\\#/g' ./file.txt
sed -i 's/\$/\\\$/g' ./file.txt
sed -i 's/\%/\\\%/g' ./file.txt
sed -i 's/\&/\\\&/g' ./file.txt
sed -i 's/\_/\\\_/g' ./file.txt

Is this correct?

Unfortunately, the file is too large to open in any GUI software, so checking if my sed line is correct with a text editor is difficult. I tried searching with grep, but the search does not work as expected (e.g. below, I searched for any lines containing "$"):

grep "\$" file.txt
  • What is the best way to put "\" in front of these characters?
  • How can I use grep to successfully check the lines with the replacements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不必了 2025-01-04 07:56:26

您可以通过一次调用 sed 来完成替换:

sed -i -E 's/([#$%&_\])/\\&/g' file.txt

替换文本中的 & 将填充括号中的单个字符。请注意,由于 \ 是 LaTeX 转义字符,因此您也必须在原始文件中对其进行转义。

You can do the replacement with a single call to sed:

sed -i -E 's/([#$%&_\])/\\&/g' file.txt

The & in the replacement text fills in for whichever single character is enclosed in parentheses. Note that since \ is the LaTeX escape character, you'll have to escape it as well in the original file.

巨坚强 2025-01-04 07:56:26
sed -i 's/\#/\\\#/g' ./file.txt
sed -i 's/\$/\\\$/g' ./file.txt
sed -i 's/\%/\\\%/g' ./file.txt
sed -i 's/\&/\\\&/g' ./file.txt
sed -i 's/\_/\\\_/g' ./file.txt

大多数情况下,您不需要第一个(搜索)字符串上的 \ ,只需 $ (它是一个特殊字符,表示行尾;其余的并不特别)。并且在替换时,只需要两个\\,而不是三个。另外,您可以使用多个 -e 语句来完成所有操作:

sed -i.bak -e 's/#/\\#/g'  \
           -e 's/\$/\\$/g' \
           -e 's/%/\\%/g'  \
           -e 's/&/\\&/g'  \
           -e 's/_/\\_/g' file.txt

您不需要对任何内容进行双重转义(\\ 除外),因为它们是单转义的引。在您的 grep 中,bash 正在解释 $ 上的转义符,因为它是一个特殊字符(特别是变量的符号),因此 grep 正在获取并搜索 $,这是一个特殊字符,表示行尾。您需要将其用单引号引起来,以防止 bash 解释 \ ('\$',或者添加另一对 \\: "\\\$" 大概是您获取\` 的地方,但在 sed 中不需要它。 > 正如其所写。

sed -i 's/\#/\\\#/g' ./file.txt
sed -i 's/\$/\\\$/g' ./file.txt
sed -i 's/\%/\\\%/g' ./file.txt
sed -i 's/\&/\\\&/g' ./file.txt
sed -i 's/\_/\\\_/g' ./file.txt

You don't need the \ on the first (search) string on most of them, just $ (it's a special character, meaning the end of a line; the rest aren't special). And in the replacement, you only need two \\, not three. Also, you could do it all in one with several -e statements:

sed -i.bak -e 's/#/\\#/g'  \
           -e 's/\$/\\$/g' \
           -e 's/%/\\%/g'  \
           -e 's/&/\\&/g'  \
           -e 's/_/\\_/g' file.txt

You don't need to double-escape anything (except the \\) because these are single-quoted. In your grep, bash is interpreting the escape on the $ because it's a special character (specifically, a sigil for variables), so grep is getting and searching for just the $, which is a special character meaning the end of a line. You need to either single-quote it to prevent bash from interpreting the \ ('\$', or add another pair of \\: "\\\$". Presumably, that's where you're getting the\` from, but you don't need it in the sed as it's written.

别想她 2025-01-04 07:56:26

我认为你的问题是 bash 本身正在处理这些转义。

  1. 你所拥有的对我来说看起来很合适。但警告:它也会双重转义,例如已经转义的 \# 。如果这不是您想要的,您可能需要修改您的模式以检查前面是否存在 \ 。
  2. $ 用于 bash 命令替换语法。我想 grep "\\$" file.txt 应该符合您的预期。

I think your problem is that bash itself is handling those escapes.

  1. What you have looks right to me. But warning: it will also doubly escape e.g. a \# that is already escaped. If that's not what you want, you might want to modify your patterns to check that there isn't a preceding \ already.
  2. $ is used for bash command substitution syntax. I guess grep "\\$" file.txt should do what you expect.
亢潮 2025-01-04 07:56:26

我不响应 sed,其他答案都很好;-)

您可以使用 less 作为查看器来检查您的大文件(或 more,但 lessmore 更舒服)。

对于搜索,您可以使用 fgrep:它会忽略正则表达式 => fgrep '\$' 将真正搜索文本 \$fgrep 与调用 grep -F 相同。

编辑:
fgrep '\$'fgrep "\$" 是不同的。在第二种情况下,bash 解释该字符串并将其替换为单个字符:$(即 fgrep 将搜索 $仅限)。

I do not respond for sed, the other answers are good enougth ;-)

You can use less as viewer to check your huge file (or more, but less is more comfortable than more).

For searching, you can use fgrep: it ignores regular expression => fgrep '\$' will really search for text \$. fgrep is the same as invoking grep -F.

EDIT:
fgrep '\$' and fgrep "\$" are different. In the second case, bash interprets the string and will replace it by a single character: $ (i.e. fgrep will search for $ only).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文