Giter Club home page Giter Club logo

awk-learn's Introduction

YES, I learn old-fashion awk

Useful Snippets

  1. print the last field of records; print the last nth field with $(NF - n)
{ print $NF }

Pitfalls

  1. pipe 不能连续使用,只能有一个管道,而且管道的输出会直接去到标准输出,没有办法用变量收集
  2. array, 实际上是类似于lua里面的table, 可以有数字键,也可以有字符串键

history

  1. awk in fact, inspires languages like perl and ruby
  2. awk is Turing complete
  3. awk is dedicated for string processing

Design

  1. A lot of implicit thing, like default action, default pattern; it is because when awk is invented, keystrokes, in another word, IO resources is valuable. Many people share a small slice of CPU.

usage

Generals

  1. expression {action}, expression can be regex pattern, also common seen expressions something like NR == 2
  2. in fact, regex pattern is a shorthand for $0 ~ /regex/
  3. functions call in awk: () can be omitted, inspiring ruby somehow, and parameter separator can be either <space> or ,
  4. there is array type in awk
  5. it can load module with @load
  6. you can call other command line tools in awk, also piping with | and redirecting with >

Builtin Functions

  1. printf, just a C-like printf sharing the same features
  2. A lot of C math functions, like atan2, sqrt, rand
  3. builtin string processing functions

NOTE: gsub does not work as this picture claims, it acutally replace the 1st arg with the 2nd

Examples

# substitute all `root` for `good`
ps aux | awk ' { gsub("root" , "good", $0); print }'
# NOTE: 1 index
# this program print usernames without the 1st letter
ps aux | awk '{ a = substr($1, 2); print a}'

Expressions

Builtin Auto Variables

Resources

  1. Amazing material from FreedomBen
  2. GNU awk documentation

awk-learn's People

Contributors

leenuus avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.