07-20-2018, 10:53 AM
Finding Interesting Documents with grep
<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/finding-interesting-documents-with-grep.jpg" width="1983" height="1189" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/finding-interesting-documents-with-grep.jpg" class="ff-og-image-inserted" /></div>
<p><strong><span><span>Learn the basics of grep with this tutorial from our archives. </span></span></strong></p>
<p><span><span>The <em>grep</em> command is a very powerful way to find documents on your computer. You can use grep to see if a file contains a word or use one of many forms of regular expression to search for a pattern instead. Grep can check the file that you specify or can search an entire tree of your filesystem recursively looking for matching files.</span></span></p>
<p><span><span>One of the most basic ways to use grep is shown below, looking for the lines of a file that match a pattern. I limit the search to only text files in the current directory<em> *.txt </em>and the<em> -i </em>option makes the search case-insensitive. As you can see, the only matches for the string “this” are the capitalized string “This”.</span></span></p>
<pre>
<span><span>$ cat sample.txt</span>
<span>This is the sample file.</span>
<span>It contains a few lines of text</span>
<span>that we can use to search for things.</span>
<span>Samples of text</span>
<span>and seeking those samples</span>
<span>there can be many matches</span>
<span>but not all of them are fun</span>
<span>so start searching for samples</span>
<span>start looking for text that matches</span></span> <span><span>$ grep -i this sample.txt </span>
<span>This is the sample file.</span></span></pre>
<p><span><span>The <em>-A</em>, <em>-B</em>, and<em> -C</em> options to grep let you see a little bit more context than a single line that matched. These options let you specify the number of trailing, preceding, and both trailing and preceding lines to print, respectively. Matches are shown separated with a “—” line so you can clearly see the context for each match in the presented results. Notice that the last example using <em>-C 1</em> to grab both the preceding line and trailing line shows four results in the last match. This is because there are two matches (the middle two lines) that share the same context.</span></span></p>
<pre>
<span><span>$ grep -A 2 It sample.txt </span>
<span>It contains a few lines of text</span>
<span>that we can use to search for things.</span>
<span>Samples of text</span> <span>$ grep -C 1 -i the sample.txt </span>
<span>This is the sample file.</span>
<span>It contains a few lines of text</span>
<span>--</span>
<span>and seeking those samples</span>
<span>there can be many matches</span>
<span>but not all of them are fun</span>
<span>so start searching for samples</span></span></pre>
<p><span><span>The<em> -n</em> option can be used to show the line number that is being presented. Below I grab one line before and one line after the match and see the line numbers, too.</span></span></p>
<pre>
<span><span>$ grep -n -C 1 tha sample.txt </span>
<span>2-It contains a few lines of text</span>
<span>3:that we can use to search for things.</span>
<span>4-Samples of text</span>
<span>--</span>
<span>8-so start searching for samples</span>
<span>9:start looking for text that matches</span></span></pre>
<h3><span><span>Digging through a bunch of files</span></span></h3>
<p><span><span>You can get grep to recurse into a directory using the <em>-R</em> option. When you use this, the matching file name is shown on the output as well as the match itself. When you combine <em>-R</em> with <em>-n</em> the file name is first shown, then the line number, and then the matching line.</span></span></p>
<pre>
<span><span> $ grep -R sample .</span>
<span>./subdir/sample3.txt:another sample in a sub directory</span>
<span>./sample.txt:This is the sample file.</span>
<span>./sample.txt:and seeking those samples</span>
<span>./sample.txt:so start searching for samples</span>
<span>./sample2.txt:This is the second sample file</span> <span>$ grep -n -R sample .</span>
<span>./subdir/sample3.txt:1:another sample in a sub directory</span>
<span>...</span></span></pre>
<p><span><span>If you have some subdirectories that you don’t want searched, then the –exclude-dir can tell grep to skip over them. Notice that I have used single quotes around the sub* glob below. The difference can be seen in the last commands where I use echo to show the command itself rather than execute it. Notice that the shell has expanded the sub* into ‘subdir’ for me in the last command. If you have subdir1 and subdir2 and use the pattern sub* then your shell will likely expand that glob into the two directory names, and that will confuse grep which is expecting a single glob. If in doubt, enclose the directory to exclude in single quotes as shown in the first command below.</span></span></p>
<pre>
<span><span>$ grep -R --exclude-dir 'sub*' sample .</span>
<span>./sample.txt:This is the sample file.</span>
<span>./sample.txt:and seeking those samples</span>
<span>./sample.txt:so start searching for samples</span>
<span>./sample2.txt:This is the second sample file</span> <span>$ echo grep -R --exclude-dir 'sub*' sample .</span>
<span>grep -R --exclude-dir sub* sample .</span> <span>$ echo grep -R --exclude-dir sub* sample .</span>
<span>grep -R --exclude-dir subdir sample .</span></span></pre>
<p><span><span>Although the recursion built into grep is handy, you might like to combine the find and grep commands. It can be useful to use the find command by itself to see what files you will be executing grep on. The find command below uses regular expressions on the file names to limit the files to consider to only those with the number 2 or 3 in their name and only text files. The<em> -type f</em> limits the output to only files.</span></span></p>
<pre>
<span><span>$ find . -name '*[23]*txt' -type f</span>
<span>./subdir/sample3.txt</span>
<span>./sample2.txt</span></span></pre>
<p><span><span>You then tell find to execute a command for each file that is found instead of just printing the file name using the <em>-exec</em> option to find. It is convenient to use the<em> -H</em> option to grep to print the filename for each match. You may recall that grep will give you <em>-H</em> by default when run on many files. Using <em>-H</em> can be handy in case find only finds a single file; if that file matches, it is good to know what the file name is as well as the matches.</span></span></p>
<pre>
<span><span>$ find . -name '*[23]*txt' -type f -exec grep -H sampl {} +</span></span></pre>
<p><span><span>For dealing with common file types, like source code, it might be convenient to use a bash alias such as the one below to “Recursively Grep SRC code”. The search is limited to C/C++ source using file name matching. Many possible extensions are chained together using the -o argument to find meaning “OR”. The “$1” argument passed to the grep command takes the first argument to RGSRC and passes it to grep. The last command searches for the string “Ferris” in any C/C++ source code in the current directory or any subdirectory.</span></span></p>
<pre>
<span><span>$ cat ~/.bashrc</span>
<span>...</span>
<span>RGSRC() {</span>
<span> find . \( -name "*.hh" -o -name "*.cpp" -o -name "*.hpp" -o -name "*.h" -o -name "*.c" \) \</span>
<span> -exec grep -H "$1" {} +</span>
<span>}</span>
<span>...</span> <span>$ RGSRC Ferris</span>
<span>...</span>
<span>./Ferris.cpp:using namespace Ferris::RDFCore;</span>
<span>...</span></span></pre>
<h3><span><span>Regular Expressions</span></span></h3>
<p><span><span>While I have been searching for a single word using grep in the above, you can define what you want using regular expressions. There is support in grep for basic, extended, and Perl compatible regular expressions. Basic regular expressions are the default.</span></span></p>
<p><span><span>Regular expressions let you define a pattern for what you are after. For example, the regular expression ‘[Ss]imple’ will match the strings ‘simple’ and ‘Simple’. This is different from using -i to perform a case-insensitive search, because ‘sImple’ will not be considered a match for the above regular expression. Each character inside the square brackets can match, and only one of ‘S’ or ‘s’ is allowed before the remaining string ‘imple’. You can have many characters inside the square brackets and also define the an inversion. For example, [^F]oo will match any character than ‘F’ followed by two lower case ‘o’ characters. If you want to find the ‘[‘ character you have to escape it’s special meaning by preceding it with a backslash.</span></span></p>
<p><span><span>To match any character use the full stop. If you follow a character or square bracketed match with ‘*’ it will match zero or more times. To match one or more use ‘+’ instead. So ‘[B]*ar’ will match ‘ar’, ‘Bar’, ‘BBar’, ‘BBBar’, and so on. You can also use {n} to match n times and {n,m} to match at least n times but no more than m times. To use the ‘+’ and {n,m} modifiers you will have to enable extended regular expressions using the <em>-E</em> option.</span></span></p>
<p><span><span>These are some of the more fundamental parts of a regular expression, there are more and you can defined some very sophisticated patterns to find exactly what you are after. The first command below will find sek, seek, seeek in the sample file. The second command will find the strings ‘many’ or ‘matches’ in the file.</span></span></p>
<pre>
<span><span>$ grep -E 's[e]{1,3}k' sample.txt </span>
<span>and seeking those samples</span> <span>$ grep -E 'ma(ny|tches)' sample.txt </span>
<span>there can be many matches</span>
<span>start looking for text that matches</span></span></pre>
<h3><span><span>Looking across lines</span></span></h3>
<p><span><span>The grep command works on a line-by-line basis. This means that if you are looking for two words together, then you will have some trouble matching one word at the end of one line and the second word at the start of the next line. So finding the person ‘John Doe’ will work unless the Doe happens to be the first word of the next line.</span></span></p>
<p><span><span>Although there are other tools, such as awk and Perl, that will allow you to search over multiple lines, you might like to use <em>pcregrep</em> to get the job done. On Fedora, you will have to install the <em>pcre-tools</em> package.</span></span></p>
<p><span><span>The below command will find the string ‘text that’ with the words separated by any amount of whitespace. In this case, whitespace also includes the newline.</span></span></p>
<pre>
<span><span>$ pcregrep -M 'text[\s]*that' sample.txt</span>
<span>It contains a few lines of text</span>
<span>that we can use to search for things.</span>
<span>start looking for text that matches</span></span></pre>
<h3><span><span>A few other things</span></span></h3>
<p><span><span>Another grep option that might be handy is <em>-m</em>, which limits the number of matches sought in a file. The<em> -v </em>will invert the matches, so you see only the lines which do </span><span>not</span><span> match the pattern you gave. An example of an inverted match is shown below.</span></span></p>
<pre>
<span><span>$ grep -vi sampl sample.txt </span>
<span>It contains a few lines of text</span>
<span>that we can use to search for things.</span>
<span>there can be many matches</span>
<span>but not all of them are fun</span>
<span>start looking for text that matches</span></span></pre>
<h3><span><span>Final words</span></span></h3>
<p><span><span>Using grep with either<em> -R</em> to directly inspect an area of your filesystem or in combination with a complicated find command will let you search through large amounts of text fairly quickly. You will likely find grep already installed on many machines. The pcregrep allows you to search multiple lines fairly easily. Next time, I’ll take a look at some other grep-like commands that let you search PDF documents and XML files.</span></span></p>
<p><em>Learn more about Linux through the free <a href="https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux" target="_blank">“Introduction to Linux” </a>course from The Linux Foundation and edX.</em></p>
</div>
<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/finding-interesting-documents-with-grep.jpg" width="1983" height="1189" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/finding-interesting-documents-with-grep.jpg" class="ff-og-image-inserted" /></div>
<p><strong><span><span>Learn the basics of grep with this tutorial from our archives. </span></span></strong></p>
<p><span><span>The <em>grep</em> command is a very powerful way to find documents on your computer. You can use grep to see if a file contains a word or use one of many forms of regular expression to search for a pattern instead. Grep can check the file that you specify or can search an entire tree of your filesystem recursively looking for matching files.</span></span></p>
<p><span><span>One of the most basic ways to use grep is shown below, looking for the lines of a file that match a pattern. I limit the search to only text files in the current directory<em> *.txt </em>and the<em> -i </em>option makes the search case-insensitive. As you can see, the only matches for the string “this” are the capitalized string “This”.</span></span></p>
<pre>
<span><span>$ cat sample.txt</span>
<span>This is the sample file.</span>
<span>It contains a few lines of text</span>
<span>that we can use to search for things.</span>
<span>Samples of text</span>
<span>and seeking those samples</span>
<span>there can be many matches</span>
<span>but not all of them are fun</span>
<span>so start searching for samples</span>
<span>start looking for text that matches</span></span> <span><span>$ grep -i this sample.txt </span>
<span>This is the sample file.</span></span></pre>
<p><span><span>The <em>-A</em>, <em>-B</em>, and<em> -C</em> options to grep let you see a little bit more context than a single line that matched. These options let you specify the number of trailing, preceding, and both trailing and preceding lines to print, respectively. Matches are shown separated with a “—” line so you can clearly see the context for each match in the presented results. Notice that the last example using <em>-C 1</em> to grab both the preceding line and trailing line shows four results in the last match. This is because there are two matches (the middle two lines) that share the same context.</span></span></p>
<pre>
<span><span>$ grep -A 2 It sample.txt </span>
<span>It contains a few lines of text</span>
<span>that we can use to search for things.</span>
<span>Samples of text</span> <span>$ grep -C 1 -i the sample.txt </span>
<span>This is the sample file.</span>
<span>It contains a few lines of text</span>
<span>--</span>
<span>and seeking those samples</span>
<span>there can be many matches</span>
<span>but not all of them are fun</span>
<span>so start searching for samples</span></span></pre>
<p><span><span>The<em> -n</em> option can be used to show the line number that is being presented. Below I grab one line before and one line after the match and see the line numbers, too.</span></span></p>
<pre>
<span><span>$ grep -n -C 1 tha sample.txt </span>
<span>2-It contains a few lines of text</span>
<span>3:that we can use to search for things.</span>
<span>4-Samples of text</span>
<span>--</span>
<span>8-so start searching for samples</span>
<span>9:start looking for text that matches</span></span></pre>
<h3><span><span>Digging through a bunch of files</span></span></h3>
<p><span><span>You can get grep to recurse into a directory using the <em>-R</em> option. When you use this, the matching file name is shown on the output as well as the match itself. When you combine <em>-R</em> with <em>-n</em> the file name is first shown, then the line number, and then the matching line.</span></span></p>
<pre>
<span><span> $ grep -R sample .</span>
<span>./subdir/sample3.txt:another sample in a sub directory</span>
<span>./sample.txt:This is the sample file.</span>
<span>./sample.txt:and seeking those samples</span>
<span>./sample.txt:so start searching for samples</span>
<span>./sample2.txt:This is the second sample file</span> <span>$ grep -n -R sample .</span>
<span>./subdir/sample3.txt:1:another sample in a sub directory</span>
<span>...</span></span></pre>
<p><span><span>If you have some subdirectories that you don’t want searched, then the –exclude-dir can tell grep to skip over them. Notice that I have used single quotes around the sub* glob below. The difference can be seen in the last commands where I use echo to show the command itself rather than execute it. Notice that the shell has expanded the sub* into ‘subdir’ for me in the last command. If you have subdir1 and subdir2 and use the pattern sub* then your shell will likely expand that glob into the two directory names, and that will confuse grep which is expecting a single glob. If in doubt, enclose the directory to exclude in single quotes as shown in the first command below.</span></span></p>
<pre>
<span><span>$ grep -R --exclude-dir 'sub*' sample .</span>
<span>./sample.txt:This is the sample file.</span>
<span>./sample.txt:and seeking those samples</span>
<span>./sample.txt:so start searching for samples</span>
<span>./sample2.txt:This is the second sample file</span> <span>$ echo grep -R --exclude-dir 'sub*' sample .</span>
<span>grep -R --exclude-dir sub* sample .</span> <span>$ echo grep -R --exclude-dir sub* sample .</span>
<span>grep -R --exclude-dir subdir sample .</span></span></pre>
<p><span><span>Although the recursion built into grep is handy, you might like to combine the find and grep commands. It can be useful to use the find command by itself to see what files you will be executing grep on. The find command below uses regular expressions on the file names to limit the files to consider to only those with the number 2 or 3 in their name and only text files. The<em> -type f</em> limits the output to only files.</span></span></p>
<pre>
<span><span>$ find . -name '*[23]*txt' -type f</span>
<span>./subdir/sample3.txt</span>
<span>./sample2.txt</span></span></pre>
<p><span><span>You then tell find to execute a command for each file that is found instead of just printing the file name using the <em>-exec</em> option to find. It is convenient to use the<em> -H</em> option to grep to print the filename for each match. You may recall that grep will give you <em>-H</em> by default when run on many files. Using <em>-H</em> can be handy in case find only finds a single file; if that file matches, it is good to know what the file name is as well as the matches.</span></span></p>
<pre>
<span><span>$ find . -name '*[23]*txt' -type f -exec grep -H sampl {} +</span></span></pre>
<p><span><span>For dealing with common file types, like source code, it might be convenient to use a bash alias such as the one below to “Recursively Grep SRC code”. The search is limited to C/C++ source using file name matching. Many possible extensions are chained together using the -o argument to find meaning “OR”. The “$1” argument passed to the grep command takes the first argument to RGSRC and passes it to grep. The last command searches for the string “Ferris” in any C/C++ source code in the current directory or any subdirectory.</span></span></p>
<pre>
<span><span>$ cat ~/.bashrc</span>
<span>...</span>
<span>RGSRC() {</span>
<span> find . \( -name "*.hh" -o -name "*.cpp" -o -name "*.hpp" -o -name "*.h" -o -name "*.c" \) \</span>
<span> -exec grep -H "$1" {} +</span>
<span>}</span>
<span>...</span> <span>$ RGSRC Ferris</span>
<span>...</span>
<span>./Ferris.cpp:using namespace Ferris::RDFCore;</span>
<span>...</span></span></pre>
<h3><span><span>Regular Expressions</span></span></h3>
<p><span><span>While I have been searching for a single word using grep in the above, you can define what you want using regular expressions. There is support in grep for basic, extended, and Perl compatible regular expressions. Basic regular expressions are the default.</span></span></p>
<p><span><span>Regular expressions let you define a pattern for what you are after. For example, the regular expression ‘[Ss]imple’ will match the strings ‘simple’ and ‘Simple’. This is different from using -i to perform a case-insensitive search, because ‘sImple’ will not be considered a match for the above regular expression. Each character inside the square brackets can match, and only one of ‘S’ or ‘s’ is allowed before the remaining string ‘imple’. You can have many characters inside the square brackets and also define the an inversion. For example, [^F]oo will match any character than ‘F’ followed by two lower case ‘o’ characters. If you want to find the ‘[‘ character you have to escape it’s special meaning by preceding it with a backslash.</span></span></p>
<p><span><span>To match any character use the full stop. If you follow a character or square bracketed match with ‘*’ it will match zero or more times. To match one or more use ‘+’ instead. So ‘[B]*ar’ will match ‘ar’, ‘Bar’, ‘BBar’, ‘BBBar’, and so on. You can also use {n} to match n times and {n,m} to match at least n times but no more than m times. To use the ‘+’ and {n,m} modifiers you will have to enable extended regular expressions using the <em>-E</em> option.</span></span></p>
<p><span><span>These are some of the more fundamental parts of a regular expression, there are more and you can defined some very sophisticated patterns to find exactly what you are after. The first command below will find sek, seek, seeek in the sample file. The second command will find the strings ‘many’ or ‘matches’ in the file.</span></span></p>
<pre>
<span><span>$ grep -E 's[e]{1,3}k' sample.txt </span>
<span>and seeking those samples</span> <span>$ grep -E 'ma(ny|tches)' sample.txt </span>
<span>there can be many matches</span>
<span>start looking for text that matches</span></span></pre>
<h3><span><span>Looking across lines</span></span></h3>
<p><span><span>The grep command works on a line-by-line basis. This means that if you are looking for two words together, then you will have some trouble matching one word at the end of one line and the second word at the start of the next line. So finding the person ‘John Doe’ will work unless the Doe happens to be the first word of the next line.</span></span></p>
<p><span><span>Although there are other tools, such as awk and Perl, that will allow you to search over multiple lines, you might like to use <em>pcregrep</em> to get the job done. On Fedora, you will have to install the <em>pcre-tools</em> package.</span></span></p>
<p><span><span>The below command will find the string ‘text that’ with the words separated by any amount of whitespace. In this case, whitespace also includes the newline.</span></span></p>
<pre>
<span><span>$ pcregrep -M 'text[\s]*that' sample.txt</span>
<span>It contains a few lines of text</span>
<span>that we can use to search for things.</span>
<span>start looking for text that matches</span></span></pre>
<h3><span><span>A few other things</span></span></h3>
<p><span><span>Another grep option that might be handy is <em>-m</em>, which limits the number of matches sought in a file. The<em> -v </em>will invert the matches, so you see only the lines which do </span><span>not</span><span> match the pattern you gave. An example of an inverted match is shown below.</span></span></p>
<pre>
<span><span>$ grep -vi sampl sample.txt </span>
<span>It contains a few lines of text</span>
<span>that we can use to search for things.</span>
<span>there can be many matches</span>
<span>but not all of them are fun</span>
<span>start looking for text that matches</span></span></pre>
<h3><span><span>Final words</span></span></h3>
<p><span><span>Using grep with either<em> -R</em> to directly inspect an area of your filesystem or in combination with a complicated find command will let you search through large amounts of text fairly quickly. You will likely find grep already installed on many machines. The pcregrep allows you to search multiple lines fairly easily. Next time, I’ll take a look at some other grep-like commands that let you search PDF documents and XML files.</span></span></p>
<p><em>Learn more about Linux through the free <a href="https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux" target="_blank">“Introduction to Linux” </a>course from The Linux Foundation and edX.</em></p>
</div>