替换“#”、“$”、“%”、“&”和“_”与“\#”、“\$”、“\%”、“\&”和“\_”
我有一个纯文本文档,我想在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过一次调用
sed
来完成替换:替换文本中的
&
将填充括号中的单个字符。请注意,由于\
是 LaTeX 转义字符,因此您也必须在原始文件中对其进行转义。You can do the replacement with a single call to
sed
: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.大多数情况下,您不需要第一个(搜索)字符串上的
\
,只需$
(它是一个特殊字符,表示行尾;其余的并不特别)。并且在替换时,只需要两个\\
,而不是三个。另外,您可以使用多个-e
语句来完成所有操作:您不需要对任何内容进行双重转义(
\\
除外),因为它们是单转义的引。在您的grep
中,bash
正在解释$
上的转义符,因为它是一个特殊字符(特别是变量的符号),因此grep
正在获取并搜索$
,这是一个特殊字符,表示行尾。您需要将其用单引号引起来,以防止 bash 解释\
('\$'
,或者添加另一对\\
:"\\\$" 大概是您获取
\` 的地方,但在sed
中不需要它。 > 正如其所写。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:You don't need to double-escape anything (except the
\\
) because these are single-quoted. In yourgrep
,bash
is interpreting the escape on the$
because it's a special character (specifically, a sigil for variables), sogrep
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 preventbash
from interpreting the\
('\$'
, or add another pair of\\
:"\\\$". Presumably, that's where you're getting the
\` from, but you don't need it in thesed
as it's written.我认为你的问题是 bash 本身正在处理这些转义。
\#
。如果这不是您想要的,您可能需要修改您的模式以检查前面是否存在 \ 。grep "\\$" file.txt
应该符合您的预期。I think your problem is that bash itself is handling those escapes.
\#
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.grep "\\$" file.txt
should do what you expect.我不响应
sed
,其他答案都很好;-)您可以使用
less
作为查看器来检查您的大文件(或more
,但less
比more
更舒服)。对于搜索,您可以使用
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 (ormore
, butless
is more comfortable thanmore
).For searching, you can use
fgrep
: it ignores regular expression =>fgrep '\$'
will really search for text\$
.fgrep
is the same as invokinggrep -F
.EDIT:
fgrep '\$'
andfgrep "\$"
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).