Join David D. Levine for an in-depth discussion in this video Writing an AWK program, part of AWK Essential Training.
- View Offline
The simplest way to invoke OC, is to put the entire program on the command line. For example, suppose you have a file of names called names.txt, in the format first name followed by last name. If you wanted to put these names in the opposite order, last name followed by first name, here's how you would do it with OC. Let's look at that command line again, like all UNIX commands, it starts with the command being invoked, which in this case is AWK. Then there's a space, and then there's a single quote because, some of the characters in AWK programs are special to the shell.
The part within the quotes, is the AWK program itself. In this case, a very simple program that prints the second field followed by the first. The opening brace, tells AWK that the following instructions should be applied to every line in the input. The instruction within the braces is a print command, which, as you'd expect, prints its arguments on standard output. The argument $2 specifies the second field on the line. The comma indicates that it should be separated from the following argument with a space, and the final argument $1 specifies the first field on the line.
Then theres a closing brace, indicating the end of the program, and another single quote to tell the shell we're done talking to AWK. Finally, we tell AWK to take the output from our file of names, names.txt. If you make a mistake, I have to admit that AWK is not very forgiving, and it will bail out rather than keep trying to figure out what you meant. And here I'll be using a feature of the bash shell where I can hit Ctrl+p to recall a previous command line, and then I can use Ctrl+b and Ctrl+f to step through it. Suppose that you leave off the opening brace by accident, like that.
Then you'll get the following error messages, AWK syntax error at source line one, means that it could not understand something in the first line of the program. Of course because we typed the program on the command line itself, this program has only one line. The context is, print, indicates that AWK was looking at the word print, between these arrows, when it realized it couldn't understand the program. Often, the error is not actually in that location. You should look earlier in the program for a missing parenthesis, quote, or in the case, a brace.
Extra closing brace, means that it found a closing brace without an opening brace. Which helps to point out the source of the problem here. Finally, bailing out a source line one, means that AWK is not trying to figure this program out any further and giving up, before even realizing that the program doesn't even have a second line. Debugging AWK programs, can sometimes be frustrating. The good news is that AWK is an interpreted language, meaning that you get your error right away, rather than having to compile and link your code. Also, even if you are writing a large AWK program, you can always copy and paste a small piece of it into a simple command line program to see how that piece works.
In this course, award-winning author and teacher David D. Levine shows you how to use AWK to read and write data in a variety of formats, produce reports, and automate repetitive tasks. He reviews the nuts and bolts of the language, such as field separators, pattern matching, variables, operators, expressions, and control structures; functions available for manipulating data; and integration with other programs like Excel.
- What is AWK?
- Writing an AWK program
- Working with records, fields, patterns, and actions
- Specifying field and record separators with variables
- Using built-in and user-defined variables
- Building control structures
- Formatting output
- Manipulating string data with functions
- Scripting with AWK