O'Reilly logo

Linux Shell Scripting Cookbook by Sarath Lakshman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Basic awk primer

awk is a tool designed to work with data streams. It is very interesting as it can operate on columns and rows. It supports many inbuilt functionalities such as arrays, functions, and so on, as in the C programming language. Flexibility is the greatest advantage of it.

How to do it…

The structure of an awk script looks like this:

awk ' BEGIN{  print "start" } pattern { commands } END{ print "end" } file

The awk command can read from stdin also.

An awk script usually consists of three parts: BEGIN, END, and a common statements block with the pattern match option. The three of them are optional and any of them can be absent in the script. The script is usually enclosed in single-quotes or double-quotes as follows:

awk 'BEGIN { statements ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required