Shell逐行读取文件的3种方法 | 您所在的位置:网站首页 › shell读取配置文件并解析放入数组的命令是什么 › Shell逐行读取文件的3种方法 |
方法1:while循环中执行效率最高,最常用的方法。 while read linedoecho $linedone < filename注释:这种方式在结束的时候需要执行文件,就好像是执行完的时候再把文件读进去一样。
方法2 : 管道法: cat $FILENAME | while read LINE cat filename | while read linedoecho $linedone注释:当遇见管道的时候管道左边的命令的输出会作为管道右边命令的输入然后被输入出来。
方法3 for 循环。 for line in `cat filename`doecho ${line}done注释:这种方式是通过for循环的方式来读取文件的内容相比大家很熟悉了,这里不多说。
在各个方法中,for语句效率最高,而在while循环中读写文件时,第一种方式执行效率最高。
for逐行读和while逐行读是有区别的,如: $ cat t.txt111122223333 4444 555$ cat t.txt | while read line; do echo ${line}; done111122223333 4444 555 $ for line in `cat t.txt`; do echo ${line}; done1111222233334444555 |
CopyRight 2018-2019 实验室设备网 版权所有 |