0
votes

I've looked around for awhile and found only either questions touching on the subject or providing me with an answer that does not work. Here's the question:

I'm working on an assignment for school that requires me to read in command line arguments for an awk script (which seems odd to begin with, but eh). We're using an older version of Unix and I'm running Bash. This awk only has the -f and -Fc options. Basically, I keep trying to do "awk -f awk_script arg1 arg2 arg3 arg4 arg5 arg6" but each time awk attempts to open arg1 as a file, which it isn't. An example I saw elsewhere addressing this was:

awk 'BEGIN { print "ARGV[1] = ", ARGV[1] }' foo bar

It was supposed to print "foo", but on this system I only get the output "ARGV[1] = awk: can't open foo". So, in summary, is there any way around this? Can an awk this old read command line arguments and use them for anything other than input files? The instructors notes file hinted at the above usage (of printing foo), but his program doesn't even run, so...

Any help would be greatly appreciated.

After Edit: Using SunOS 5.10 and this awk does not support the -v option, ONLY the -f and -Fc

3
It may be soemt8ing about the verions of your OS and version of awk, becuase ... "it works for me" ;-) $ awk 'BEGIN{ print "ARGV[1]=" ARGV[1]}' a b c - output ->argv[1]=a. Edit your question to include output from uname -a and awk --version. Good luck. (you may not get a response for the --version, then your awk is pretty orginal equipment ; -) - shellter
Haha I did not get a response from awk --version - WannabeCoder
given SunOS 5.1, the awk you're using is "old" awk as of ~1977. Try changing the name that you call to nawk. (new awk). The book by awk's authors 'The Awk Programming Language', is about the "new" awk, and was published in 1988 but still highly recommended. 'Effective Awk Programming' (2001) is great too and covers many of the enhancments made to gawk (gnu-awk). Good luck. - shellter
The awk he's using isn't just old, it's broken. It must not be used by anyone, ever. Sun needs to be slapped for keeping shipping it. - Ed Morton

3 Answers

0
votes

Use nawk or /usr/xpg4/bin/awk. These are newer versions of awk that support more features.

Alternatively, you can install another version of awk like mawk or GNU awk.

1
votes

You can decrement ARGC after reading arguments so that only the first(s) argument(s) is(are) considered by awk as input file(s) :

#!/bin/awk -f

BEGIN {
    for (i=ARGC; i>2; i--) {
        print ARGV[ARGC-1];
        ARGC--;
    }
}
…

Or alternatively, you can reset ARGC after having read all arguments :

#!/bin/awk -f

BEGIN {
    for (i=0; i<ARGC; i++) {
        print ARGV[ARGC-1];
    }
    ARGC=2;
}
…

Both methods will correctly process myawkscript.awk foobar foo bar … as if foobar was the only file to process (of course you can set ARGC to 3 if you want the two first arguments as files, etc.). In your particular case, it seems you don't want to process any file, so you would set ARGC to 1.

0
votes

A possible work around - maybe not acceptable - would be to use the -v option of awk.

awk -v arg1=foo 'BEGIN { print arg1; }'