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