文件中的行回显

6
我有一个名为“myfile.txt”的文件,它包含以下内容:
hola mundo
hello word

我希望您能够与每一行代码一起工作。

for i in `cat myfile.txt`; do echo $i; done

i hope this give me

hola mundo
hello word

首先是一行,然后是另一行,但要获取

hola
mundo
hello
word

我可以要求将结果显示到换行符而不是每个空格吗?

谢谢大家

1个回答

13

好了许多

cat myfile.txt | while read line; do
    echo "$line"
done

或者更好的是(不会启动其他进程,例如子shell和cat):

while read line; do
    echo "$line"
done < myfile.txt

如果你更喜欢一行代码,显然就是:

while read line; do echo "$line"; done < myfile.txt

在第二个版本中,为什么要在结尾添加文件名?它是如何工作的?特别是最后一行 done < myfile.txt - Sri Hari Vignesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接