How To Read a Linux File Line by Line

This commodity will introduce the concept of playing a file line past line in Linux with the help of examples and all-time user tips. We'll walk you through some of the most common errors made when reading a file on the Linux platform, and show you 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
    • Common Errors with For Loops
    • While Loop Example
    • For Loop Example
    • Tips for For Loops
    • Bonus
  • How To Initiate a Loop

How To Read a File Line by Line

Common Errors with For Loops

1 of the about common errors when using scripts fustigate on GNU/Linux is to read a file line by line by 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 cess of every word 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) do echo "$ line" done

                This                  
is
row
No
1
This
is
row
No
two
This

[...]

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

It is likewise possible to become the result with a for loop, provided you alter the value of the variable $ IFS (Internal Field Separator, internal field separator) before starting the loop.

While Loop Instance

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

Syntax:

while read line                
exercise
control
done <file

For Loop Example

The starting file:

                This is line 1                              
  • This is line 2 *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"; done < file.txt
                

or in a "bash" script:

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

This is line 2

This is line 3

This is line 4

This is line 5

              

Tips for For Loops

                

From a structured file (such equally an address book or /etc/passwd), it is entirely possible to retrieve the values of each field and assign them to several variables with the control 'read'. Exist conscientious to properly assign the IFS variable with good field separators (space by default).
Case:

#! /bin/bash                
while IFS=: read user pass uid gid total dwelling house beat
practice
echo -e "$full :\due north\
Pseudo : $user\n\
UID :\t $uid\n\
GID :\t $gid\n\
Domicile :\t $home\n\
Beat :\t $trounce\north\n"
washed < /etc/passwd
              

Bonus

                
while read i; do echo -e "Parameter : $i"; done < <(echo -e "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 have its side effects. The while loop will obliterate the formatting of lines, including spaces and tabs. Furthermore, the for loop coupled with a alter of IFS helps keep the construction of the document output.
Syntax:

old_IFS=$IFS      # salvage the field separator                
IFS=$'\n' # new field separator, the end of line
for line in $(cat fichier)
do
command
done
IFS=$old_IFS # restore default field separator
                

Photo: © Everypixel

This document, titled « How To Read a Linux File Line by Line », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.cyberspace).