-->
Previous | Table of Contents | Next |
The preceding gawk program will print out the text Workers for Spacely Sprockets before printing each line in the workers file. One the command line, this will look like the following (if we stored the preceding gawk program in a file named gawk.2):
gilbert:/$ gawk -f gawk.2 workers Workers for Spacely Sprockets Eric 286 555-6674 erc 8 Geisha 280 555-4221 geisha 10 Kevin 279 555-1112 kevin 2 Tom 284 555-2121 spike 12
The print "" prints a blank line.
The END statement similarly lists commands to execute after all data is read. Heres where the NR command, number of records (or lines), comes in handy, as in the following example:
BEGIN { print "Workers for Spacely Sprockets"; print "" } { print } END { print "There are ", NR, " employees left after the latest wave of layoffs." }
This example uses cleaner formatting for the END statements. It would make no difference in the output if we had instead placed the entire END command on one line. We can name this file gawk.3 and then execute the following command:
gilbert:/$ gawk -f gawk.3 workers Workers for Spacely Sprockets Eric 286 555-6674 erc 8 Geisha 280 555-4221 geisha 10 Kevin 279 555-1112 kevin 2 Tom 284 555-2121 spike 12 There are 4 employees left after the latest wave of layoffs.
This brief explanation covers gawk in the simplest terms. For example, gawk includes most of the trappings of a full programming language, including loops, variables, string operations, numeric operations, and the creation and manipulation of arrays. If youre interested in a useful programming language that can be mastered relatively quickly, we recommend further reading on gawk; our recommendations can be found in Appendix A.
We realize that many of you are potential and practicing programmers, so we spent a great deal of space on the many programming tools available with Linux. Even so, weve barely touched the surface of the Linux programming environment, for both traditional character-based programs and those running under the X Window System.
The chapter began with a discussion of the GNU C compiler, gcc, which ships with Linux. With gcc, you can create and compile C, C++, and Objective-C programs, as explained in this chapter. You learned about compiling the long way and the short way, using the make command.
The chapter then discussed programming under the X Window System and the programming libraries youll need. A freeware library called LessTif acts as a substitute for the commercial Motif programming libraries. While LessTif is clearly a work in progress, its interesting enough for us to include on the second accompanying CD-ROMand it should be interesting enough for you to look at if youre at all interested in Motif programming.
In addition, you learned about the imake command, which is used by many X Window applications for compiling on various operating systems.
The Tcl/Tk combination allows you to create Motif-like interfaces through the use of a relatively easy-to-master scripting language. We provided an example script that throws a toolbar on the screen.
Perl is a hot scripting language, made hotter by its widespread use on the Internet. But you should be able to take advantage of its many uses, even if you never go near the Internet.
In what should come as a shock to no one, Linux features yet another command from the GNU Project, gawk, which is the functional equivalent of the awk programming language. Gawk works best on structured commands, although it does have extended programming capabilities.
Previous | Table of Contents | Next |