Linuxawk operator (arithmetic operator, value assignment operator, relational operator, logical operation

Source: Internet
Author: User
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.

Related Article

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.