2: Simple example
4: Finer Control
5: X-Y Plots
6: Contour Plots
7: Image Plots
9: Gri Commands
12: Emacs Mode
15: Gri Bugs
16: Test Suite
17: Gri in Press
11.2: Using System Tools With GriUsing system tools to manipulate your data makes sense for several reasons. First, you may be familiar with those tools already. Second, learning these tools will help you in all your work.
11.2.1: IntroductionEach of the programs listed in the sections below is available for Unix. Some (e.g. Perl and the Awk variant called Gawk) are available on other operating systems as well. Each of these tools is fully documented in Unix manpages, so here I'll just give an indication of a couple of useful techniques you might want to use.
Bear in mind that these tools can do very similar jobs. For example, Awk can do much of what Sed and Grep can do, but also a whole lot more. If you don't know Sed or Grep, I suggest you learn Awk instead. Then again, Perl can also do anything Gawk can do, and a whole lot more! (For one thing, it is easier to work with multiple files in Perl.) In fact, Perl is the most powerful of this list. If you know none of these tools, you might want to learn Perl instead of the others. But Perl is more complicated for simple work than Awk is, so the most reasonable path might be to learn both Awk and Perl.
For simple applications, you will probably want to use these tools in a piped open command, e.g.
which creates a temporary file (automatically erased when Gri finishes)
which contains the output from running the system command that preceeds
the pipe symbol (`
(Here and in all the examples of this chapter, it is assumed that the user's input file is named `MyFile'.)
For more complicated appplications, you may use the Gri `
Here a temporary file, named `tmp', has been used to store the
results of the calculation. Note that this file was specifically
cleaned up by the second `
11.2.2: GrepThe most common application of Grep is to select lines matching a pattern, or not matching a pattern. Here is how to skip all lines with the word "HEADER" in them:
Note that Gawk and Perl do this just as easily.
11.2.3: SedSed is normally used for simple changes to files. For example, if you have columnar data which are separated with comma characters instead of whitespace, you could make it Gri compatible by
Where the `
11.2.4: AwkAwk is great for one-liners. If your system lacks Awk, you can procure the GNU version, called Gawk, from the web for free. For better or worse, Gawk is not fully compatible with Awk. The good thing is that Gawk is pretty much the same on all operating systems, whereas the installed Awk may not be. I use Gawk instead of Awk, for this reason and because it is normally faster.
The main concept in Awk is of "patterns" and "actions." In the Awk syntax, actions are written in braces following patterns written with no braces. (This will become clear presently.)
Whenever a line in the data file matches the pattern, the action is done.
The default pattern is to match to every line in the file. This is done if no pattern is supplied.
The default action is to print the line, and this is done if no action is supplied.
Here are a few examples. To skip first 10 lines of a file:
Here the pattern was that `
To plot the cosine of the second column against the first column:
Here no pattern was supplied, so the action was done for every line.
Combining these two forms, then, and supplying both a pattern and an action, here is how one might print the first and eighth columns of the file `MyFile', but only for the first 10 lines of the file:
11.2.5: PerlPerl can do almost anything with your data, since it is a full programming language designed to also emulate several Unix utilities.
In perl, as in other commands, the commandline switch `
Perl also has a commandline switch `