Shell逐行读取文件的4种方法

2019-09-23 09:31:13王旭

echo -e "method 1:"
echo -e "function while_read_bottm"
time while_read_bottm >> $TIMEFILE
echo -e "n"
echo -e "method 2:"
echo -e "function while_read_line "
time while_read_line >> $TIMEFILE
echo -e "n"
echo -e "method 3:"
echo "function while_read_line_fd"
time while_read_line_fd >>$TIMEFILE
echo -e "n"
echo -e "method 4:"
echo -e "function  for_in_file"
time  for_in_file >> $TIMEFILE

执行脚本后: [root@localhost shell]# ./while /scripts/bigfile
脚本输出内容:

method 1:
function while_read_bottm
real    0m5.689s
user    0m3.399s
sys    0m1.588s
method 2:
function while_read_line
real    0m11.612s
user    0m4.031s
sys    0m4.956s
method 3:
function while_read_line_fd
real    0m5.853s
user    0m3.536s
sys    0m1.469s
method 4:
function  for_in_file
real    0m5.153s
user    0m3.335s
sys    0m1.593s

下面我们对各个方法按照速度进行排序。

real    0m5.153s    method 4 (for 循环法)
real    0m5.689s    method 1  (while 釜底抽薪法)
real    0m5.853s    method 3    (标识符法)
real    0m11.612s  method 2    (管道法)

由此可见在各个方法中,for语句效率最高,而在while循环中读写文件时,

while read LINE
do
echo $LINE
done < $FILENAME

方式执行效率最高。