Using Awk

GNU awk—or gawk—is a version of the text processing and patterning language originally developed by Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan (the name awk comes from the first letters of their last names).

Awk is best used for processing text files because it "thinks" in fields and records, typically words and lines. Therefore, it's not very good at handling binary files. A typical awk application is one that translates data into a formatted approach.

You'll find a great deal of information about programming with gawk in your /usr/doc/gawk-3.0.3 directory.

Gawk can be used from the command interactively, in which case the gawk commands need to be enclosed in single quotation marks to tell the shell not to process them. Gawk, programs longer than a command or two should be written in a file.

In Gawk, NF is a predefined variable holding the number of fields in each record (words in a line), $0 contains the record (line), $1 contains the first field in the line (first word), $2 contains the second field in the line (second word), and so on.

To display a listing with the number of words in each line in a text file:

1.
Create a text file (Figure 12.21).

Figure 12.21. Gawk is best used for manipulating structured files.


2.
Save the file as barn.txt.

3.
At the command prompt, type

cat barn.txt | gawk '{print NF ":"  $0}'

This line uses the cat command to pipe the text file to the gawk command.

4.
Press Enter.

The number of words in each line of the file will be displayed, along with the full text of each line (Figure 12.22).

Figure 12.22. You can use gawk interactively or run gawk commands in a file. Here, here gawk is used to display the number of words in each line of a file.


Tip

The full text of this gawk script is

{print NF ":"  $0}


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.222.185