zoukankan      html  css  js  c++  java
  • Awk基本入门[3] Awk Variables and Operators

    1、Variables


    You don't need to declare an variable to use it. If you wish to initialize an awk variable, it is better to do it in the BEGIN section, which will be executed only once.

    There are no data types in Awk. Whether an awk variable is a number or a string depends on the context in which the variable is used in.

    employee-sal.txt sample file
    employee-sal.txt is a comma delimited file that contains 5 employee records in the following format:

    $ vi employee-sal.txt
    101,John Doe,CEO,10000
    102,Jason Smith,IT Manager,5000
    103,Raj Reddy,Sysadmin,4500
    104,Anand Ram,Developer,4500
    105,Jane Miller,Sales Manager,3000

    The following example shows how to create and use your own variable inside an awk script. In this example, "total" is the user defined Awk variable that is used to calculate the total salary of all the employees in the company.

    $ cat total-company-salary.awk
    BEGIN {
    FS=",";
    total=0;
    }
    {
    print $2 "'s salary is: " $4;
    total=total+$4
    }
    END {
    print "---
    Total company salary = $"total;
    }
    $ awk -f total-company-salary.awk employee-sal.txt
    John Doe's salary is: 10000
    Jason Smith's salary is: 5000
    Raj Reddy's salary is: 4500
    Anand Ram's salary is: 4500
    Jane Miller's salary is: 3000
    ---
    Total company salary = $27000

    2、 Operators


     

    Unary Operators

    An operator which accepts a single operand is called a unary operator.

    Unary Operators
    Operator     Description
    +    

    The number (returns the number itself)

    -  

    Negate the number

    ++

    Auto Increment 

    --

    Auto Decrement

    Arithmetic Operators

    An operator that accepts two operands is called a binary operator.

    Arithmetric  Operator
    Operator  Description
    + Addtion
    - Subtraction
    * Multiplication 
    / Devision
    % Module Devision

    String Operator

    (space) is a string operator that does string concatenation.

    $ cat string.awk
    BEGIN {
    FS=",";
    OFS=",";
    string1="Audio";
    string2="Video";
    numberstring="100";
    string3=string1 string2;
    print "Concatenate string is:" string3;
    numberstring=numberstring+1;
    print "String to number:" numberstring;
    }
    $ awk -f string.awk items.txt
    Concatenate string is:AudioVideo
    String to number:101

    Assignment Operators

    Comparison Operators 
    Operator

    Description

    >

    Is greater than

    >=

    Is greater than or equal to

    <   

    Is less than

    <= Is less than or equal to
    ==

    Is equal to

    !=  

    Is not equal to

    &&

    Both the conditional expressions are true

    ||

     

    Either one of the conditional expressions is true

    3、Regular Expression Operators


    Print lines where field two contains “Tennis”:

    $ awk -F "," '$2 ~ "Tennis"' items.txt
    104,Tennis Racket,Sports,190,20

    Print lines where field two does not contain “Tennis”:

    $ awk -F "," '$2 !~ "Tennis"' items.txt
    101,HD Camcorder,Video,210,10
    102,Refrigerator,Appliance,850,2
    103,MP3 Player,Audio,270,15
    105,Laser Printer,Office,475,5

     

  • 相关阅读:
    SpringBoot-源码解析
    SpringBoot快速部署
    vue.js
    数据库设计--三范式
    mysql 建表及练习
    索引复制迁移
    nginx检查、重启、、、
    设计模式--Strategy 策略模式
    设计模式--设计原则
    设计模式--Template Method(模板方法)
  • 原文地址:https://www.cnblogs.com/yangfengtao/p/3144711.html
Copyright © 2011-2022 走看看