Lire un fichier

Method 1: PIPED while-read loop

#!/bin/bash
FILENAME=$1
count=0
cat $FILENAME | while read LINE
do
       let count++
       echo "$count $LINE"
done

echo -e "\nTotal $count Lines read"

 

Method 2: Redirected “while-read” loop

#!/bin/bash
FILENAME=$1
count=0

while read LINE
do
      let count++
      echo "$count $LINE"

done < $FILENAME

echo -e "\nTotal $count Lines read"

 

Method 3:while read LINE Using File Descriptors

#!/bin/bash
FILENAME=$1
count0=
exec 3<&0
exec 0< $FILENAME

while read LINE
do
        let count++
        echo "$count $LINE"
done

exec 0<&3
echo -e "\nTotal $count Lines read"

 

Method 4: Process file line by line using awk

#!/bin/bash
FILENAME=$1

awk '{kount++;print  kount, $0}
     END{print "\nTotal " kount " lines read"}' $FILENAME

 

Time Comparison

# du -h bigfile.txt
70M     bigfile.txt
# wc -l bigfile.txt
900000 bigfile.txt

 

# time ./method1.sh bigfile.txt >/dev/null
real    6m2.911s
user    2m58.207s
sys     2m58.811s

# time ./method2.sh bigfile.txt > /dev/null
real    2m48.394s
user    2m39.714s
sys     0m8.089s

# time ./method3.sh bigfile.txt > /dev/null
real    2m48.218s
user    2m39.322s
sys     0m8.161s

# time ./method4.sh bigfile.txt > /dev/null
real    0m2.054s
user    0m1.924s
sys     0m0.120s

 

Source de cet article : http://www.bashguru.com/2010/05/how-to-read-file-line-by-line-in-shell.html
(Article complet avec explications, mais aussi plein de pubs^^)

Leave a Reply