如果我打开在Windows中创建的文件,所有行都以^M结尾。如何一次删除这些字符?
当前回答
在VIM中:
:e ++ff=dos | set ff=unix | w!
带VIM的外壳:
vim some_file.txt +'e ++ff=dos | set ff=unix | wq!'
e++ff=dos-强制以dos格式打开文件。
set ff=unix-将文件转换为unix格式。
其他回答
以下步骤可以将DOS的文件格式转换为Unix:
:e ++ff=dos Edit file again, using dos file format ('fileformats' is ignored).[A 1]
:setlocal ff=unix This buffer will use LF-only line endings when written.[A 2]
:w Write buffer using Unix (LF-only) line endings.
参考:文件格式
在VIM中:
:e ++ff=dos | set ff=unix | w!
带VIM的外壳:
vim some_file.txt +'e ++ff=dos | set ff=unix | wq!'
e++ff=dos-强制以dos格式打开文件。
set ff=unix-将文件转换为unix格式。
我通常使用
:%s/\r/\r/g
这看起来有点奇怪,但由于Vim与linefeed的匹配方式,它起到了作用。我也觉得更容易记住:)
我想要用换行符代替^M。Perl拯救:
perl -pi.bak -e 's/\x0d/\n/g' excel_created.txt
或写入标准输出:
perl -p -e 's/\x0d/\n/g' < excel_created.txt
:%s/\r\+//g
在Vim中,它去掉所有回车符,只留下换行符。