Chapter 10. Awk Fundamentals

The stream editor is not alone in its family and has a big brother awk. In this chapter, we will run through the basics or fundamentals of awk and see the power of the awk programming language. We will learn why we need and love awk and how we can make use of some of the basic features before we start putting awk to practical use in the next two chapters. As we work our way through this, we will cover the following topics:

  • Filtering content from files
  • Formatting output
  • Displaying non-system users from /etc/passwd
  • Using awk control files

The history behind awk

The awk command is a mainstay in the command suite in both Unix and Linux. The Unix command awk was first developed by Bell Labs in the 1970's and is named after the surnames of the main authors: Alfred Aho, Peter Weinberger, and Brian Kernighan. The awk command allows access to the awk programming language, which is designed to process data within text streams.

To demonstrate the programming language that is provided with awk, we should create a hello world program. We know this is compulsory for all languages:

$ awk 'BEGIN { print "Hello World!" }'

Not only can we see that this code will print the ubiquitous hello message, we can also generate header information with the BEGIN block. Later, we will see that we can create summary information with an END code block by allowing for a main code block.

We can see the output of this basic command in the following screenshot:

The history behind awk
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.158.87