If one enters gawk without arguments then one gets
a couple of examples. The second is obviously
UNIX-specific but the first one fails as written, as well,
under Windows XP. I assume its some sort of quoting problem;
however, in a previous version of gawk it did work ok.
Here is a demonstration of the problem using gawk 3.1.3:
C:/> gawk
[...snip...]
Examples:
gawk '{ sum += $1 }; END { print sum }' file
gawk -F: '{ print $1 }' /etc/passwd
C:\>echo 10 > file
C:\> gawk '{ sum += $1 }; END { print sum }' file
gawk: cmd. line:1: '{
gawk: cmd. line:1: ^ invalid char ''' in expression
C:\>gawk --version | findstr Awk
GNU Awk 3.1.3
Here it is working in an older version of gawk:
C:\> gawk304 '{ sum += $1 }; END { print sum }' file
10
C:\>gawk304 --version | findstr Awk
GNU Awk 3.0.4
How does one run the example from the command line, i.e.
without putting the one line program in a file, on XP?
This is really OT for this NG since we mainly deal with the awk
language, so you'd be better off asking in a Windows NG. Having said
that, I THINK I've seen people post articles here saying that you need
to use double quotes instead of single quotes when runnin gawk in Windows.
Ed.
You are correct that double quotes work in both versions. I also tried
it with mawk and double quotes worked there too.
However, its more than just a Windows problem not related to gawk.
There is something gawk-specific going on since the single quotes
worked in my 3.0.4 version of gawk but not in my 3.1.3-2 version.
There has been some change in gawk resulting in the changed behavior.
Probably some difference in the way the versions were built - maybe
different compiler - maybe different makefile or other configuration
option. It is virtually certain that it was not due to change in GAWK
itself (as in, the actual GAWK sources).
Basically, under DOS/Windows, the handling of the command line is all done
by the application - which is good in a way because it allows applications
to overcome the primitiveness of the "OS" itself, but is bad in that things
are done differently by different application writers and by different
compiler writers.
1. Perhaps,
gawk --version
could give more information to help in these situations. Right
now it just gives a partial version number such as 3.1.3 (even
though I am apparently using 3.1.3-2) and
no indication on how it was built. In particular, I think it would
be highly desirable to be able to uniquely identify which gawk
one is using since it seems that two people can be using
the same version yet due to build differences get different
behaviors. Also, how does other software handle this problem?
Maybe an 'official' binary version?
2. Is anyone aware of a gawk executable for 3.1.3 for which
gawk '{ sum += $1 }; END { print sum }' file
(which is the first example when one issues the gawk command
without arguments) can be used as is, that is with single
quotes and all under Windows?
I have had 3.0.4 for so long that I no longer remember where
I got it but the 3.0.4 I do have does allow that example to
work out of the box.
As mentioned previously, my 3.1.3 version is the binary at
sourceforge:
http://gnuwin32.sourceforge.net/packages/gawk.htm
Mawk accepts single quotes within the double quotes:
gawk "BEGIN{print \"Hi, Mom!\"}"
mawk "BEGIN{print 'Hi, Mom!'}"
As I alluded to in my previous post, it is almost certainly the case that
it is more of a function of the build environment used to compile the
versions of mawk & gawk that you are using than any inherent difference
between mawk & gawk.