widged.com
 

Using MS-DOS batch file

(To start awk)

Even using and the GAWK command line parameters, it can be a somewhat tedious job re-typing or editing a long GAWK command line every time you want to perform a slightly different task on a set of data, or the same task on a new set of data. Fortunately, it is possible to automate the process still further by putting the commands into an MS-DOS batch file.

A variable is used to store numbers or strings of characters. As its name suggests the value stored in a variable can vary! Variables can be given names, so that we can refer to them individually and distinguish between them.

A batch file is simply a file that contains a number of lines of text, each of which is a valid MS-DOS command or a program that can be run on the MS-DOS command line. MS-DOS performs this “batch” of commands as if they had been typed one at a time on the keyboard. To tell MS-DOS that a file is a batch file it must be given the extension .bat (e.g., printall.bat).

Using batch programs has the following advantages:

  • They speed up your work.
  • You may only have to remember one command name instead of several.
  • You can create customized commands to meet your own data processing requirements.

For example, we could save the command line

gawk -f princols.awk -v a=3 -v b=2 < a:\geochem.dat > a:\out.dat 

in a file called process.bat, which we could run simply by typing process after the MS-DOS prompt.

Try this out for yourself.

You will note that, in addition to the output (results) from the batch file, the commands contained in the batch file are themselves printed out. This may be helpful, because you have a record of both the commands issued and the resulting output in a single file, or on the screen. If, however, you want to stop this happening, put the “at” symbol (@) in front of the relevant line(s) in the batch file, e.g.,

@ gawk -f princols.awk -v a=3 -v b=2 < a:\geochem.dat > a:\out.dat

Although batch files save you a certain amount of typing, use of batch files in the way illustrated above really only helps where the command line is exactly the same every time. However, if any of the elements of the command line change (e.g., the input and output files, or the columns of data that we wish to work on), then a much better approach is to use replaceable parameters. In this case, the command line above could be entered as a:\process 2 4 a:\geochem.dat a:\out.dat where the batch file, chemproc.bat, is given in Program 6.3.

Program 6.3 chemproc.bat 
gawk -f a:\princols.awk -v a=%1 -v b=%2 < %3 > %4

The replaceable parameters in this batch file are denoted %1, %2, %3 and %4, respectively.9 In the example given, %1 through to %4 are substituted by 2, 4, geochem.dat and out.dat, respectively, when the batch file is run. Thus, for instance, MS-DOS understands that any reference to %3 in this batch program refers to the file called geochem.dat.

Replaceable parameters allow you to make your batch programs more general, rather than having to re-edit it each time you want it to work on different files etc.. Again, try this exercise yourself. You don't need to use the .bat extension when running a batch file, since MS-DOS will look for an “executable” file called process. Assuming that it can't find a file called process.exe or process.com first, it will look for and “execute” (i.e., run) a file called process.bat.

In fact MS-DOS can handle up to 10 replaceable parameters, i.e., %0 through %9, simultaneously.

 
en/computers/lg/awk/windows-batch-file.txt · Last modified: 2007/06/07 10:28 by marielle
 
RSS - Banner by widged, template © 7throot HeadQuarters, 2007