AWK application-Retrieval of information
The awk program can be used to retrieve information from the database, which is actually various types of text files. The better the structure of a text file, the easier it is to work, even though the result is simply a line of independent words.
The following acronym list is a simple database.
$CatAcronyms
BASIC Beginner ' s AI i-purpose Symbol IC instruction code
cics Customer Information Control system
cobol Common business oriented language
dbms Data Base Management system
gigo garbage in, garbage out
girl Generalized information Retrieval Language
tab characters are used as field separators. We will see a program that takes the acronym as input and selects the corresponding row in the database as the output. (In the next fortunately, we will see two other programs that use the acronym database.) One program is to read the acronym list and find the location of these acronyms in another file. Another program is to fix the first occurrence of these acronyms in a text file and insert a description of the corresponding acronym. The shell script we wrote is named Acro. It gets the first argument from the command line (the name of the acronym) and passes it to the awk script, the Acro script is as follows:
$CatAcro
#!/bin/sh
#将shell的 The search variable assigned to awk
Awk' = = Search 'Search= $Acronyms
The first parameter in the shell command line ($) is assigned to the variable search, which is passed as an argument to the AWK program. The arguments passed to the AWK program are explained after the script. (This is a bit confusing because in the awk program, it represents the first field in each input row, and in the shell script, it represents the first parameter provided by the command line.) )
The following example shows how to use this program to find a special acronym in the list.
$ Acro CICS
CICS Customer Information Control System
Notice that we detect the parameter as a string ($1==search). We can also write this as a regular expression match ($1~search).
Reference: http://www.linuxawk.com/communication/533.html
AWK application-Retrieval of information