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

Implementing head, tail, and tac with awk

Mastering text-processing operations comes with practice. This recipe will help us practice incorporating some of the commands that we have just learned with some that we already know.

Getting ready

The commands head, tail, uniq, and tac operate line by line. Whenever we need line by line processing, we can always use awk. Let's emulate these commands with awk.

How to do it...

Let's see how different commands can be emulated with different basic text processing commands, such as head, tail, and tac.

The head command reads the first ten lines of a file and prints them out:

$ awk 'NR <=10' filename

The tail command prints the last ten lines of a file:

$ awk '{ buffer[NR % 10] = $0; } END { for(i=1;i<11;i++) { ...

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