Top-rated recipes tagged "grep"http://code.activestate.com/recipes/tags/grep/top/2014-03-05T19:47:50-08:00ActiveState Code Recipesgrep in Python (Python) 2014-03-05T19:47:50-08:00Andy Dustmanhttp://code.activestate.com/recipes/users/2435929/http://code.activestate.com/recipes/578845-grep-in-python/ <p style="color: grey"> Python recipe 578845 by <a href="/recipes/users/2435929/">Andy Dustman</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/search/">search</a>). </p> <p>The grep() function is inspired by UNIX grep, but is not limited to string patterns and files or streams.</p> Flatten XML to XPath syntax lines (Python) 2011-01-19T16:05:24-08:00Raphaël Jolivethttp://code.activestate.com/recipes/users/4135673/http://code.activestate.com/recipes/577547-flatten-xml-to-xpath-syntax-lines/ <p style="color: grey"> Python recipe 577547 by <a href="/recipes/users/4135673/">Raphaël Jolivet</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/sed/">sed</a>, <a href="/recipes/tags/xml/">xml</a>, <a href="/recipes/tags/xpath/">xpath</a>). </p> <p>This script acts like <a href="http://www.ofb.net/~egnor/xml2/">xml2</a>. It transforms a XML file into a flat text output, with <em>XPath</em>-like syntax, one line per XML node or attribute. This format is more suitable for working with standard unix CLI utils (sed, grep, ... etc).</p> find + grep (Bash) 2013-09-17T08:48:07-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578661-find-grep/ <p style="color: grey"> Bash recipe 578661 by <a href="/recipes/users/4184815/">yota</a> (<a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/zsh/">zsh</a>). Revision 2. </p> <p>look for a text pattern in files defined by a pattern. First argument is passed to the find command, second one to the grep</p> <p>./find_n_grep.sh '*.py' 'dict'</p> <p>more over, it sort by date.</p> Cheap-date trick; a different way to parse (Python) 2012-03-06T14:08:10-08:00Scott S-Allenhttp://code.activestate.com/recipes/users/4181178/http://code.activestate.com/recipes/578064-cheap-date-trick-a-different-way-to-parse/ <p style="color: grey"> Python recipe 578064 by <a href="/recipes/users/4181178/">Scott S-Allen</a> (<a href="/recipes/tags/cheap/">cheap</a>, <a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/sharp/">sharp</a>). </p> <p>... a light meal with a heavy dose of "tutorial mash" on the side.</p> <p>In the constructive spirit of "more ways to solve a problem"; this is a portion of my lateral, occasionally oblique, solutions. Nothing new in le régime de grande, but hopefully the conceptual essence will amuse.</p> <p>Initially started as a response to <a href="http://code.activestate.com/recipes/577135/">recipe 577135</a> which parses incremental date fragments and preserves micro-seconds where available. That script does more work than this, for sure, but requires special flow-control and iterates a potentially incumbering shopping list (multi-dimensional with some detail).</p> <p>So here's a different box for others to play with. Upside-down in a sense, it doesn't hunt for anything but a numerical "pulse"; sequences of digits punctuated by other 'stuff' we don't much care about.</p> <p>Missing a lot of things, intentionally, this snippet provides several examples demoin' flexibility. Easy to button-up, redecorate and extend later for show, till then the delightful commentary makes it hard enough to see bones already -- all six lines or so!</p> <p><strong>Note:</strong> <em>The core script is repeated for illustrative purposes. The first is step-by-step, the second is lean and condensed for utilitarian purposes. It is the second, shorter, version that I yanked from a file and gussied up.</em></p> uniform matcher( "re pattern" / re / func / dict / list / tuple / set ) (Python) 2009-05-06T06:17:16-07:00denishttp://code.activestate.com/recipes/users/4168005/http://code.activestate.com/recipes/576741-uniform-matcher-re-pattern-re-func-dict-list-tuple/ <p style="color: grey"> Python recipe 576741 by <a href="/recipes/users/4168005/">denis</a> (<a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/re/">re</a>, <a href="/recipes/tags/text_processing/">text_processing</a>, <a href="/recipes/tags/uniform/">uniform</a>). Revision 2. </p> <p>matcher() makes a string matcher function from any of:</p> <ul> <li>"RE pattern string"</li> <li>re.compile()</li> <li>a function, i.e. callable</li> <li>a dict / list / tuple / set / container</li> </ul> <p>This uniformity is simple, useful, a Good Thing.</p> <p>A few example functions using matchers are here too: grep getfields kwgrep.</p> Access grep from python (Python) 2010-02-24T08:30:25-08:00Daniel Cohnhttp://code.activestate.com/recipes/users/4172918/http://code.activestate.com/recipes/577069-access-grep-from-python/ <p style="color: grey"> Python recipe 577069 by <a href="/recipes/users/4172918/">Daniel Cohn</a> (<a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/linux/">linux</a>). </p> <p>The recipe below passes a filename and an argument to grep, returning the stdout and stderr. Each line in the stdout will have its line number prepended.</p>