Operators awk

1. Arithmetic operators: +, -, *, /,%

[root@oldboy test]# awk 'BEGIN{a=50;b=20;print "(a+b)=",(a+b)}'        
(a+b)= 70
[root@oldboy test]# awk 'BEGIN{a=50;b=20;print("(a+b)=",(a+b))}' 
(a+b)= 70
[root@oldboy test]# awk 'BEGIN{a=50;b=20;print "(a-b)=",(a-b)}'   
(a-b)= 30
[root@oldboy test]# awk 'BEGIN{a=50;b=20;print "(a*b)=",(a*b)}'  
(a*b)= 1000
[root@oldboy test]# awk 'BEGIN{a=50;b=20;print "(a/b)=",(a/b)}'  
(a/b)= 2.5
[root@oldboy test]# awk 'BEGIN{a=50;b=20;print "(a%b)=",(a%b)}'  
(a%b)= 10

 

2. Increment and decrement operators +, -

AWK support increment and decrement operators:

Pre-increment

It is represented by ++.

It is the value of the operator to increment the operand 1 by the value of the increment operation, and then returns the incremented value.

For example, in the following example this value set by the operator two operands, a and b added to 11.

[root@oldboy test]# awk 'BEGIN { a = 10; b = ++a; printf "a = %d, b = %d\n", a, b }'
a = 11, b = 11
 
  

Reduction ago

It is represented by the following formula - FIG.

Subtracting operand 1, the operator is first decremented value of the operand, and then returns the value decremented.

For example, in the following example this value set by the operator in the two operands, a and b to 9.

# --a表示a=a-1,++a表示a=a+1
[root@oldboy ~]# awk 'BEGIN{a=10;print a,--a}' 10 9 [root@oldboy ~]# awk 'BEGIN{a=10;print a,--a,--a}' 10 9 8 [root@oldboy ~]# awk 'BEGIN{a=10;print a,--a,--a,++a}' 10 9 8 9 [root@oldboy ~]# awk 'BEGIN{a=10;print a,--a,--a,++a,++a}' 10 9 8 9 10

 

Post-increment

It is represented by ++.

It increments the value of the operand 1, the operator returns the value of the first operand, and to increase its value.

# Increment value of the operand 1, the first returned value of the operand, return to the original value for the first time a ++ 
[Oldboy the root @ ~] # awk 'the BEGIN {A = 10;} a ++ Print' 10

# to go back to the original value, and then increments. 1 [the root @oldboy ~] # awk 'the BEGIN {A = 10; Print A ++, A ++}' 10. 11 [the root @oldboy ~] # awk 'the BEGIN {A = 10; Print A ++, A ++, A ++}' 10. 11 12

 

After descending

It consists of - representation.
-1 operand, the operator returns the first operand value, and then decreasing its value.
[root@oldboy ~]# awk 'BEGIN{a=10;print a--,a--,a--}'
10 9 8

 

3. assignment operators =, + =, - =, * =, / =,% =, ^ =, = **

[root@oldboy ~]# awk 'BEGIN{name="Joe";print name}'  
Joe

[root@oldboy ~]# awk 'BEGIN{cnt=100;cnt+=10;print "Counter =",cnt}'  
Counter = 110
[root@oldboy ~]# awk 'BEGIN{cnt=100;cnt-=10;print "Counter =",cnt}' 
Counter = 90
[root@oldboy ~]# awk 'BEGIN{cnt=100;cnt*=10;print "Counter =",cnt}'
Counter = 1000
[root@oldboy ~]# awk 'BEGIN{cnt=100;cnt/=10;print "Counter =",cnt}' 
Counter = 10
[root@oldboy ~]# awk 'BEGIN{cnt=100;cnt%=10;print "Counter =",cnt}' 
Counter = 0
[root@oldboy ~]# awk 'BEGIN{cnt=100;cnt^=10;print "Counter =",cnt}' 
Counter = 100000000000000000000
[root@oldboy ~]# awk 'BEGIN{cnt=100;cnt**=10;print "Counter =",cnt}'  
Counter = 100000000000000000000

 

4. Relational operators ==,! =, <, <=,>,> =

[root@oldboy ~]# awk 'BEGIN{a=10;b=10;if (a==b) print( "a==b")}'
a==b
[root@oldboy ~]# awk 'BEGIN{a=10;b=20;if (a != b) print( "a != b")}'       
a != b
[root@oldboy ~]# awk 'BEGIN{a=10;b=20;if (a < b) print( "a < b")}'      
a < b
[root@oldboy ~]# awk 'BEGIN{a=10;b=20;if (a <= b) print( "a <= b")}'
a <= b
[root@oldboy ~]# awk 'BEGIN{a=10;b=20;if (b > a ) print( "b > a ")}'              
b > a 
[root@oldboy ~]# awk 'BEGIN{a=10;b=10;if (a >= b ) print( "a >= b ")}'      
a >= b 

 

The logical operators ||, &&

[root@oldboy ~]# awk 'BEGIN{num=5;if (num>=0 && num <= 7) printf "%d is in octal format\n",num}'
5 is in octal format

[root@oldboy ~]# awk 'BEGIN{ch="\n";if (ch == " " || ch == "\t" || ch == "\n" ) print "Current character is whitespace."}'
Current character is whitespace.

[root@oldboy ~]# awk 'BEGIN {name=""; if (! length(name)) print "name is empty string."}'
name is empty string.

 

6. The ternary operator

Ternary operator conditional expressions easily.

grammar:

  condition expression ? statement1: statement2

When the conditional expression returns a value of true, statement1 is executed, otherwise statement2

[root@oldboy ~]# awk 'BEGIN{a=10;b=10;(a>b) ? max=a : max=b; print "Max =",max}'
Max = 10

 

7. The unary operators: one yuan adding monohydric subtraction

One yuan addition operation represented by a +, a single operand multiplied by +1.

Unary minus operation, represented by - said multiplying -1 by a single operand.

# a = +a 即 a=a*1
[root@oldboy ~]# awk 'BEGIN{a=-10; print a= +a}'                    
-10

# -10*1=-10; -10 *1 =-10
[root@oldboy ~]# awk 'BEGIN{a=-10; print a= +a,a = +a}'   
-10 -10

# -10 + (-10)=-20
[root@oldboy ~]# awk 'BEGIN{a=-10; print a= +a,a = +a+a}'
-10 -20

# -10 * -1 = 10
[root@oldboy ~]# awk 'BEGIN{a=-10; print a= +a,a = -a}'   
-10 10

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zoe233/p/11927748.html
awk