私はawkスクリプトを使用して、将来繰り返すのに役立つ可能性のあるかなり重い解析を行っていますが、UNIXに不慣れな同僚が解析を行うためにawk/gawkをインストールするかどうかはわかりません. スクリプトから自己完結型の実行可能ファイルを作成する方法はありますか?
4 に答える
私の知る限り、Cygwin Toolkit にはスタンドアロンの awk.exe があります。
同僚に配布するファイルにそれをバンドルすることができます。
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 チェーンを持つことができます。
それは自己完結型でなければなりませんか?適切な引数で awk を呼び出し、結果をユーザーが選択したファイルまたは stdout (同僚に適した方) にパイプする小さな実行可能ファイルを作成できます。
GnuWin32 の MAWK — http://gnuwin32.sourceforge.net/packages/mawk.htm
また、興味深い代替手段である Java 実装 — http://sourceforge.net/projects/jawk/