Linuxawk string connection operation (string to digit, number to string)

Source: Internet
Author: User
The data types in awk do not need to be defined and are adaptive. Sometimes forced conversion is required. You can complete the following operations:

I. convert an awk string to a number

[Chengmo @ centos5 ~] $ Awk 'In in {a = "100"; B = "10test10"; print (a + B + 0 );}'
110

You only need to connect the variable through "+. Automatically convert the string to an integer type. If the number is not 0, the first non-numeric character is found, which is automatically ignored later.

II. convert awk numbers into strings

[Chengmo @ centos5 ~] $ Awk 'In in {a = 100; B = 100; c = (a "" B); print c }'
100100

You only need to connect the variable with the "" symbol to perform the operation.

3. awk string connection

[Chengmo @ centos5 ~] $ Awk 'In in {a = "a"; B = "B"; c = (a "" B); print c }'
AB

[Chengmo @ centos5 ~] $ Awk 'In in {a = "a"; B = "B"; c = (a + B); print c }'
0

String join operations are performed by the "2", "+" operator. The mode forces the value of the left and right sides to be numeric. Then perform the operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.