Awk operators (Arithmetic Operators, value assignment operators, Relational operators, logical operators, and regular operators)

Source: Internet
Author: User

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

    • Awk assignment operator

A + = 5; equivalent to: a = a + 5; other similar

    • Awk logical operators

[Chengmo @ localhost ~] $ Awk 'in in {A = 1; B = 2; print (A> 5 & B <= 2), (a> 5 | B <= 2 );}'
0 1

    • Awk regular operators

[Chengmo @ localhost ~] $ Awk 'in in {A = "100 Testa"; if (~ /^ 100 */) {print "OK ";}}'
OK

    • Awk Relational operators

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

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

? : 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.

 

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.