views:

361

answers:

2

Looking out for a shell script which searches for an attribute (a string) in all the files in current directory and stores the attribute values along with the file name.

e.g File1.txt

abc xyz = "pqr"

File2.txt

abc xyz = "klm"

Here File1 and File2 contains desired string "abc xyz" and have values "pqr" and "klm". I want result something like this: File1.txt:pqr File2.txt:klm

A: 

Please don't use bash scripting for this.

There is much room for small improvements in the code,
but in 20 lines the damn thing does the job.

Note: the code assumes that "abc xyz" is at the beginning of the line.

#!/usr/bin/python

import os
import re

MYDIR = '/dir/you/want/to/search'

def search_file(fn):
    myregex = re.compile(r'abc xyz = \"([a-z]+)\"')
    f = open(fn, 'r')
    for line in f:
        m = myregex.match(line)
        if m:
            yield m.group(1)

for filename in os.listdir(MYDIR):
    if os.path.isfile(os.path.join(MYDIR, filename)):
        matches = search_file(os.path.join(MYDIR, filename))
        for match in matches:
            print filename + ':' + match,

Thanks to David Beazley, A.M. Kuchling, and Mark Pilgrim for sharing their vast knowledge.
I couldn't have done something like this without you guys leading the way.

Adam Bernier
A: 

Well, this depends on how do you define a 'shell script'. Here are 3 one-line solutions:

  • Using grep/sed:

    egrep -o "abc xyz = ".*"' * | sed -e 's/abc xyz = "(.*)"/\1/'

  • Using awk:

    awk '/abc xyz = "(.)"/ { print FILENAME ":" gensub("abc xyz = \"(.)\"", "\1", 1) }' *

  • Using perl one-liner:

    perl -ne 'if(s/abc xyz = "(.*)"/$ARGV:$1/) { print }' *

I personally would go with the last one.

abbot
@abbot: Your answer is much better. In my work I tend to reuse and extend my scripts; so Python is ideal. But this appears to be a one-off task. Kudos on the terse one-liners. Let me know if you think I should delete my answer.
Adam Bernier
No need to delete it. However a python version could be made much more compact, check http://gist.github.com/126323
abbot
Pretty hefty compression ratio you got there! Thanks for sharing that gem.
Adam Bernier