If you want to format the message or extract the packets from a large text file, awk can accomplish these tasks. It
Excellent performance in text browsing and proficiency in data usage.
It can be said that awk is a self-explanatory programming language that uses awk in the shell
Because awk itself is a good example of learning, combining awk with other tools such as grep and SED will make shell programming more
Easy.
1. Call awk
The first is the command line method
awk [-ffield-separator] ' commands ' input-file
The second method is to insert all awk commands into a file and make the awk program executable, and then use the awk command to interpret
As the first line of the script so that it can be called by typing the script name.
The third Way is to insert all the awk commands into a single file,
Awk-f Awk-scripts-file Input-file
The-F option indicates the awk script in file Awk_script_file, Input_file (s) is a file that is browsed using awk
Name.
2. awk Script
When Awk is called in a command, the awk script consists of various operations and patterns.
If the-f option is set, awk reads one record or row at a time and separates the specified domain with the specified delimiter, but
If the-f option is not set, awk assumes that the space is a field delimiter and maintains this setting until a new row is found. When a new line appears
, the awk command learns that the complete record has been read, and then starts the Read command on the next record, which continues to the file
Tail or file no longer exists
2-1. Modes and actions
Any awk statement is made up of patterns and actions. There may be many statements in an awk script. Mode part determines the dynamic
When the statement triggers and triggers the event. Processing is the manipulation of the data. If you omit the mode section, the action is always guaranteed
Execution status.
A pattern can be any conditional statement or compound statement or regular expression. The pattern consists of two special fields begin and
END. Use the BEGIN statement to set the count and print head. The BEGIN statement is used before any text-browsing action, and then
The text-browsing action is executed according to the input file. The end statement is used to print the output after awk completes the text-browsing action
The total and end status flags. If the pattern is not specifically specified, awk always matches or prints the number of rows.
The actual action is indicated within the curly braces {}. Actions are mostly used for printing, but there are some longer code such as if and loops
(looping) statement and loop exit structure. If you do not specify an action, awk prints out all the records that were browsed.
These patterns and actions are explained in depth below
2-2. Fields and Records
When Awk executes, its browse domain is marked as $1,$2 ... $n. This method is called the domain identifier. Using these domain identities will allow more
Further processing of the domain is easy.
Use $1,$3 to refer to fields 1th and 3rd, and note that the fields are delimited by commas. If you want to print one with 5 fields
All fields of the record, you do not have to indicate $1,$2,$3,$4,$5, you can use $ A, meaning all domains. When Awk browses, it arrives at a new
Line, which assumes that the end of the record that contains the field is reached, then executes the read action of the next row in the new record and sets the domain separation.
Note Do not confuse the symbol $ and shell prompt $ when executing, and they are different.
To print a field or all domains, use the Print command. This is an awk action (the verb syntax is enclosed in parentheses).
Save awk output
There are two ways to save the output of the awk script at the shell prompt. The simplest way to do this is to use the output redirect symbol >
Name, the following example redirects output to a file Wow.
awk ' {print $} ' somefile >wow
The second approach is to use the tee command to output to the screen while outputting to a file.
Using standard input
Scripts.awk Somefile
Or
Scripts.awk <somefile
Somefile | Scripts.awk
Print Report Header
The print information header is placed in the B E G I N mode section because the print header is bounded
As an action, it must be enclosed in curly braces.
awk ' Begin{print ' Name belt\n------------------"}
{print "\ T" $4} ' Somefile
Print end of message
If you add end of report information to the last line, you can use the end statement. The end statement executes in all text processing actions
is not executed until it is finished. The position of the end statement in the script is placed after the primary action.
awk ' Begin{print ' Name belt\n------------------"}
{print ' \ t ' $4} somefile
END {"End-of-report"} ' Somefile
AWK error message hint
When you use awk for the first time, you may be overwhelmed by the error message, but through long and continuous learning, you can always
The following rules are in the balance. When you encounter an awk error, you can search for it accordingly:
Make sure that the entire awk command is enclosed in single quotes.
Make sure that all quotes in the command appear in pairs.
Be sure to enclose the action statement in curly braces, enclosing the conditional statement in parentheses.
You might forget to use curly braces, and maybe you don't think it's necessary, but awk doesn't think so, and will interpret the syntax as it is.
Metacharacters
\ ^ $ . [] | () * + ?
+ use + to match one or more characters.
? The frequency at which the matching pattern occurs. For example, using/xy? z/matches XYZ or YZ.
awk built-in variables
ARGC number of command line arguments
ARGV Command line parameter arrangement
ENVIRON support for the use of system environment variables in queues
FileName awk browses the file name
FNR number of records to browse files
FS sets the input domain delimiter, which is equivalent to the command line-F option
NF browsing record number of fields
NR number of records read
OFS output Field delimiter
ORS Output Record delimiter
RS Control record delimiter
ARGC supports the number of arguments that are passed to the awk script in the command line. ARGV is an array of parameter permutations for ARGC, where each
An element represents a command-line argument that Argv[n],n is expected to access.
ENVIRON supports system-set environment variables, to access individual variables, use the actual variable name, for example
environ["editor"]= "Vi".
FILENAME supports the input file for the actual operation of the awk script. Because awk can process many files at the same time,
This variable will tell the actual file the system is currently viewing.
FNR supports the number of records that AWK currently operates. Its variable value is less than or equal to Nr. If the script is accessing many files,
This variable is reset for each new input file.
FS is used to set the domain delimiter in awk, which is the same as the-f option in the command line. By default, it is a space. If you use
Comma to make the field delimiter, set fs= ",".
NF supports the number of record fields and then sets them after the record is read.
OFS allows you to specify the output field delimiter, which is the default space. If you want to set to #, write ofs= "#".
The ORS is the output record delimiter, which defaults to the new line (\ n).
RS is the record delimiter, and the default is a new line (\ n).
Built-in String functions
Gsub (R,s) replaces R with s in the entire $
Gsub (R,S,T) replaces R with S in the whole t
Index (S,T) returns the first position of the string T in S
Length (s) returns s
Match (S,R) tests if s contains a string that matches R
Split (S,A,FS) divides s into sequence a on FS
Sprint (FMT,EXP) returns the EXP formatted by FMT
Sub (r,s) replaces s with the longest leftmost substring in
SUBSTR (s,p) returns the suffix part of the string s starting from P
SUBSTR (s,p,n) returns the suffix part of the string s starting from p in length n
This article is from the "Linux is belong to You" blog, make sure to keep this source http://jwh5566.blog.51cto.com/7394620/1639716
AWK Learning Notes