[Copyright statement: reprinted. Please retain the Source: blog.csdn.net/gentleliu. Mail: shallnew at 163 dot com]
Many regular expressions are used in the previous grep section. This section uses regular expressions in awk condition operations. Awk allows you to use a regular expression to select whether to execute an independent code block based on whether the regular expression matches the current row.
The following describes the conditional operators of awk:
<Less
<= Less than or equal
= Equal
! = Not equal
> Greater
> = Greater than or equal
~ Match Regular Expression
!~ Do not match Regular Expression
This section continues to use the previous operation File
1. Match
If we want to filter rows that only meet the conditions (for example, if the group name is a1), we can write as follows:
# Awk '$1 ~ /A1/ {print} 'group_file1a1x 1001 # awk '/a1/ {print}' group_file1a1x 1001 # awk '{if ($1 ~ /A1/) print} 'group_file1a1x 1001 # awk' $1 ~ /A1/ 'group _ file1 // no specific action (such as print). No curly braces are required. The default action is to print all matching records. Alb x 1001
The above four methods print the rows with the first domain containing the a1ns. Likewise !~ The operation symbols are used in the same way.
2. Exact match
To perform exact matching, use the = Operator. For example:
# awk '$3 =="984"{print}' group_file1vboxusers x 984allen# awk '{if($3=="984")print}' group_file1vboxusers x 984allen# awk'$3=="984"' group_file1vboxusers x 984allen#
! = The operation symbol is similar.
3. Greater
The following example uses the file to be operated:
# catgroup_file2wireshark x 987123usbmon x 986 999jackuser x 985 985vboxusers x 9841003 allenaln x 1001 787
The following example shows the group name of the rows that meet the requirements of $3> $4:
# awk '{if($3>$4)print $1}' group_file2wiresharkaln# awk'$3>$4{print $1}' group_file2wiresharkaln
The operation symbols <=,>,> = are similar.
4. Match the beginning of the line
# awk'$1~/^....user/' group_file2jackuser x 985985vboxusers x 9841003 allen# awk '{if($1~/^....user/)print}' group_file2jackuser x 985985vboxusers x 9841003 allen
Awk also allows the use of boolean operators "|" (logical and) and "&" (logical or) to create more composite boolean expressions, A composite expression is an expression that combines modes by using the following expressions:
& And: both sides of the statement must be true.
| Or: The statement matches both sides of the statement or one of them to true.
! Non-Inverse
5. sum:
# awk'$1~/^....user/ && $3==985' group_file2jackuser x 985985# awk '{if($1~/^....user/ && $3 == 985)print}' group_file2jackuser x 985985
6. Seek or
# awk '{if($1~/^....user/ || $3 == 1001)print}' group_file2jackuser x 985985vboxusers x 9841003 allenaln x 1001 787
7. Do not
# awk'!($1~/^....user/)' group_file2wireshark x 987123usbmon x 986 999aln x 1001 787
Shell text filtering programming (III): Conditional judgment for awk