awk like usage in python

Tim Roberts timr at probo.com
Sat Nov 10 02:50:29 EST 2012


Rudra Banerjee <bnrj.rudra at gmail.com> wrote:
>
>Friends,
>I am in process learning python.
>I basically use shell scripts for text formatting and trying my hand on
>python.
>where I am fighting is awk's functionality in python. 
>Say, one of my real tiny code looks like:
>#!/bin/bash
>TMP=store
>for i in $1/MnBi_EOS_*
>do
>#  echo $i
>  grep -A 15 "T(est)" $i/out-Dy-eos2 >$TMP
>  var_T=`awk '/T\(est\)/{printf $2}' $TMP`
>  var_s1=`awk '/s1,torque/{print $6;exit}' $TMP`
>  var_t=`awk '/s1,torque/{print $7;exit}' $TMP`
>  echo  $var_T  $var_s1  $var_t >>tfl
>#  echo ""
>#  echo ""
>done
>sort -n tfl >$2
>rm -i $TMP tfl

Well, describe your program in words.  Then, you can convert it to Python.

For every directory in the given directory whose name starts with
MnBi_EOS_:
    extract the 15 lines starting with T(est) from the file out-Dy-eos2 to
a temporary file
    extract the 2nd field from the line with T(est) in it
    extract the 6th field from the first line with "s1,torque"
    extract the 7th field from the first line with "s1,torque"
    print those three fields to a file
sort that file
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list