AWK application-Retrieval of information

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.