[Tutor] awk like functionality in python

Ron Williams ron20018 at gmail.com
Tue Sep 13 13:27:43 EDT 2016


Hello, this is my first post. I'm glad a place like this exists. I'm coming
from primarily a shell scripting background and it was suggested I should
learn python because the two are closely related. I've been studying python
now for about a week and as of right now, I'm not really seeing many
similarities. Anyway, to learn python as fast as I can, I thought I'd just
dive in and try to write a program but I'm stuck on the first step. The
program I'm writing comes from this website:

http://www.meetup.com/NOVA-Python/events/232629865/


I am not a member of the group and have not been to any meetings yet but
here's the homework assignment I'm working on...


*1.  Find the total number of births in each year.  If you can, find the
total for both male and female births.  Plot your results. *

I've downloaded the files and put them in a directory and here's the script
I wrote to tackle the first part of the question, the total number of
births in a year:


----------------------------------------------------------------------

#!/bin/bash

FILE_DIR=/home/ronw/my_python_journey/names

for i in `ls $FILE_DIR`

do

OUTPUT=`awk -F, 'BEGIN{OFS=" ";} {sum+=$3} END {print FILENAME, sum}'
$FILE_DIR/$i | awk -F'[^0-9]*' '{print $2, $3}'`

echo $OUTPUT

done

--------------------------------------------------------------------

Question: How would I write a similar program and achieve awk like
functionality in python?


Thanks


More information about the Tutor mailing list