Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want an AWK that implements '@include'

Tags:

windows

awk

In the gawk manual I have it describes an @include operator. This lets you make .awk modules and share scripts, etc. Include files.

So far the latest cgywin, mingw and gnu awk-s (awk, gawk, mawk, nawk, ...) I've found for Windows fail on the first: @include statement.

Can someone point me to a version that implements the whole nawk or awk implementation for Windows? I am pretty sure there was at least one nawk, back in the day, that we had downloaded to run Windows NT.

Alternately a work around would be helpful in the meantime. (Say a pre-tested awk includer program??)

tia, Will

like image 303
will Avatar asked Apr 20 '13 00:04

will


3 Answers

As far as I can tell, @include files are not supported natively by gawk until 4.x. However, at least on Linux, it ships with a shell script called igawk that you can use instead; it preprocesses awk scripts and replaces @includes with the contents of the included file (found via $AWKPATH). Perhaps Cygwin also has this script?

$ cat foo.awk
@include bar.awk
$ cat bar.awk
BEGIN {print "Hello, world!" }
$ awk -f foo.awk
awk: foo.awk:1: @include bar.awk
awk: foo.awk:1: ^ invalid char '@' in expression
$ gawk -f foo.awk
gawk: foo.awk:1: @include bar.awk
gawk: foo.awk:1: ^ invalid char '@' in expression
$ igawk -f foo.awk
Hello, world!

Note, however, that igawk and gawk 4's @include syntaxes are not compatible. The native @include in gawk 4 requires a quoted string for the included filename; igawk's takes a bareword, and will try to find a file with quotation marks in its name if you quote it.

like image 132
Mark Reed Avatar answered Oct 17 '22 20:10

Mark Reed


You can try runawk.

RunAWK is a small wrapper for AWK interpreter that helps write standalone programs in AWK. It provides support for modules and other powerful features. It comes with dozens of modules which provide efficient means for handling command line options, powerful functions for manipulating strings and arrays as well as sorting, mathematical, tmpfile, braceexpand functions and a lot of others.

http://sourceforge.net/projects/runawk

http://awk.info/?tools/runawk

like image 45
slitvinov Avatar answered Oct 17 '22 21:10

slitvinov


If you want to stick to standard awk, do the following.

Create your library awk file, and let's call it myawklib.awk. In the library file, have your awk program statements. Do not include the BEGIN and the END block. Do not include the BODY block, unless you know what you are doing. Only have the library functions.

You can run on the command line:

awk -f myawklib.awk -f main.awk program_arguments

and

awk -f myawklib.awk -f another_main.awk program arguments

Hope this may help.

  • MD
like image 1
user4323184 Avatar answered Oct 17 '22 22:10

user4323184