I have a plain text document, which I want to compile inside LaTeX. However, sometimes it has the characters, \"#\", \"$\", \"%\", \"&\", and \"_\". To compile properly
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.
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).
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.
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.