Reprinted: http://www.cnblogs.com/chengmo/archive/2010/10/11/1847515.html
As one of the excellent tools for text processing, awk has its own rich operators. The following is a summary of all operators. Can be divided into: Arithmetic Operators, value assignment operators, Relational operators, logical pre-Algorithm, Regular operator.
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: awk operators andC LanguageSame. Expressions and functions are basically the same
Ii. instance Introduction
A + = 5; equivalent to: a = a + 5; other similar
[Chengmo @ localhost ~] $ Awk 'in in {A = 1; B = 2; print (A> 5 & B <= 2), (a> 5 | B <= 2 );}'
0 1
[Chengmo @ localhost ~] $ Awk 'in in {A = "100 Testa"; if (~ /^ 100 */) {print "OK ";}}'
OK
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
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
? : 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.