Ramblings

September 11, 2008

Pythonic grep sort of

Filed under: cool, python, tip, tool — michaelangela @ 4:00 pm

Looking for a way to crank through files and search for specific text within matching files. Found this and it’s very neat.

Python: find files using Unix shell-style wildcards « Muharem Hrnjadovic

The download though was a little hard to find so I just got the source tar.gz file from here:

http://ppa.launchpad.net/al-maisan/ubuntu/pool/main/s/scriptutil/

[update]In the end I tried grep which was significantly faster, just reading the output of the command in while looping through a list of files:

def fgrep(the_string,the_files):
    all_lines = []
    for the_file in the_files:
        command = 'grep "%s" "%s"' % (the_string,the_file)
        response = os.popen(command,'r')
        lines = response.readlines()
        for line in lines:
            all_lines.append(line)
    return all_lines

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: