views:

356

answers:

4

I'm using an awk script to do some reasonably heavy parsing that could be useful to repeat in the future but I'm not sure if my unix-unfriendly co-workers will be willing to install awk/gawk in order to do the parsing. Is there a way to create a self-contained executable from my script?

A: 

Does it have to be self contained? You could write a small executable that will invoke awk with the right arguments and pipe the results to a file the users chooses, or to stdout - whichever is appropriate for your co-workers.

Ori Pessach
+1  A: 

Theres a standalone awk.exe in the Cygwin Toolkit as far as I know.

You could just bundle that in with whatever files you're distributing to your colleagues.

Eoin Campbell
If it's part of Cygwin, then it's not exactly standalone, is it? I'd use the one from http://unxutils.sourceforge.net/ instead.
Rob Kennedy
A: 
vartec
A: 

I'm not aware of a way to make a self-contained binary using AWK. However, if you like AWK, chances seem good that you might like Python, and there are several ways to make a self-contained Python program. For example, Py2Exe.

Here's a quick example of Python:

# comments are introduced by '#', same as AWK

import re  # make regular expressions available

import sys  # system stuff like args or stdin

# read from specified file, else read standard input
if len(sys.argv) == 2:
    f = open(sys.argv[1])
else:
    f = sys.stdin

# Compile some regular expressions to use later.
# You don't have to pre-compile, but it's more efficient.
pat0 = re.compile("regexp_pattern_goes_here")
pat1 = re.compile("some_other_regexp_here")

# for loop to read input lines.
# This assumes you want normal line separation.
# If you want lines split on some other character, you would
# have to split the input yourself (which isn't hard).
# I can't remember ever changing the line separator in my AWK code...
for line in f:
    FS = None  # default: split on whitespace
    # change FS to some other string to change field sep
    words = line.split(FS)

    if pat0.search(line):
        # handle the pat0 match case
    elif pat1.search(line):
        # handle the pat1 match case
    elif words[0].lower() == "the":
        # handle the case where the first word is "the"
    else:
        for word in words:
            # do something with words

Not the same as AWK, but easy to learn, and actually more powerful than AWK (the language has more features and there are many "modules" to import and use). Python doesn't have anything implicit like the

/pattern_goes_here/ {
    # code goes here
}

feature in AWK, but you can simply have an if/elif/elif/else chain with patterns to match.

steveha
By the way, I have written many AWK programs in the past, but these days I prefer Python. AWK wins if you just want to toss off a quick column extractor in the shell, but Python wins if you want to do anything nontrivial.
steveha