Popular Python recipes tagged "awk"http://code.activestate.com/recipes/langs/python/tags/awk/2011-11-25T17:14:57-08:00ActiveState Code Recipesawk-like module (Python)
2011-11-25T17:14:57-08:00davidhttp://code.activestate.com/recipes/users/4179974/http://code.activestate.com/recipes/577962-awk-like-module/
<p style="color: grey">
Python
recipe 577962
by <a href="/recipes/users/4179974/">david</a>
(<a href="/recipes/tags/awk/">awk</a>).
</p>
<p>This python module is similar to awk (pattern scanning and processing language in unix/linux).
It scans the input file for lines, split each line to fields.
*._nr # AWK NR
*._nf # AWK NF
*._0r # AWK $0
*._1 # AWK strip($1)
*._1r # AWK $1 , raw string
*._1i # AWK int($1)
*._1f # AWK float($1)</p>
Line-oriented processing in Python from command line (like AWK) (Python)
2011-04-14T19:49:16-07:00Artur Siekielskihttp://code.activestate.com/recipes/users/4177664/http://code.activestate.com/recipes/577656-line-oriented-processing-in-python-from-command-li/
<p style="color: grey">
Python
recipe 577656
by <a href="/recipes/users/4177664/">Artur Siekielski</a>
(<a href="/recipes/tags/awk/">awk</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/shell/">shell</a>).
</p>
<p>A very simple but powerful shell script which enables writing ad-hoc Python scripts for processing line-oriented input. It executes the following code template:</p>
<pre class="prettyprint"><code>$INIT
for line in sys.stdin:
$LOOP
$END
</code></pre>
<p>where $INIT, $LOOP and $END code blocks are given from command line. If only one argument is given, then $INIT and $END are empty. If two arguments are given, $END is empty.</p>
<p>Examples (script is saved as 'pyk' in the $PATH):</p>
<ul>
<li>"wc -l" replacement:
$ cat file | pyk 'c=0' 'c+=1' 'print c'</li>
<li>grep replacement:
$ cat file | pyk 'import re' 'if re.search("\d+", line): print line'</li>
<li>adding all numbers:
$ seq 1 10 | pyk 's=0' 's+=int(line)' 'print s'</li>
<li>prepending lines with it's length:
$ cat file | pyk 'print len(line), line'</li>
<li>longest file name:
$ ls -1 | pyk 'longest=""' 'if len(line) > len(longest): longest=line' 'print longest'</li>
<li>number of unique words in a document:
$ pyk 'words=[]' 'words.extend(line.split())' 'print "All words: {}, unique: {}".format(len(words), len(set(words))'</li>
</ul>
Pyliner - Script to run arbitrary Python code on the command line (Python)
2011-07-29T13:22:54-07:00Drew Gulinohttp://code.activestate.com/recipes/users/4119417/http://code.activestate.com/recipes/577075-pyliner-script-to-run-arbitrary-python-code-on-the/
<p style="color: grey">
Python
recipe 577075
by <a href="/recipes/users/4119417/">Drew Gulino</a>
(<a href="/recipes/tags/awk/">awk</a>, <a href="/recipes/tags/oneliner/">oneliner</a>, <a href="/recipes/tags/perl/">perl</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sed/">sed</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>).
</p>
<p>A Python script that runs arbitrary Python scripts in an input loop. This allows one-liner Python scripts similarly to how Perl runs them (-l,-a,-n,-F, BEGIN, END)</p>
<p>It provides additional syntax that allows multiline Python scripts to be run on a single line</p>