• 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

     

  • 相关阅读:
    隐藏窗口任务栏图标
    初始化 键盘设备
    web2.0最全的国外API应用集合
    about hadoop-eclipse-plugin used by IDE
    Instructions Set JAVA_HOME System-Wide
    腾讯面试题 腾讯面试题:给40亿个不重复的unsigned int的整数,没排过序的,然后再给一个数,如何快速判断这个数是否在那40亿个数当中?
    word 2013 没有控件菜单怎么办,添加控件菜单
    面试题:实现LRUCache::Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法
    面试题:m个长度为n的ordered array,求top k 个 数字
    面试题: generate an equation, by inserting operator add ("+") and minus ("-") among the array to make equationExpression == 0
  • 原文地址:https://www.cnblogs.com/yangfengtao/p/3144711.html
Copyright © 2020-2023  润新知