Using awk Features

So far you have learned about the basics of using awk, now you will look at some of its more powerful features:

  • Variables

  • Flow control

  • Loops


Variables in awk are similar to variables in the shell; they are words that hold a value. The basic syntax of defining a variable is


Here name is the name of the variable and value is the value of that variable. For example, the following awk command


creates the variable fruit and assigns it the value peach. There is no need to initialize a variable; the first time you use it, it is automatically initialized.

Like the shell, the name of a variable can contain only letters, numbers, and underscores. A variable's name cannot start with a number.

You can ...

Get Sams Teach Yourself Shell Programming in 24 Hours, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.