Previous Table of Contents Next


PROGRAM EXAMPLES

The following are only the program parts of some useful nawk programs. You must add the nawk command and filenames to be processed to each of the following programs.


Command Description

'{ print NR:$0 }' # print line numbers(NR) and line($0)
'{ print NR:NF }' # print line numbers and number of fields
'{ print $1 }' # print first field of each line
'{ print $2 $1 }' # print field 1 and 2 of each line, reversed
'{ print $2" "$1 }' # same as above, separated by " "
' s = s + $1 END { print s }' # sum up field one, only print the sum
'{ if ( length > 72 ) {
    print substr($0,1,72)" *"
    print substr($0,73,132) }
else print $0 }'
# flag lines longer than 72 chars with a *
and print rest of line on next line
else print the line as normal
'{print substr($0,index($0,$5),132)}' # print the line ($0) starting with column 5 continuing to the end of line

DIAGNOSTICS AND BUGS

As with any programmrring language, there are some bugs. The most common problems appear to be the syntax of the patterns versus the action. Most of these problems are caused by users forgetting to insert a left or right brace ({}) or a double or single quote. Fortunately, nawk provides fairly good diagnostics when it aborts because of these problems.

Another area where problems arise is the handling of strings versus numbers. If you assign a string to a variable and then compare it to a number, it will not match. To convert a string to a number, add a zero. For example,

A="153" # assigns the string "153" to variable A
A=A + 0 # converts A to a number

The same problem arises with numbers. You can convert a number to a string by adding a null string. For example,

N=153 # assigns the number 153 to variable N
N=N + "" # converts N to a string

RELATED COMMANDS

Refer to the ed, ex, grep, sed, and ksh commands described in modules 39, 43, 60, 117, and 71.

RELATED FILES

By default, nawk reads from the standard input and writes to the standard output. If files are specified on the command line, then the files are read as input. You can use the getline command to read from specific files, pipes, or the current input.

The print and printf commands allow you to write to the standard output, specific file, and pipes. It is possible to read and write from various sources and destinations.

The one difference between nawk and most programming languages is the processing of input and output. The input is read automatically. The first line is read and all the pattern-action statements are checked for execution. If you want to read input from a specific file or pipe, you must use a loop to read the entire file or read one line of the specified file each time you process a line of the normal input.

APPLICATIONS

The nawk utility is a very powerful and flexible tool. It provides the functions of grep, egrep, sed, echo, and various other commands all wrapped in one package. It is best suited for reading in tables of data, searching for specific information and performing string manipulations and numeric calculations, and displaying formatted output.

TYPICAL OPERATION

In this activity you use the nawk command to center a line of text. Begin at the shell prompt.

1.  Type the following code in a shell script named center. You can use the cat command or your favorite editor (use vi). We use the cat command for simplicity. If you use the cat command, remember to press Ctrl-D to exit cat.
     cj> cat > center
     echo $* | \
     nawk '{ for ( i = 1; i <= ( 40 - ( length / 2 )); i++ )
          printf " "
         print $0
     }'
     ^D
2.  Type chmod 755 center and press Return to change the modes on the new center command to -rwxr-xr-x. This allows all users to read and execute the script, but only you can write to it.
3.  Now test the new shell script and nawk program. Type center Centering Text on a line! and press Return. Notice the output line is centered on the next line.
     cj> center Centering Text on a line!
                         Centering Text on a line!
4.  Turn to Module 25 (SV), Module 133 (BSD) to continue the learning sequence.


Previous Table of Contents Next