- Bash Script: How read file line by line (best and worst way)

There are many-many way to read file in bash script, look at the first section where I used while loop along with pipe (|) (cat $FILE | while read line; do ... ) and also incremented the value of (i) inside the loop and at the end I am getting the wrong value of i, the main reason is that the usage of pipe (|) will create a new sub-shell to read the file and any operation you do withing this while loop (example - i++) will get lost when this sub-shell finishes the operation.

In second and the worst method, One of the most common errors when reading a file line by line is by using a for loop (for fileline in $(cat $FILE);do ..), which prints the every word in a file on separate line, because, for loop uses the default value of IFS (space).

In third perfect method, The while loop (while read line;do .... done < $FILE) is the most appropriate and easiest way to read a file line by line, see the example below.

Input: $ cat sample.txt
This is sample file
This is normal text file

Source: $ cat readfile.sh
#!/bin/bash

i=1;
FILE=sample.txt

# Wrong way to read the file.
# This may cause problem, check the value of 'i' at the end of the loop
echo "###############################"
cat $FILE | while read line; do
        echo "Line # $i: $line"
        ((i++))
done
echo "Total number of lines in file: $i"

# The worst way to read file.
echo "###############################"
for fileline in $(cat $FILE);do
        echo $fileline
done

# This is correct way to read file.
echo "################################"
k=1
while read line;do
        echo "Line # $k: $line"
        ((k++))
done < $FILE
echo "Total number of lines in file: $k"

Continue Reading...


source:http://linuxpoison.blogspot.com/2012/08/135781677518171.html