1 awk usage report generation tool, which is mainly powerful in text processing and is also an independent programming language. It and grep sed constitute three muskeys for text processing tools.
Both gawk and gawk are open-source tools developed by GNU.
2 awk working principle, each record has been separated from each domain, for example, each row is often used as a record, blank as the default separator, then assign variables to each field with built-in variables!
3. Syntax structure
Awk option scrips file
Or this echo "" | awk option scripts pipeline is used
Option options mainly include:
-F specifies the delimiter, instead of the default blank space (space character or tab)
-V: specify variables
-F specifies the script file name, which puts the script into a file to avoid awk writing too long, affecting vision, and repeated writing.
Scripts is an operation on the processed text.
The awk program is defined by a pair of curly brackets. The script command must be placed in two curly brackets. Curly braces must be enclosed in single quotes.
For example, awk-F: '{print $1, $4}'/etc/passwd
4 awk basic skills
1. Use the data field variable $1, $2,... $ n
2. Use multiple commands in the script;
3. Read the script program from the file and use-f
4. Some scripts run before data processing BEGIN
5. Some scripts run after data processing END
5 simple Usage examples
Instance 1: awk first recognized
[[Email protected] ~] # Echo "how are you" | awk '{print $1, "\ t", $3 }'
How you
This section describes how are texts in the awk input stream for processing. First, use blank spaces to separate the field Fields (filed) and then assign values using built-in variables.
Built-in variables
$1 equals how in the first domain instance
$2 equals to "are" in the second domain instance.
$3 equals to you
If there are other segments, then the following statements are made: $4... $ n.
The last $0 equals to the entire record, that is, the whole line of text how are you
Option is not used here, and the default blank space is used for segmentation.
Scripts is print $1, "\ t", $3 prints the first field, a tab, and a third field
Example 2: Extract/data partition usage
[[Email protected] ~] # Df-h | grep "/data"
/Dev/sda7 97G 1.2G 91G 2%/data
[[Email protected] ~] # Df-h | grep "/data" | awk-F' [\ t %] + ''{print $5 }'
2
Interpretation step by step completes the extraction usage of/data usage to 2. Regular expressions are supported for the specified separator after-F. Here, a blank tab % can be used as the separator, and can be N
The above can also be used as follows, but it is not professional enough. Two awk requests are used.
[[Email protected] ~] # Df-h | grep "/data" | awk '{print $5}' | awk-f' % ''{print $1 }'
2
Instance 3-f usage
[[Email protected] awk] # awk-F: '{test = "home directory is"; print $1, test, $6}'/etc/passwd
-F implementation method
1. First, write the scripts part into a file.
[[Email protected] awk] # cat awkfile
{
Test = "home directory is"
Print $1, test, $6
}
[[Email protected] awk] # awk-F:-f awkfile/etc/passwd
Root home directory is/root
Bin home directory is/bin
The same effect is achieved. It is always necessary to put the processing script into a specific file. When test = "home directory is", the value of the test variable is assigned.
Usage of instance 4-v option
[[Email protected] awk] # awk-F:-v test = "home directory is" '{print $1, test, $6}'/etc/passwd
The result is the same as that of the instance.-v assigns a value to the test variable.
Example 5: BEGIN runs some scripts before processing data
For example:
[[Email protected] awk] # awk-F: 'In in {print "User", "--------------", "Shell"} {print $1, "\ t ", $7} '/etc/passwd
Explanation: The BEGIN keyword executes a specified script segment. The script segment for data processing defines {print $1, "\ t", $7} in the second program}
Result: the User --------- Sheel header is added.
[[Email protected] awk] # awk-F: 'In in {print "User", "--------------", "Shell"} {print $1, "\ t ", $7} '/etc/passwd
User -------------- Shell
Root/bin/bash
Bin/sbin/nologin
.....
Instance 6: END
[[Email protected] awk] # awk-F: 'In in {print "User", "--------------", "Shell"} {print $1, "\ t ", $7} END {print "The is last Line"} '/etc/passwd
After all The operations are completed, run The END {print "The is last Line"} script segment defined by The END keyword "}
Note: The two or three-segment script commands will be treated as a text string on the awk, so a single quotation mark must be added.
For more information about the advanced usage of awk, see the article.
Basic usage and advanced usage of awk