Previous Section  < Day Day Up >  Next Section

6.24. Review

Unless otherwise noted, the examples in this section use the following datafile, repeated periodically for your convenience.

% cat datafile

northwest

NW

Joel Craig

3.0

.98

3

4

western

WE

Sharon Kelly

5.3

.97

5

23

southwest

SW

Chris Foster

2.7

.8

2

18

southern

SO

May Chin

5.1

.95

4

15

southeast

SE

Derek Johnson

4.0

.7

4

17

eastern

EA

Susan Beal

4.4

.84

5

20

northeast

NE

TJ Nichols

5.1

.94

3

13

north

NO

Val Shultz

4.5

.89

5

9

central

CT

Sheri Watson

5.7

.94

5

13


Example 6.161.

% nawk '{if ( $8 > 15 ){ print $3 " has a high rating"}\

  else print $3 "---NOT A COMPETITOR---"}' datafile



Joel---NOT A COMPETITOR---

Sharon has a high rating

Chris has a high rating

May---NOT A COMPETITOR---

Derek has a high rating

Susan has a high rating

TJ---NOT A COMPETITOR---

Val---NOT A COMPETITOR---

Sheri---NOT A COMPETITOR---


EXPLANATION

The if statement is an action statement. If there is more than one statement following the expression, it must be enclosed in curly braces. (Curly braces are not required in this example, because there is only one statement following the expression.) The expression reads: if the eighth field is greater than 15, print the third field and the string has a high rating; else print the third field and – – –NOT A COMPETITOR– – –.

Example 6.162.

% nawk '{i=1; while(i<=NF && NR < 2){print $i; i++}}' datafile

northwest

NW

Joel

Craig

3.0

.98

3

4


EXPLANATION

The user-defined variable i is assigned 1. The while loop is entered and the expression tested. If the expression evaluates true, the print statement is executed; the value of the ith field is printed. The value of i is printed, next the value is incremented by 1, and the loop is reentered. The loop expression will become false when the value of i is greater than NF and the value of NR is 2 or more. The variable i will not be reinitialized until the next record is entered.

% cat datafile

northwest

NW

Joel Craig

3.0

.98

3

4

western

WE

Sharon Kelly

5.3

.97

5

23

southwest

SW

Chris Foster

2.7

.8

2

18

southern

SO

May Chin

5.1

.95

4

15

southeast

SE

Derek Johnson

4.0

.7

4

17

eastern

EA

Susan Beal

4.4

.84

5

20

northeast

NE

TJ Nichols

5.1

.94

3

13

north

NO

Val Shultz

4.5

.89

5

9

central

CT

Sheri Watson

5.7

.94

5

13


Example 6.163.

% nawk '{ for( i=3 ; i <= NF && NR == 3 ; i++ ){ print $i }}' datafile

Chris

Foster

2.7

.8

2

18


EXPLANATION

This is similar to the while loop in functionality. The initialization, test, and loop control statements are all in one expression. The value of i (i = 3) is initialized once for the current record. The expression is then tested. If i is less than or equal to NF , and NR is equal to 3, the print block is executed. After the value of the ith field is printed, control is returned to the loop expression. The value of i is incremented and the test is repeated.

Example 6.164.

(The Command Line)

% cat nawk.sc4

# Awk script illustrating arrays

BEGIN{OFS="\t"}

{ list[NR] = $1 }   # The array is called list. The index is

                    # the number of the current record. The value of the

                    # first field is assigned to the array element.

END{ for( n = 1; n <= NR; n++){

         print list[n]}   # for loop is used to loop through the array.

}

(The Command Line)

% nawk -f nawk.sc4 datafile

northwest

western

southwest

southern

southeast

eastern

northeast

north

central


EXPLANATION

The array, list, uses NR as an index value. Each time a line of input is processed, the first field is assigned to the list array. In the END block, the for loop iterates through each element of the array.

Example 6.165.

(The Command Line)

% cat nawk.sc5

# Awk script with special for loop

/north/{name[count++]=$3}

END{ print "The number living in a northern district: " count

     print "Their names are: "

     for ( i in name )       # Special nawk for loop is used to

           print name[i]     # iterate through the array.

}



% nawk -f nawk.sc5 datafile

The number living in a northern district: 3

Their names are:

Joel

TJ

Val


EXPLANATION

Each time the regular expression north appears on the line, the name array is assigned the value of the third field. The index count is incremented each time a new record is processed, thus producing another element in the array. In the END block, the special-for loop is used to iterate through the array.

% cat datafile

northwest

NW

Joel Craig

3.0

.98

3

4

western

WE

Sharon Kelly

5.3

.97

5

23

southwest

SW

Chris Foster

2.7

.8

2

18

southern

SO

May Chin

5.1

.95

4

15

southeast

SE

Derek Johnson

4.0

.7

4

17

eastern

EA

Susan Beal

4.4

.84

5

20

northeast

NE

TJ Nichols

5.1

.94

3

13

north

NO

Val Shultz

4.5

.89

5

9

central

CT

Sheri Watson

5.7

.94

5

13


Example 6.166.

(The Command Line)

% cat nawk.sc6

# Awk and the special for loop

{region[$1]++}   # The index is the first field of each record

END{for(item in region){

        print region[item], item

    }

}



% nawk -f nawk.sc6 datafile

1 central

1 northwest

1 western

1 southeast

1 north

1 southern

1 northeast

1 southwest

1 eastern



% nawk -f nawk.sc6 datafile3

4 Mary

2 Tom

1 Alax

1 Bob

1 Sean


EXPLANATION

The region array uses the first field as an index. The value stored is the number of times each region was found. The END block uses the awk special-for loop to iterate through the array called region.

    Previous Section  < Day Day Up >  Next Section