How to read a file line by line

Last update on February 22, 2009 11:17 AM by deri58
Published by deri58

How to read a file line by line




Intro


One of the most common errors of learning scripts bash on GNU / Linux is to read a file line by line, is to use a loop "for" (for line in $ (cat file.txt) do. ..), which in this example leads to an assessment for li not every word of the file.


Sample output with a loop "for":
for line in $ (cat file.txt) do echo "$ line" done
This
is
the
row
No
1
This
is
the
row
No
2
This

[...]

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

But it is possible to get me the result with a loop "for" provided to change the value of the variable $ IFS (Internal Field Separator, internal field separator) before starting the loop. This is what we will see more ...
While loop

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

while read line
do
command
done <file


==Example==

The starting file:
This is the No. 1 line
This is line 2
This is the line # 3
This is the line n ° 4
This is the line # 5


The instructions from the command line:
while read line; do echo-e "$ line \ n" done <file.txt

or in a script "bash":
#! / bin / bash

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

The output on the screen (stdout):
This is the No. 1 line

This is line 2

This is the line # 3

This is the line n ° 4

This is the line # 5

Tips


It is entirely possible from a structured file (like an address book or / etc / passwd for example), retrieve the values of each field and assigned to several variables with the command "read" . Be careful to properly assign the variable IFS "good field separator (space by default).

Example:
#! / bin / bash

while IFS =: read user pass full uid gid home shell
do
echo-e "$ full: \ n \
Username: $ user \ n \
UID: \ t $ uid \ n \
GID: \ t $ gid \ n \
Home: \ t $ home \ n \
Shell: \ t $ shell \ n \ n "
done </ etc / passwd

Bonus


while read i; do echo-e "Parameter: $ i" done <<(echo-e "a \ nab \ nc")
For loop

If the loop "while" is despite the easiest method, it has its side effect, however, that it obliterates the formatting lines including spaces and tabs - \
Moreover the loop "for" coupled with a change of IFS helps keep the structure of the document output.
Syntax

oldIFS = $ IFS # save the field separator
IFS = $ '\ n' # new field separator, the end of line
for line in $ (cat file)
do
command
done
IFS = $ # old_IFS
restoration of field separator default
Best answers for « How to read a file line by line » in :
Reading a .Pub file without Publisher Show Reading a .Pub file without Publisher Basically it is impossible. The aim is to: Simply convert the .Pub to .Pdf. Here is a link, for a website on which you can make this conversion for free: http://www.conv2pdf.com/ Just...
How to read and create a BIN and CUE file? ShowHow to read and create a BIN and CUE file? What is a BIN file? How to create BIN and CUE files How to burn BIN and CUE files How to read/convert BIN and CUE files What is a BIN file? A BIN and CUE file together create a binary...
What is an MKV file and how to read it ShowWhat is an MKV file and how to read it What is an MKV file? An MKV file is an audio and video container that can support various audio, video and subtitle compression formats. MKV files usually have a menu system slightly similar to a DVD menu,...
Transforming columns into lines ShowTransforming columns into lines Example Limitations It is endemic for most Linux tools to work with lines, but not with columns (sed, awk, grep, etc..). However, it may happens,that you have a file where the data should be read in...
Download Medieval Bluetooth OBEX File Transfer ShowMedieval Bluetooth OBEX File Transfer is an administrator for file transfer between your PC and your telephone, PDA, Palm or Notebook and through your Bluetooth connection.The management of files is done by drag and drop operations and you can view...
Linux - The shell ShowIntroduction to the shell The command interpreter is the interface between the user and the operating system, hence the name "shell". The shell therefore acts as an intermediary between the operating system and the user thanks to command lines...
Linux - User management ShowFirst step for the administrator When several people have access to a system, the administrator must manage the users. To do so, he must know the common commands and files to be configured. The important files are: the /etc/passwd file the...
UNIX system - The shell ShowIntroduction to the shell The command interpreter is the interface between the user and the operating system, hence its name "shell". The shell therefore acts as an intermediary between the operating system and the user using command lines...