Top-rated recipes tagged "grep" Code Recipesgrep in Python (Python) 2014-03-05T19:47:50-08:00Andy Dustman <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 Jolivet <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="">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:00yota <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>./ '*.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-Allen <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="">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:00denis <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 Cohn <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>