任何人都知道如何在 Windows 上制作独立的 Awk/Gawk 程序

发布于 2024-07-17 14:01:32 字数 111 浏览 5 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赴月观长安 2024-07-24 14:01:32

我不知道如何使用 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 链来匹配模式。

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.

天煞孤星 2024-07-24 14:01:32

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

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

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.

毁梦 2024-07-24 14:01:32

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

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.

滴情不沾 2024-07-24 14:01:32

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

也是有趣的替代方案,Java实现 - http://sourceforge.net/projects/jawk/

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

also interesting alternative, Java implementation — http://sourceforge.net/projects/jawk/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文