How To Read a Linux File Line by Line

This article will introduce the concept of playing a file line by line in Linux with the help of examples and best user tips. We'll walk you through some of the nigh mutual errors fabricated when reading a file on the Linux platform, and prove y'all examples of how the for loop and while loop outputs differ. We'll also provide you with some tips and examples on how to initiate a loop, and how to use the while loop output.

  • How To Read a File Line by Line
    • Mutual Errors with For Loops
    • While Loop Instance
    • For Loop Case
    • Tips for For Loops
    • Bonus
  • How To Initiate a Loop

How To Read a File Line by Line

Mutual Errors with For Loops

One of the nigh common errors when using scripts bash on GNU/Linux is to read a file line past line past using a for loop (for line in $ (cat file.txt) practice. ..). In this instance, the for loop leads to an assessment for each line, rather than as assessment of every give-and-take in the file.

It is possible to change the value of the variable $ IFS (Internal Field Separator, internal field separator) with a for loop before starting the loop.

Here is a sample output with a for loop:

for line in $ (cat file.txt) practise echo "$ line" done

                This                  
is
row
No
one
This
is
row
No
2
This

[...]

The solution is to use a while loop coupled with the internal read.

It is also possible to get the result with a for loop, provided you change the value of the variable $ IFS (Internal Field Separator, internal field separator) earlier starting the loop.

While Loop Case

The while loop remains the most appropriate and easiest mode to read a file line by line.

Syntax:

while read line                
do
command
done <file

For Loop Example

The starting file:

                This is line 1                              
  • This is line two *This is line 3 *This is line 4 *This is line 5
                

The instructions in the command line:

while read line; do echo -e "$line\n"; washed < file.txt
                

or in a "bash" script:

#! / bin / bash                
while read line
do
echo-e "$ line \ n"
done <file.txt
The output on the screen (stdout):
This is line 1

This is line 2

This is line 3

This is line iv

This is line 5

              

Tips for For Loops

                

From a structured file (such every bit an address book or /etc/passwd), information technology is entirely possible to retrieve the values of each field and assign them to several variables with the control 'read'. Be careful to properly assign the IFS variable with good field separators (space by default).
Example:

#! /bin/fustigate                
while IFS=: read user pass uid gid full home shell
do
echo -e "$total :\n\
Pseudo : $user\n\
UID :\t $uid\north\
GID :\t $gid\north\
Home :\t $habitation\northward\
Vanquish :\t $shell\due north\northward"
done < /etc/passwd
              

Bonus

                
while read i; do echo -e "Parameter : $i"; washed < <(repeat -due east "a\nab\nc")
              

How To Initiate a Loop

                

Although the while loop remains the easiest method for reading a file line by line, information technology does take its side effects. The while loop volition obliterate the formatting of lines, including spaces and tabs. Furthermore, the for loop coupled with a change of IFS helps keep the structure of the document output.
Syntax:

old_IFS=$IFS      # salve the field separator                
IFS=$'\north' # new field separator, the end of line
for line in $(true cat fichier)
exercise
command
done
IFS=$old_IFS # restore default field separator
                

Photo: © Everypixel

This certificate, titled « How To Read a Linux File Line by Line », is bachelor under the Creative Eatables license. Whatsoever copy, reuse, or modification of the content should exist sufficiently credited to CCM (ccm.net).