zoukankan      html  css  js  c++  java
  • Awk基本入门[6] Additional Awk Commands 4

    1、Bit Manipulation


     Just like C, awk can manipulate bits.

    AND  and 

    OR or

    XOR xor

    Complement compl

    Left Shift  lshift

    Right Shift rlshift

    Awk Example using Bit Functions

    $ cat bits.awk
    BEGIN {
      number1=15
      number2=25
      print "AND: " and(number1,number2);
      print "OR: " or(number1,number2)
      print "XOR: " xor(number1,number2)
      print "LSHIFT: " lshift(number1,2)
      print "RSHIFT: " rshift(number1,2)
    }
    $
    awk -f bits.awk AND: 9 OR: 31 XOR: 22 LSHIFT: 60 RSHIFT: 3

     2、User Defined Functions


    Awk allows you to define user defined functions. This is extremely helpful when you are writing a lot of awk code and end-up repeating certain pieces of code every time. Those pieces could be fit into a user defined function.

    Syntax:

    function fn-name(parameters)
    {
        function-body
    }

    The following example creates a simple user defined function called discount that gives a discount in the prices for the specified percentage. For example, discount(10) gives 10% discount on the price.

    For any items where the quantity is <= 10, it gives 10% discount,otherwise it gives 50% discount.

    $ cat function.awk
    BEGIN {
      FS=","
      OFS=","
    }
    {
    if ($5 <= 10)
      print $1,$2,$3,discount(10),$5
    else
      print $1,$2,$3,discount(50),$5
    }
    function discount(percentage)
    {
      return $4 - ($4*percentage/100)
    }
    $ awk -f function.awk items.txt
    101,HD Camcorder,Video,189,10
    102,Refrigerator,Appliance,765,2
    103,MP3 Player,Audio,135,15
    104,Tennis Racket,Sports,95,20
    105,Laser Printer,Office,427.5,5

    3、Language Independent Output (Internationalization)


    4、Two Way Communication


     Awk can communication to an external process using "|&", which is two way communication.

    The following simple sed example substitutes the word "Awk" with "Sed and Awk".

    $ echo "Awk is great" | sed 's/Awk/Sed and Awk/'
    Sed and Awk is great

    To understand how the two way communication from Awk works, the following awk script simulates the above simple example using "|&"

    $ cat two-way.awk
    BEGIN {
    command = "sed 's/Awk/Sed and Awk/'"
    print "Awk is Great!" |& command
    close(command,"to");
    command |& getline tmp
    print tmp;
    close(command);
    }
    
    $ awk -f two-way.awk
    Sed and Awk is Great!

    In the above example:

      • command = "sed 's/Awk/Sed and Awk/'" -- This is the command to which we are going to establish the two way communication from awk. This is a simple sed substitute command, that will replace "Awk" with "Sed and Awk".
      • print "Awk is Great!" |& command -- The input to the command. i.e. The input to the sed substitute command is "Awk is Great!". The "|&" indicates that it is a two way communication. The input to the command on the right side to the "|&" comes from the left side.
      • close(command,"to") - Once the process is executed, you should close the "to" process.
      • command |& getline tmp - Now that the process is completed, it is time to get the output of the process using the getline. The output of the previously executed command will now be stored in the variable "tmp".
      • print tmp - This prints the output.
      • close(command) - Finally, close the command.


    Two way communication can come-in handy when you rely heavily on output from external programs.

    see: http://www.gnu.org/software/gawk/manual/html_node/Two_002dway-I_002fO.html

  • 相关阅读:
    C++程序设计第二周作业
    navicat 连接windows服务器中的mysql数据库
    Python 多进程(二度回顾)
    MySQl 合并结构相同的多张表
    Python 验证码识别-- tesserocr
    Navicat Premium 修改MySQL密码(忘记密码的情况下)
    Navicat Premium 出现2059错误解决办法
    MySQL 1053错误 服务无法正常启动的解决方法
    mysql触发器trigger 实例详解
    navicat for mysql 连接报错1251详细解决步骤
  • 原文地址:https://www.cnblogs.com/yangfengtao/p/3310231.html
Copyright © 2011-2022 走看看