[英]loop in the bash shell
I read a file like this: 我读了这样的文件:
while read line; do
echo "$line"
done < file.txt
Sometimes, I can't read the last line. 有时,我看不到最后一行。
Open the file with VIM, and exec ":set list", then I can see a "$" at the end of each line. 用VIM打开文件,然后执行“:set list”,然后我可以在每行的结尾看到“ $”。
But, when I exec "cat file.txt": 但是,当我执行“ cat file.txt”时:
$ cat file.txt
aa
bb$
The last line "bb" and the "$" are in the same line. 最后一行“ bb”和“ $”在同一行。
I think it's a problem. 我认为这是一个问题。
How can I use "while" to read all lines? 如何使用“ while”读取所有行?
ps:Can vim show newline like \\n and return like \\r? ps:vim可以像\\ n一样显示换行符,而像\\ r一样返回吗?
I'm a beginner, help! 我是新手,请帮助!
It is most likely because of missing \\n
from end of line OR presence of \\r
in a line. 这很可能是因为行尾缺少\\n
或行中\\r
存在。
You can test it by this command: 您可以通过以下命令对其进行测试:
cat -vte file
To remove \\r
from end use this sed: 要从终端删除\\r
,请使用以下sed:
sed -i.bak 's/'$'\r''$//' file
The way Vim alerts you to this condition (a missing newline ( \\n
) character in the last line) is during opening, where [noeol]
will be in the message. Vim会在打开期间提醒您这种情况(最后一行缺少换行符 ( \\n
)字符),而消息中会出现[noeol]
。
:edit foo
"foo" [noeol] 3L, 42C
You can also check later via 您也可以稍后通过查看
:setlocal eol?
To fix this retroactively (meaning you cannot modify the file's source to avoid creating such files), just :write
the file from Vim; 要追溯解决此问题(意味着您不能修改文件的源文件以避免创建此类文件),只需:write
从Vim中:write
文件; it'll insert the final newline automatically. 它将自动插入最后的换行符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.