1

我正在使用 awk 脚本进行一些相当繁重的解析,这些解析可能在未来重复使用,但我不确定我对 unix 不友好的同事是否愿意安装 awk/gawk 来进行解析. 有没有办法从我的脚本创建一个独立的可执行文件?

4

4 回答 4

1

据我所知,Cygwin Toolkit 中有一个独立的 awk.exe。

您可以将其与您分发给同事的任何文件捆绑在一起。

于 2009-04-20T15:37:46.250 回答
1

我不知道使用 AWK 制作自包含二进制文件的方法。但是,如果您喜欢 AWK,那么您可能会喜欢 Python,并且有多种方法可以制作自包含的 Python 程序。例如,Py2Exe

这是 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

与 AWK 不同,但易于学习,实际上比 AWK 更强大(该语言具有更多功能,并且有许多“模块”可以导入和使用)。Python没有像

/pattern_goes_here/ {
    # code goes here
}

AWK 中的功能,但您可以简单地拥有一个 if/elif/elif/else 链,其中包含要匹配的模式。

于 2009-10-14T07:16:56.013 回答
0

它必须是自给自足的吗?您可以编写一个小的可执行文件,该可执行文件将使用正确的参数调用 awk 并将结果通过管道传输到用户选择的文件或标准输出 - 无论哪个适合您的同事。

于 2009-04-20T15:34:53.933 回答
0

GnuWin32 中的 MAWK — http://gnuwin32.sourceforge.net/packages/mawk.htm

另一个有趣的替代方案,Java 实现 — http://sourceforge.net/projects/jawk/

于 2009-04-20T15:55:40.793 回答