Previous: Empty Files, Up: Data File Management


10.3.5 Treating Assignments as File Names

Occasionally, you might not want awk to process command-line variable assignments (see Assignment Options). In particular, if you have a file name that contain an ‘=’ character, awk treats the file name as an assignment, and does not process it.

Some users have suggested an additional command-line option for gawk to disable command-line assignments. However, some simple programming with a library file does the trick:

     
     # noassign.awk --- library file to avoid the need for a
     # special option that disables command-line assignments
     
     
     
     function disable_assigns(argc, argv,    i)
     {
         for (i = 1; i < argc; i++)
             if (argv[i] ~ /^[[:alpha:]_][[:alnum:]_]*=.*/)
                 argv[i] = ("./" argv[i])
     }
     
     BEGIN {
         if (No_command_assign)
             disable_assigns(ARGC, ARGV)
     }
     

You then run your program this way:

     awk -v No_command_assign=1 -f noassign.awk -f yourprog.awk *

The function works by looping through the arguments. It prepends ‘./’ to any argument that matches the form of a variable assignment, turning that argument into a file name.

The use of No_command_assign allows you to disable command-line assignments at invocation time, by giving the variable a true value. When not set, it is initially zero (i.e., false), so the command-line arguments are left alone.