As one of the excellent tools for text processing, awk has its own rich operators. The following is a summary of all operators. It can be divided into arithmetic operators, value assignment operators, relational operators, logical pre-algorithms, and regular operators.
I. INTRODUCTION to operators
Operator |
Description |
Value assignment operator |
= + =-= * =/= % = ^ = ** = |
Assignment statement |
Logical operators |
| |
Logic or |
&& |
Logic and |
Regular operators |
~ ~! |
Match regular expressions and do not match regular expressions |
Relational operators |
<<=>>=! === |
Relational operators |
Arithmetic operators |
+- |
Add, subtract |
*/& |
Multiplication, division and remainder |
+ -! |
Mona1 addition, subtraction, and non-logical |
^ *** |
Power |
++ -- |
Increase or decrease as a prefix or suffix |
Other operators |
$ |
Field reference |
Space |
String connector |
? : |
C-condition expression |
In |
Whether a key value exists in the array |
Note: The awk operator is basically the same as the C language. Expressions and functions are basically the same
II. instance introduction
Awk assignment operator
Copy codeThe code is as follows:
A + = 5; equivalent to: a = a + 5; other similar
Awk logical operators
Copy codeThe code is as follows:
[Chengmo @ localhost ~] $ Awk 'In in {a = 1; B = 2; print (a> 5 & B <= 2), (a> 5 | B <= 2 );}'
0 1
Awk regular operators
Copy codeThe code is as follows:
[Chengmo @ localhost ~] $ Awk 'In in {a = "100 testa"; if (~ /^ 100 */) {print "OK ";}}'
OK
Awk relational operators
Copy codeThe code is as follows:
For example:> <can be used for string comparison or numerical comparison. The key is that if the operand is a string, it is converted to string comparison. Both of them are numeric values. String comparison: Compare strings in ascii order.
[Chengmo @ localhost ~] $ Awk 'In in {a = "11"; if (a> = 9) {print "OK ";}}'
[Chengmo @ localhost ~] $ Awk 'In in {a = 11; if (a> = 9) {print "OK ";}}'
OK
Awk arithmetic operators
Copy codeThe code is as follows:
It indicates that all operations are performed as arithmetic operators, the operands are automatically converted to numerical values, and all non-numeric values are changed to 0.
[Chengmo @ localhost ~] $ Awk 'In in {a = "B"; print a ++, ++ ;}'
0 2
Other operators
Copy codeThe code is as follows:
? : Operator
[Chengmo @ localhost ~] $ Awk 'In in {a = "B"; print a = "B "? "OK": "err ";}'
OK
In operator
[Chengmo @ localhost ~] $ Awk 'In in {a = "B"; arr [0] = "B"; arr [1] = "c"; print (a in arr );}'
0
[Chengmo @ localhost ~] $ Awk 'In in {a = "B"; arr [0] = "B"; arr ["B"] = "c"; print (a in arr );}'
1
In operator to determine whether the key value exists in the array.