如何在 VIM 中替换行结尾
如何替换大文件(>100MB)中的所有行结尾? 我尝试过
:%s/\n/, /g
但它太慢了。
How can I replace all line-endings in big file (>100MB)?
I have tried to do
:%s/\n/, /g
but it's too slow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因此,我浏览并测试/计时了其他人给出的一些答案,以及我自己的 python 答案。这是我得到的:
tr:
python:
awk:
perl:
sed: >
这是我使用的文件:
最初的计时是在 cygwin 中进行的,现在已使用完全更新的 ubuntu 9.10 进行计时。此外,文本文件大小增加到 100 兆,行宽 80 个字符。正如您所看到的,除了 sed 之外的任何东西都是个好主意。
So, I went through and tested/timed some of the answers that were given by other people, plus a python answer of my own. Here is what I got:
tr:
python:
awk:
perl:
sed:
Here is the file I used:
Originally the timings were taken in cygwin, they have now been taken with fully updated ubuntu 9.10. Also, the text files size was increased to 100 megs, with lines 80ish characters wide. As you can see pretty much anything other than sed is a good idea.
:%s/$/, /
后跟:1,$j
可能会更快。否则,请在外部实用程序中执行此操作:我不知道哪个最快。
:%s/$/, /
followed by a:1,$j
might be faster. Otherwise, do it in an external utility:Don't know off the top of my head which would be fastest.
使用此 Perl 脚本浏览您的文件;它比用 VIM 将所有内容保存在内存中要快。只需将输出通过管道传输到新文件即可。
Use this Perl script to go through your file; it'd be faster than holding everything in memory with VIM. Just pipe output to a new file.
你必须在vim中这样做吗?
有一个很好的 Unix 实用程序可以进行基于字符的翻译。它称为
tr
。一些参考。
在你的情况下,它将是:
Do you have to do this in vim?
There is nice Unix utility that does character based translation. It'c called
tr
.Some reference.
In your case it would be:
最好的工具是 sed,您可以将它与 :! 一起使用。命令
所以使用
:!sed -e 's/\n/,/g' % > %.tmp;猫 %.tmp > %; rm %.tmp'
在集成到当前文件之前,您需要创建一个进行更改的 tmp 文件
The best tool is sed and you can use it with :! command
so use
:!sed -e 's/\n/,/g' % > %.tmp ; cat %.tmp > % ; rm %.tmp'
You need create a tmp file with change before integrate in your current file