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

Iterating through lines, words, and characters in a file

Iterating through character, word, and lines in a file is a frequently required script element while writing different text processing and file operation scripts. Even though it is simple to perform, we make simple mistakes and it gets erroneous without getting the expected output. This recipe will help you out to learn how to do it.

Getting ready

Iteration with a simple loop and redirection from stdin or file are basic components of performing the mentioned tasks.

How to do it...

In this recipe we discuss about performing three tasks of iterating through line, word, and characters. Let's see how each of these tasks can be performed.

  1. Iterate through each line in a file:

    We can use a while loop ...

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