Esmail <ebo...@hotmail.com> wrote:
>  thanks for including the script, that really helps. Nice way of
>  finding files.

Python has lots of useful stuff like that!

>  Two quick questions:
> 
>  As a replacement for grep I would use the re module and its
>  methods?

The re module works on strings not files, but basically yes.

Note that the re module uses a superset of the "grep -E" regexps, and
they are almost identical to those used in perl and php.

Here is a simple grep in python

#!/usr/bin/python
import re
import sys
import fileinput
pattern = sys.argv.pop(1)
for line in fileinput.input():
    if re.search(pattern, line):
        print line.rstrip()

Save in a file called grep.py then you can do

$ ./grep.py dizzy *.py
        self.dizzy = 0
        if self.dizzy:

$ ls | ./grep.py '\d{2}'
linux-image-2.6.24-21-eeepc_2.6.24-21.39eeepc1_i386.deb
linux-ubuntu-modules-2.6.24-21-eeepc_2.6.24-21.30eeepc6_i386.deb

>  What about awk which I regularly use to extract fields based on position
>  but not column number, what should I be using in Python to do the
>  same?

I presume you mean something like this

   ... | awk '{print $2}'

In python, assuming you've got the line in the "line" variable, then

In python an equivalent of the above would be

import fileinput
for line in fileinput.input():
    print line.split()[1]

Note that the fileinput module is really useful for making shell
command replacements!

>  The other things I need to do consist of moving files, manipulating file
>  names and piping outputs of one command to the next, so I'm digging into
>  the documentation as much as I can.

Read up on the os module and the subprocess module.  You'll find you
need to do much less piping with python as with shell because it has
almost everything you'll need built in.

Using built in functions is much quicker than fork()-ing an external
command too.

>  So much to learn, so little time (but so much fun!)

;-)

-- 
Nick Craig-Wood <n...@craig-wood.com> -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to