有人知道如何在Windows上制作一个独立的Awk/Gawk程序吗

有人知道如何在Windows上制作一个独立的Awk/Gawk程序吗,awk,executable,gawk,Awk,Executable,Gawk,我正在使用一个awk脚本来执行一些相当繁重的解析,这可能有助于将来重复,但我不确定我的unix不友好的同事是否愿意安装awk/gawk来进行解析。有没有办法从我的脚本中创建一个自包含的可执行文件?它必须是自包含的吗?您可以编写一个小的可执行文件,使用正确的参数调用awk,并将结果通过管道传输到用户选择的文件或stdout,以适合您的同事为准。据我所知,Cygwin Toolkit中有一个独立的awk.exe 您可以将其与分发给同事的任何文件捆绑在一起。MAWK in GnuWin32- 另一个有

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

它必须是自包含的吗?您可以编写一个小的可执行文件,使用正确的参数调用awk,并将结果通过管道传输到用户选择的文件或stdout,以适合您的同事为准。

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

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

MAWK in GnuWin32-


另一个有趣的选择是Java实现-

我不知道如何使用AWK生成自包含的二进制文件。然而,如果您喜欢AWK,那么您很可能会喜欢Python,并且有几种方法可以制作一个自包含的Python程序。例如

下面是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链,其中包含要匹配的模式。

如果它是Cygwin的一部分,那么它就不是完全独立的,不是吗?顺便说一句,我以前写过很多AWK程序,但现在我更喜欢Python。如果您只想在shell中扔掉一个快速列提取器,AWK会赢,但是如果您想做任何不寻常的事情,Python会赢。将gawk.exe与脚本一起分发。