title: Replacing "#", "$", "%", "&", and "_" with "\#", "\$", "\%", "\&", and "\_"
body: <p>I have a plain text document, which I want to compile inside LaTeX. However, sometimes it has the characters, "#", "$", "%", "&amp;", and "_". To compile properly in LaTeX, I must first replace these characters with  "#", "\$", "\%", "\&amp;", and "_". I have used this line in <code>sed</code>:</p>
  
  <pre><code>sed -i 's/\#/\\\#/g' ./file.txt
  sed -i 's/\$/\\\$/g' ./file.txt
  sed -i 's/\%/\\\%/g' ./file.txt
  sed -i 's/\&amp;/\\\&amp;/g' ./file.txt
  sed -i 's/\_/\\\_/g' ./file.txt
  </code></pre>
  
  <p>Is this correct?</p>
  
  <p>Unfortunately, the file is too large to open in any GUI software, so checking if my <code>sed</code> line is correct with a text editor is difficult. I tried searching with <code>grep</code>, but the search does not work as expected (e.g. below, I searched for any lines containing "$"):</p>
  
  <pre><code>grep "\$" file.txt
  </code></pre>
  
  <ul>
  <li>What is the best way to put "\" in front of these characters?</li>
  <li>How can I use <code>grep</code> to successfully check the lines with the replacements?</li>
  </ul>