awk like usage in python

Tim Roberts timr at
Sat Nov 10 08:50:29 CET 2012

Rudra Banerjee <bnrj.rudra at> wrote:
>I am in process learning python.
>I basically use shell scripts for text formatting and trying my hand on
>where I am fighting is awk's functionality in python. 
>Say, one of my real tiny code looks like:
>for i in $1/MnBi_EOS_*
>#  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 ""
>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
    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
Providenza & Boekelheide, Inc.

More information about the Python-list mailing list