Script to search text pattern in directories

19 views
Skip to first unread message

dilawar....@gmail.com

unread,
Apr 11, 2014, 10:45:02 AM4/11/14
to wncc...@googlegroups.com
<html>
<head>
<meta name="generator" content="text2mime-markdown0.1">
<style>
code { font-family: 'Andale Mono', 'Lucida Console', 'Bitstream Vera Sans Mono', 'Courier New', monospace; }
pre { border-left: 20px solid #ddd; margin-left: 10px; padding-left: 5px; }
</style>
</head>
<body>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<style type="text/css">code{white-space: pre;}</style>
</head>
<body>
<p>Following is the code for copying pasting in bash.</p>
<pre><code> #!/bin/bash
# Find a file recursively containing a pattern
if [ $# -eq 1 ]; then
pattern=&quot;$1&quot;
glob_pattern=&quot;.*&quot;
dir_pattern=&quot;.&quot;
elif [ $# -eq 2 ]; then
pattern=&quot;$1&quot;
glob_pattern=&quot;$2&quot;
dir_pattern=&quot;.&quot;
elif [ $# -eq 3 ]; then
pattern=&quot;$1&quot;
glob_pattern=&quot;$2&quot;
dir_pattern=&quot;$3&quot;
else
echo &quot;Usage: $0 text_pattern [glob_pattern] [dir_pattern]&quot;
exit
fi
#echo &quot;Searching $dir_pattern for text $pattern in files $glob_pattern&quot;
files=`find $dir_pattern -type f -name &quot;$glob_pattern&quot;`
for f in $files; do
grep -Hnr -B 1 -A 1 &quot;$pattern&quot; $f
done</code></pre>
<p>This script takes three arguments: last two are optional.</p>
<ul>
<li>First argument is the text pattern you want to search in text files.</li>
<li>Second argument is the pattern of file you wants to filters e.g. <em>.cpp </em>.h etc. If not given, all files will be searched.</li>
<li>Third argument is the direction in which do want to do searching. If not given, current directory will be searched.</li>
</ul>
<p>Updated version of this script can be found <a href="http://github.com/dilawar/Scripts/g">here</a>.</p>
<h2 id="usage-example">Usage example</h2>
<p>Lets name is script g and save it somewhere in PATH (e.g. /usr/local/bin) after making it executable. I want to search all header files containing the word <code>kolomogorov</code>. I also know that any file containing this word is most likely to be found in <code>/usr/include</code> folder.</p>
<pre><code> $g kolmogorov *.h /usr/include </code></pre>
<p>The output will be a list of following entries.</p>
<pre><code> /usr/include/boost/graph/boykov_kolmogorov_max_flow.hpp-158- // the complete class is protected, as we want access to members in
/usr/include/boost/graph/boykov_kolmogorov_max_flow.hpp:159: // derived test-class (see test/boykov_kolmogorov_max_flow_test.cpp)
/usr/include/boost/graph/boykov_kolmogorov_max_flow.hpp-160- protected:
--
</code></pre>
<p>It tells you the file path and line no where the pattern can be found. We can be more ambitious and feed a regex to it. As you can see, we are using <code>grep</code> command to sniff the pattern so whatever regex is accepted by this command is good enough for this script.</p>
<p>This script can be used to search complicated expression in large code base. You may also like to check an application called <code>source navigator</code> (snavigator). It is a gui based application which does its job quite well. Using exuberant-ctags is also recommended but once a while there will be a situation when you would like to check if a file contains some pattern of text.</p>
</body>
</html>
</body></html>
html-markdown-alternative.html
Reply all
Reply to author
Forward
0 new messages