Awk is short for three people: Aho, (Peter) weberger and (Brain) Kernighan. The three created awk, an excellent style scanning and processing tool.
What is AWK's function?
Like sed and grep, awk is a style scanning and processing tool. However, the function is much stronger than sed and grep. Awk provides extremely powerful functions: it can almost complete all the work that grep and sed can do. At the same time, it also supports style loading, flow control, mathematical operators, process control statements, and even built-in variables and functions. It has almost all the exquisite features of a complete language. In fact, awk does have its own language: awk programming language. The three creators of awk have formally defined it as: style scanning and processing language.
Why use awk?
The first reason for using awk is that text-based style scanning and processing is what we often do. What awk does is a bit like a database, but unlike a database, it processes text files. These files do not have a special storage format, so ordinary people can edit, read, understand, and process them. Database files often have special storage formats, so that they must be processed by database processing programs. Since we often encounter this kind of database-like processing work, we should find a simple and easy way to process them. UNIX has many tools in this field, for example, sed, grep, sort, and find, awk is an excellent one.
The second reason for using awk is that awk is a simple tool. Of course, this is relative to its powerful functions. Indeed, UNIX has many excellent tools, such as the UNIX natural development tool C language and its continuation of C ++ is very good. However, compared with them, it is much easier and easier for awk to complete the same function. The first reason is that awk provides solutions to meet a variety of needs: From awk command lines that solve simple problems to complex and sophisticated awk programming languages. The advantage of doing so is that, you don't have to use complicated methods to solve simple problems. For example, you can use a command line to solve simple problems, but C is not good. Even a simple program, the C language must be compiled and compiled throughout the process. Secondly, the awk itself is interpreted and executed, which makes the awk program not to be compiled, but also makes it fit well with the shell script program. Finally, awk itself is relatively simple in C language. Although awk absorbs many excellent components of C language, it will be of great help to learn awk, however, awk itself does not need to use C language-a development tool that is powerful but requires a lot of time to learn its skills.
The third reason for using awk is that awk is an easy-to-obtain tool. Unlike C and C ++, awk has only one file (/bin/awk), and almost every version of UNIX provides its own version of awk, you don't have to bother thinking about how to get awk. But this is not the case with C language. Although C language is a natural UNIX development tool, it is released independently. In other words, you must pay for your unix c language development tool separately, except for those who use the D version), obtain and install it, and then you can use it.
For the above reasons, coupled with the powerful features of awk, we have reason to say that if you want to deal with the work related to text style scanning, awk should be your first choice. Here is a general principle that can be followed: If you have difficulty using a common shell tool or shell script, try awk. If awk still cannot solve the problem, use C language, if the C language still fails, move it to C ++.
Awk call Method
As mentioned earlier, awk provides different solutions to meet various needs. They are:
I. awk command line: You can use awk like a Common UNIX Command. You can also use the awk programming language in the command line. Although awk supports multi-line input, however, inputting a long command line and ensuring that it is correct is a headache. Therefore, this method is generally only used to solve simple problems. Of course, you can also reference awk command lines or even awk program scripts in shell script programs.
2. Use the-f option to call the awk program. Awk allows an awk program to be written into a text file, and then the program is called and executed using the-f option in the awk command line. The specific method is described in the awk syntax.
3. Use the command interpreter to call the awk program: using the command interpreter function supported by UNIX, we can write an awk program into a text file, and then add the following to its first line:
#! /Bin/awk-f
And grant this text file the execution permission. After doing so, you can call and execute this awk program in the command line in a way similar to the following.
$ Awk script text name: file to be processed
Awk syntax
Like other UNIX commands, awk has its own Syntax:
Awk [-F re] [parameter...] ['prog'] [-f progfile] [in_file...]
Parameter description:
-F re: Allows awk to change its field separator.
Parameter: this parameter helps assign values to different variables.
'Prog': Specifies the Program Statement segment of the awk. The statement segment must be enclosed by the single extension number 'and' to prevent shell interpretation. The standard format of this Program Statement segment is:
'Pattern{ action }'
The pattern parameter can be any of the regular expressions of egrep. It can be made up of the syntax/re/and some style matching techniques. Similar to sed, you can also use "," to separate the two formulas to select a certain range. For details about the matching, refer to the appendix. If you still don't understand it, find a UNIX book and learn grep and sed. I learned the matching technology when learning ed ). The action parameter is always enclosed by braces. It consists of a system awk Statement, which is separated. Awk interprets them and performs their operations on the records that match the pattern given. Similar to shell, you can also use "#" as the annotator to make the content from "#" to the end of the line into comments, which will be ignored during interpretation. You can omit either pattern or action, but not both. If pattern is omitted, no style match exists, indicating that all rows are recorded.) All operations are performed, if action is omitted, the default operation is executed. The default operation is displayed on the standard output.
-F progfile: Allows awk to call and execute progfile to specify a program file. Progfile is a text file that must comply with awk syntax.
In_file: the input file of the awk. awk allows processing of multiple input files. It is worth noting that awk does not modify the input file. If no input file is specified, awk accepts the standard input and displays the result on the standard output. Awk supports input/output redirection.