2

I love Python but do not really care for AWK. For purposes of comparison (and to see how a Python-to-AWK master would do this), could someone rewrite the following Python program in AWK? Considering how short it is, some would think that the rewrite would be simple and easy for anyone with a little time.

import os

ROOT = '/Users/Zero/Documents/MyProgram.app/Contents/TempFiles'
ID = '628251 173511 223401 138276 673278 698450 629138 449040 901575'.split()

def main():
    for name in os.listdir(ROOT):
        if '.log' in name.lower():
            path = os.path.join(ROOT, name)
            if os.path.isfile(path):
                data = open(path, 'rb').read()
                for line in data.split('\r'):
                    for number in ID:
                        if number in line:
                            print line
                            break

if __name__ == '__main__':
    main()
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
  • ... This is exactly the sort of thing that I wouldn't bother converting. Ever. I'd rather port the Python VM to an exotic platform than convert it. – Ignacio Vazquez-Abrams Oct 20 '10 at 01:41
  • 2
    as simple as the conversion might be, this is not a code-4-free shop.... – Mitch Wheat Oct 20 '10 at 01:42
  • 4
    Why use awk when find and grep would do this pretty fast? (and in fewer lines than your python.) – ocodo Oct 20 '10 at 01:43
  • This looks like it was ported from Perl, and now you want it from Python to AWK?! – Matt Joiner Oct 20 '10 at 01:44
  • Honestly, I do not entirely care if it is never converted. I have a teacher that seems to really like AWK, but Python seems sufficient. If an AWK master rewrote this, then maybe the benefits would be obvious, but it does not hurt my feelings to not be convinced otherwise. – Noctis Skytower Oct 20 '10 at 01:46
  • find . -name "*.log" -exec grep -E "628251|173511|223401|138276|673278|698450|629138|449040|901575" {} \; converting that to awk is pretty obvious, albeit pointless. – ocodo Oct 20 '10 at 01:49
  • I'm no Perl fan, but it completely obsoleted awk over a decade ago. It has no use in any modern environment. If your teacher seems to really like it, then you have a teacher who's fallen into a common academic trap--teaching the tools that were in use when he learned them, ignoring everything that's happened since, and doing very real damage to their students in the process. – Glenn Maynard Oct 20 '10 at 01:56

2 Answers2

6

Why awk?

This looks like a simple grep command to me; something like:

egrep -w '628251|173511|223401|138276|673278|698450|629138|449040|901575' /Users/Zero/Documents/MyProgram.app/Contents/TempFiles/*.log*

update: or use find+grep, as suggested in some of the comments, if a recursive search is intended

David Gelhar
  • 27,873
  • 3
  • 67
  • 84
4
BEGIN{
   id="628251 173511 223401 138276 673278 698450 629138 449040 901575"
   m=split(id,ID," ")
   for(i=1;i<ARGC;i++){
       while( (getline line<ARGV[i] ) > 0 ){
           n=split(line,LINE," ")
           for ( o=1; o<=n; o++){
                for(num in ID){
                   if ( num == LINE[o] ){
                     print line
                   }
                }
           }
       }
   }
}

save as myscript.awk , then

#!/bin/bash
ROOT = "/Users/Zero/Documents/MyProgram.app/Contents/TempFiles"
cd $ROOT
awk -f myscript.awk file* #do for files that start with "file"

@OP,

For text/file processing, awk doesn't lose to Perl or Python or any others. If you (or others here thinking awk is obsoleted) are interested, go to http://awk.info. And no, awk still has its uses in the modern environment. don't let anyone tell you otherwise

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • Thanks for conversion! I guess it really is just a matter of porting from one language to another. Thanks to everyone else as well who pointed out the usage of `grep`. Being acquainted with *Nix systems and their many tools is obviously superior when working with them. – Noctis Skytower Oct 20 '10 at 02:16