bash 用“\#”、“\$”、“\%”、“\&”和“\_”替换“#”、“$”、“%”、“&”和“_”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8950527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replacing "#", "$", "%", "&", and "_" with "\#", "\$", "\%", "\&", and "\_"
提问by Village
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:
我有一个纯文本文档,我想在 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
Is this correct?
这样对吗?
Unfortunately, the file is too large to open in any GUI software, so checking if my sedline 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 "$"):
不幸的是,该文件太大而无法在任何 GUI 软件中打开,因此sed很难使用文本编辑器检查我的行是否正确。我尝试用 搜索grep,但搜索没有按预期工作(例如,在下面,我搜索了任何包含“$”的行):
grep "$" file.txt
- What is the best way to put "\" in front of these characters?
- How can I use
grepto successfully check the lines with the replacements?
- 将“\”放在这些字符前面的最佳方法是什么?
- 如何使用
grep替换成功检查行?
回答by chepner
You can do the replacement with a single call to sed:
您可以通过一次调用来进行替换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.
该&在哪个单个字符用括号括起来的替换文本罢了。请注意,由于\是 LaTeX 转义字符,因此您还必须在原始文件中对其进行转义。
回答by Kevin
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 -estatements:
\在大多数情况下,您不需要第一个(搜索)字符串上的 ,只是$(它是一个特殊字符,意味着一行的结尾;其余的并不特殊)。而在替换中,你只需要两个\\,而不是三个。此外,您可以使用多个-e语句将所有内容合二为一:
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, bashis interpreting the escape on the $because it's a special character (specifically, a sigil for variables), so grepis 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 bashfrom interpreting the \('\$', or add another pair of \\: "\\\$". Presumably, that's where you're getting the\` from, but you don't need it in the sedas it's written.
您不需要双重转义任何内容(除了\\),因为它们是单引号的。在您的grep,bash正在解释 上的转义$符,因为它是一个特殊字符(特别是变量的符号),因此grep只获取和搜索$,这是一个特殊字符,表示一行的结尾。您需要单引号以防止bash解释\( '\$',或者添加另一对\\: "\\\$". Presumably, that's where you're getting the\` from,但在sed编写时不需要它。
回答by wim
I think your problem is that bash itself is handling those escapes.
我认为您的问题是 bash 本身正在处理这些转义。
- 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. - $ is used for bash command substitution syntax. I guess
grep "\\$" file.txtshould do what you expect.
- 你所拥有的在我看来是正确的。但警告:它也将双重转义,例如
\#已经转义的 a 。如果这不是您想要的,您可能需要修改您的模式以检查前面是否没有 \。 - $ 用于 bash 命令替换语法。我想
grep "\\$" file.txt应该做你期望的。
回答by olibre
I do not respond for sed, the other answers are good enougth ;-)
我不回应sed,其他答案已经足够了 ;-)
You can use lessas viewer to check your huge file (or more, but lessis more comfortable than more).
您可以less用作查看器来检查您的大文件(或more,但less比 更舒适more)。
For searching, you can use fgrep: it ignores regular expression => fgrep '\$'will really search for text \$. fgrepis the same as invoking grep -F.
对于搜索,您可以使用fgrep:它忽略正则表达式 =>fgrep '\$'将真正搜索文本\$。fgrep与调用相同grep -F。
EDIT:
fgrep '\$'and fgrep "\$"are different. In the second case, bashinterprets the string and will replace it by a single character: $(i.e. fgrepwill search for $only).
编辑:
fgrep '\$'和fgrep "\$"是不同的。在第二种情况下,bash解释字符串并将其替换为单个字符:($即仅fgrep搜索$)。

