Awk string concatenation operation (string to number, number to string)

  

awk data type, is not required to define, adaptive. Sometimes you need to cast. We can do this by the following operations.

1, awk string to number


[chengmo@centos5 ~]$ awk 'BEGIN{a="100";b="10test10"; Print (a+b+0);}' 110


You only need to connect the variables through the ”+” connection. Automatically force a string to be an integer. The non-numeric becomes 0, and the first non-numeric character is found, which is automatically ignored later.


Second, awk numbers are converted to strings

[chengmo@centos5 ~]$ awk 'BEGIN{a=100;b=100;c=(a" "b);print c}' 100100


You only need to connect variables to the ”” symbol.


Three, awk string connection operation

[chengmo@centos5 ~]$ awk 'BEGIN{a="a";b="b"; c=(a""b);print c}' ab


[chengmo@centos5 ~]$ awk 'BEGIN{a="a";b="b" ;;c=(a+b);print c}' 0


String concatenation operation "second",”+” number operator. The mode forces the values ​​of the left and right sides to be converted to numeric types. Then proceed.

Copyright © Windows knowledge All Rights Reserved