gawk win32 binary & single quote invalid char 
Author Message
 gawk win32 binary & single quote invalid char

> I am a newbie to gawk and have been attempting to learn on XP
> bianaries. When I run something like

> gawk '/foo/ {print $0}' dat_file

> I get the complaint "invalid character ''' in expression". Changing
> the single quotes to double resolves the problem for this small
> Why does this happen and can/should I remedy this. If so, how?

> I have been using a GNU binary 3.1.0 and another binary (3.04) from
> within UnxUtils. Because I am at such an early stage in learning gawk,
> would it matter which I use (i.e. would one tend to cause less grief?

Reading between the lines a bit suggests that you're using the GNU Win
32 port of gawk.

For a number of reasons Windows' shells do not support quoting in the
same way as is usual in UNIX, or POSIX conforming, shells.  This is a
continuing problem that can only really be improved by making Windows'
shells POSIX compliant, but don't hold your breath!

In UNIX-like and POSIX conforming shells command line interpretation in
respect of quoting and expansion of wildcards is a function of the shell
so applications like gawk don't need to do anything much other than to
accept the command line passed to it and work from there.  This is the
case with the GNUWin32 port of gawk AFAIK, so when you pass '/foo/{print
$0}' on the command line this is what gawk receives - but it isn't a
valid gawk program, hence the error message.  A UNIX-like shell would
remove the outer set of paired quotes and pass /foo/{print $0} to gawk
(among other things), which gawk would recognize as being its "program".

One solution, as you note, is to use double quotes instead (which the M$
command processor _does_ remove correctly), but this soon becomes
unwieldy when double quotes are required as a part of the gawk program
as they need to be escaped with backslashes.  For example this UNIX

    gawk '/foo/ {print "Line containing foo: "$0}' dat_file

needs to become:

    gawk "/foo/ {print \"Line containing foo: \"$0}" dat_file

to run under a Windows command processor.

A second solution is to place your awk program into a file and invoke it
from there using something like this:

    gawk -f myawkprog.awk dat_file

the contents of myawkprog.awk are never seen by or processed by the
shell, so there are no quoting problems.  This is a good solution if you
write a lot of awk programs, but no use for "one-liners".

A third solution is to use a version of gawk that is specifically
tailored to handle its own command-line processing, such as those
provided in D J Delorie's DJGPP ports[1].  These will work from the M$
command processors and correctly interpret single or double quotes and
expand wildcards used in file names.

A fourth solution is to install UNIX-like, or POSIX, shell such as bash
and then to invoke whichever version of gawk that you choose to install
alongside it.

A fifth is to install MinGW [2] (10Mb or so download IIRC) which
provides many tools and a suitable shell.

A sixth solution is to install a distribution such as Cygwin [3] that
will give you access to a huge number of UNIX-like shells and utilities
that run on the Windows OS, but the drawback is that the full Cygwin
distro is large.  It is, however, possible to install just gawk.exe and
the .dlls that it requires to work under one of the Windows command
processors, although you may still run into some quoting problems (once
again because the Cygwin ports don't interpret the command line

The "simplest" solution (until you need more facilities) might be to use
the DJGPP port as it runs "out of the box".  I used to use this on
Windows OSs until I required more facilities, when I moved to Cygwin.

Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"

[1] <url: http://www.*-*-*.com/ ; and follow the instructions there.
[2] <url: http://www.*-*-*.com/ ;
[3] <url: http://www.*-*-*.com/ ;

Sun, 01 May 2005 10:20:56 GMT  
 gawk win32 binary & single quote invalid char

> Peter S Tillier wrote on Wed, 13 Nov 2002 02:20:56 -0000:
> > Reading between the lines a bit suggests that you're using the GNU
> > 32 port of gawk.

> Correct. Sorry for not spelling that out.

No problem.



> Thanks for the follow-up. I am using Windows out of necessity, but I
> that I will be back to a dual-boot soon for these reasons. Help such
as yours
> gets me through.

A word of warning.  I forget to mention (because I had forgotten the
fact) that if you run even the DJGPP port under, say, and
your program is single quoted, then < and > are interpreted by as being redirection operators.  This means that:

    gawk 'FNR>1{ print save}; {save = $0}; END{print "End"}' myfile.txt

results in gawk a) seeing an invalid program 'FNR print save}; {save =
$0}; END{print "End"}' and b) MSDOS creating a file called 1{

Not quite what is intended!  However:

    gawk "FNR>1{ print save}; {save = $0}; END{print \"End\"}"

does work correctly.

Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"

Mon, 16 May 2005 10:27:12 GMT  
 [ 2 post ] 

 Relevant Pages 

1. replace single quote to double quote with an example

2. Gawk for win32 slower than Gawk for Dos_32?

3. single-quoted string conversion to triple-quoted string

4. Gawk 3.0.98 - |& on Win32

5. kjBuckets binary for Win32 & Python2.0

6. handling quoted fields with gawk

7. Quoting with GAWK in OS/2 cmd Shell

8. How to print out single quoted columns?

9. Substituting single quotes from Perl via shell using awk and gsub

10. parsing lines with quoted strings as single fields

11. awk not printing single quotes

12. How can I escape the single quote symbol?


Powered by phpBB® Forum Software