Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


As you can see, you have no real need to execute step 3 because step 2 has already printed a line. To prevent step 3 from executing, you can use the next command. The next command tells awk to skip all the remaining patterns and expressions and instead read the next input line and start from the first pattern or expression.

Change your script to use it:

#!/bin/sh
awk '
    $3 <= 75 { printf "%s\t%s\n",$0,"REORDER" ; next ; }
    $3 > 75 { print $0 ; }
' fruit_prices.txt ;

Now the execution of the script is as follows:

1.  Checks whether the value of the third column, 22, is less than 75. Because the value is less than 75, the script proceeds to step 2.
2.  Prints the input line followed by REORDER.
3.  Reads the next input line and starts over with the first pattern.

As you can see, the second comparison ($3 > 75) is never performed for this input line.

Using STDIN as Input

Recall that the basic form of an awk command is

awk 'script' files

If files, the list of files, is omitted, awk reads its input from STDIN. This enables us to use it to filter the output of other commands. For example, the command

$ ls -l

produces output formatted similar to the following:

total 64
-rw-r--r--   1 ranga   users   635 Nov 29 11:10 awkfruit.sh
-rw-r--r--   1 ranga   users   115 Nov 28 14:07 fruit_prices.txt
-rw-r--r--   1 ranga   users    80 Nov 27 13:53 fruit_prices.txt.7880
lrwxrwxrwx   1 ranga   users     8 Nov 27 19:01 nash -> nash.txt
-rw-r--r--   1 ranga   users    62 Nov 27 16:06 nash.txt
-rw-r--r--   1 ranga   users    11 Nov 29 10:38 nums.txt
lrwxrwxrwx   1 ranga   users     8 Nov 27 19:01 urls -> urls.txt
-rw-r--r--   1 ranga   users   180 Nov 27 12:34 urls.txt

You can use awk to manipulate the output of the ls -l command so that only the name of a file and its size are printed. Here, the name of the file is in field 9, and the size is in field 5. The following command prints the name of each file along with its size:

$ /bin/ls -l | awk '$1 !~ /total/ { printf "%-32s %s\n",$9,$5 ; }'

The output looks like the following:

awkfruit.sh                      635
fruit_prices.txt                 115
fruit_prices.txt.7880            80
nash                             8
nash.txt                         62
nums.txt                         11
urls                             8
urls.txt                         180

Using awk Features

You have seen some of the basics of using awk, and you’ll now look at some of the more powerful features that it provides. The main topics are

  Variables
  Flow control
  Loops

These features let you fully exploit the power of awk.

Variables

Variables in awk are similar to variables in the shell: They are words that refer to a value. The basic syntax of defining a variable is

name=value

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

fruit="peach"

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 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 assign both numeric and string values to a variable in the same script. For example, consider the following awk commands:

fruit="peach"
fruit=100

The first command assigns the value peach to the variable fruit. The second command assigns the value 100 to the variable fruit.

The value that you assign a variable can also be the value of another variable or a field. For example, the following awk commands

fruit=peach
fruity=fruit

set the value of the variables fruit and fruity to peach.

In order to set the value of a variable to one of the fields parsed by awk, you need to use the standard field access operator. For example, the following awk command

fruit=$1

sets the value of the variable fruit to the first field of the input line.

Using Numeric Expressions


You can also assign a variable the value of a numeric expression. Numeric expressions are commands used to add, subtract, multiply, and divide two numbers. Numeric expressions are constructed using the numeric operators given in Table 17.2. The numeric expressions are of the form
num1 operator num2

Here num1 and num2 can be constants, such as 1 or 2, or variable names. A numeric expression performs the action specified by operator on num1 and num2 and returns the answer. For example, the following awk commands

a=1
b=a+1

assign the value 2 to the variable b.


Table 17.2 Numeric Operators in awk

Operator Description

+ Add
- Subtract
* Multiply
/ Divide
% Modulo (Remainder)
^ Exponentiation


Previous Table of Contents Next