Replacing “#”, “$”, “%”, “&”, and “_” with “\#”, “\$”, “\%”, “\&”, and “\_”

前端 未结 4 1840
臣服心动
臣服心动 2020-12-31 10:02

I have a plain text document, which I want to compile inside LaTeX. However, sometimes it has the characters, \"#\", \"$\", \"%\", \"&\", and \"_\". To compile properly

相关标签:
4条回答
  • 2020-12-31 10:29
    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.

    0 讨论(0)
  • 2020-12-31 10:34

    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).

    0 讨论(0)
  • 2020-12-31 10:38

    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.

    0 讨论(0)
  • 2020-12-31 10:43

    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.
    0 讨论(0)
提交回复
热议问题