zoukankan      html  css  js  c++  java
  • AWK 学习手札之一: an AWK tutorial

    these article base on the AWK programming language by alfred v.aho brian W.Kernighan and Peter J.Weinberger.

    1.1 geting start:

    generate a file name emp.data, which contain the name of employee, pay rate in dollar perl hour and worked hour, one employee record per line

    Beth    4.00    0
    Dan    3.75    0
    Kathy    4.00    10
    Mark    5.00    20
    Mary    5.50    22
    Susie    4.25    18

    now you want to print the name and pay for everyone who worked more than zero hour, type the comman line:

    1  awk '$3>0{print $1,$2*$3}' emp.data

    your will get this output:

    Kathy 40
    Mark 100
    Mary 121
    Susie 76.5

    the part inside the quotes is the complete awk program. it consists of a single pattern-action statement.

    the pattern: $3>0, matchs every input line in which the third column, or field, is greater than zero, and the action

    {print $1,$2*$3}

    prints the first field and theproduct of the second and third fields of each matched line.

    if you want to print the name of those employees who did not work, type this command line:

    1  awk '$3>0{print $1,$2*$3}' emp.data

    you will get:

    Beth

    Dan

    1.2 the structure of an awk program

    each awk program in this chapter is a sequence of one or more pattern-action statements:

    pattern {action}

    the basic operation of awk is to scan a sequence of input lines one after another, searching for lines that are matched by any of the patterns in the program.

    1.3 running awk programming

    awk 'program' input files

    awk 'program'

  • 相关阅读:
    Java的类演进过程
    P112、面试题16:反转链表
    P107、面试题15:链表中倒数第K个结点
    Java对象相关元素的初始化过程
    P102、面试题14:调整数组顺序使奇数位于偶数前面
    P99、面试题13:在o(1)时间删除链表结点
    面试常考的数据结构Java实现
    Linux命令面试常考的简单汇总
    操作系统与进程基础知识
    python--process
  • 原文地址:https://www.cnblogs.com/hanleilei/p/2291203.html
Copyright © 2011-2022 走看看