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'

  • 相关阅读:
    Android Studio 优秀插件: Parcelable Code Generator
    Android Studio 优秀插件:GsonFormat
    DrawerLayout(抽屉效果)
    Python拼接字符串的七种方式
    Python爬虫教程-使用chardet
    Python爬虫教程-实现百度翻译
    Tensorflow分布式部署和开发
    简单的Python GUI界面框架
    用keras构建自己的网络层 TensorFlow2.0教程
    Python GUI教程一:Hello World
  • 原文地址:https://www.cnblogs.com/hanleilei/p/2291203.html
Copyright © 2011-2022 走看看