简体   繁体   English

在linux中读取文件中每行的更好方法是什么?

[英]What is a better way to read each line in a file in linux?

cat  swerror.txt | while read Wrapper 
do
......
done

Hi all, I want to read each line in a file and then make a whole url to do some thing. 大家好,我想读取文件中的每一行,然后制作一个完整的URL来做一些事情。 The above scripts worked, but what is a better way to read file line by line? 上面的脚本有效,但是逐行读取文件的更好方法是什么? Thanks. 谢谢。

This is a nice example of the useless use of cat , simply use IO redirection: 这是无用的cat的一个很好的例子,只需使用IO重定向:

while read -r Wrapper 
do
    ...
done < swerror.txt

The use of -r in read is explained here . 这里解释read使用-r

It saves you the creation of a pipe and a process ( cat ) and will thus be more efficient. 它可以为您节省管道和进程( cat )的创建,从而提高效率。 Although as @TomFenech says, svn (the inner loop command you first gave) will probably be the dominant factor since it requires network traffic. 虽然@TomFenech说, svn (你首先给出的内循环命令)可能是主要因素,因为它需要网络流量。 And anyway, in most cases the runtime will not improve much by optimizing pipes, etc. 无论如何,在大多数情况下,通过优化管道等运行时不会有太大改善。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM