title: LaTeX: Search keywords in file
body: <p>Can you use LaTeX to read in a file and search for key words? How would you do this?</p>
  
  <p>I currently have code that reads a file and counts the number of lines. I was thinking that during each line read I could parse the line for specific words or tags. Can this be done?</p>
  
  <pre><code>\documentclass{book}
  
  \usepackage{ifthen}
  
  \newcounter{FileLines}
  \newboolean{RestFile}
  \newcommand{\FileLine}{}
  \newread\File
  
  \newcommand{\CountLinesInFile}[2]
  {
    \setboolean{RestFile}{true}
    \setcounter{FileLines}{0}
  
    \openin\File=#1
    \whiledo{\boolean{RestFile}}
    {
      \ReadNextLine{\File}
      \ifthenelse{\boolean{RestFile}}{
      \stepcounter{FileLines}
      }{}
    }
    \closein\File
  }
  
  \newcommand{\ReadNextLine}[1]{
    \ifthenelse{\boolean{RestFile}}{
     \read#1 to \FileLine
     \ifeof#1\setboolean{RestFile}{false}
     \else % if last line already is read, EOF appears here
     \fi
    }{}
  }
  
  \begin{document}
  
  \CountLinesInFile{textfile.txt}
  
  Lines in file: \arabic{FileLines}
  
  \end{document} 
  </code></pre>