我试着运行这个脚本:

for line in $(cat song.txt)
do echo "$line" >> out.txt
done

在Ubuntu11.04上运行
当“song.txt”包含:
I read the news today oh boy
About a lucky man who made the grade

运行脚本之后,“out.txt”看起来是这样的:
I
read
the
news
today
oh
boy
About
a
lucky
man
who
made
the
grade

有人能告诉我我做错了什么吗?

最佳答案

对于每行输入,应使用while read,例如:

cat song.txt | while read line
do
    echo "$line" >> out.txt
done

更好(更有效)的方法如下:
while read line
do
    echo "$line"
done < song.txt > out.txt

关于linux - 在读取文本文件时,shell是否会将<space>与<new line>混淆?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8093475/

10-09 21:13