Domanda

Sto usando uno script awk di fare qualche analisi ragionevolmente pesante che potrebbe essere utile ripetere in futuro, ma non sono sicuro se i miei unix-unfriendly collaboratori saranno disposti a installare awk / gawk al fine di fare il parsing. C'è un modo per creare un file eseguibile autonomo dal mio script?

È stato utile?

Soluzione

Io non sono a conoscenza di un modo per fare un binario autonomo con AWK. Tuttavia, se vi piace AWK, le probabilità sembrano buone che ti avrebbe fatto piacere Python, e ci sono diversi modi per fare un programma Python autonomo. Ad esempio, py2exe .

Ecco un rapido esempio di 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

Non è la stessa AWK, ma facile da imparare, e in realtà più potente di AWK (la lingua ha più funzioni e ci sono molti "moduli" per importare e utilizzo). Python non ha nulla implicita come il

/pattern_goes_here/ {
    # code goes here
}

funzione in AWK, ma si può semplicemente avere un if / elif / elif / catena altro con i modelli da abbinare.

Altri suggerimenti

C'è un awk.exe autonomo nel Cygwin Toolkit per quanto ne so.

Si può solo bundle che con qualunque file che si sta distribuendo ai tuoi colleghi.

Ha a essere autonomo? Si potrebbe scrivere un piccolo eseguibile che richiamerà awk con gli argomenti giusti e pipe i risultati in un file gli utenti sceglie, o su stdout -. A seconda dei casi per i vostri colleghi di lavoro

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

anche interessante alternativa, Java implementazione - http://sourceforge.net/projects/jawk/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top