如何在 VIM 中替换行结尾

发布于 2024-08-24 00:41:59 字数 86 浏览 2 评论 0原文

如何替换大文件(>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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

月竹挽风 2024-08-31 00:41:59

因此,我浏览并测试/计时了其他人给出的一些答案,以及我自己的 python 答案。这是我得到的:

tr:

> time tr "\n" "," < lines > line
real    0m1.617s
user    0m0.100s
sys     0m1.520s

python:

> time python -c 'import sys; print sys.stdin.read().replace("\n",", "),' < lines > line
real    0m1.663s
user    0m0.060s
sys     0m1.610s

awk:

> time awk '{printf("%s, ", $0)}' lines > line                                 
real    0m1.998s
user    0m0.390s
sys     0m1.600s

perl:

> time perl -e 'while (<>) { chomp; print "$_, " }' lines > line
real    0m2.100s
user    0m0.590s
sys     0m1.510s

sed: >

> time sed 's/$/, /g' lines > line                                             
real    0m6.673s
user    0m5.050s
sys     0m1.630s

这是我使用的文件:

> ls -lh lines
-rw-r--r-- 1 some one 101M 2010-03-04 19:54 lines
> wc -l < lines
1300000
> head -n 3 < lines
The pretty pink puma pounced on the unsuspecting aardvark, the scientist watched.
The pretty pink puma pounced on the unsuspecting aardvark, the scientist watched.
The pretty pink puma pounced on the unsuspecting aardvark, the scientist watched.
> head -n 1 < lines | wc -c
82

最初的计时是在 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:

> time tr "\n" "," < lines > line
real    0m1.617s
user    0m0.100s
sys     0m1.520s

python:

> time python -c 'import sys; print sys.stdin.read().replace("\n",", "),' < lines > line
real    0m1.663s
user    0m0.060s
sys     0m1.610s

awk:

> time awk '{printf("%s, ", $0)}' lines > line                                 
real    0m1.998s
user    0m0.390s
sys     0m1.600s

perl:

> time perl -e 'while (<>) { chomp; print "$_, " }' lines > line
real    0m2.100s
user    0m0.590s
sys     0m1.510s

sed:

> time sed 's/$/, /g' lines > line                                             
real    0m6.673s
user    0m5.050s
sys     0m1.630s

Here is the file I used:

> ls -lh lines
-rw-r--r-- 1 some one 101M 2010-03-04 19:54 lines
> wc -l < lines
1300000
> head -n 3 < lines
The pretty pink puma pounced on the unsuspecting aardvark, the scientist watched.
The pretty pink puma pounced on the unsuspecting aardvark, the scientist watched.
The pretty pink puma pounced on the unsuspecting aardvark, the scientist watched.
> head -n 1 < lines | wc -c
82

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.

夏の忆 2024-08-31 00:41:59

:%s/$/, / 后跟 :1,$j 可能会更快。否则,请在外部实用程序中执行此操作:

perl -e 'while (<>) { chomp; print "$_, " }' input_file > output_file

awk '{printf("%s, ", $0)}' input_file > output_file

我不知道哪个最快。

:%s/$/, / followed by a :1,$j might be faster. Otherwise, do it in an external utility:

perl -e 'while (<>) { chomp; print "$_, " }' input_file > output_file

awk '{printf("%s, ", $0)}' input_file > output_file

Don't know off the top of my head which would be fastest.

你列表最软的妹 2024-08-31 00:41:59

使用此 Perl 脚本浏览您的文件;它比用 VIM 将所有内容保存在内存中要快。只需将输出通过管道传输到新文件即可。

#!/usr/local/bin/perl

while (<>) {
  $_ =~ s/\n/,/g;
  print $_;
}

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.

#!/usr/local/bin/perl

while (<>) {
  $_ =~ s/\n/,/g;
  print $_;
}
天涯沦落人 2024-08-31 00:41:59

你必须在vim中这样做吗?

有一个很好的 Unix 实用程序可以进行基于字符的翻译。它称为tr
一些参考

在你的情况下,它将是:

tr "\n" "," < input_file > output_file

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:

tr "\n" "," < input_file > output_file
香草可樂 2024-08-31 00:41:59

最好的工具是 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

无声情话 2024-08-31 00:41:59
$ more file
aaaa
bbbb
cccc
dddd
eeee

$ awk 'NR>1{printf("%s, ", p)}{p=$0}END{print p}' file
aaaa, bbbb, cccc, dddd, eeee

$ sed -e :b -e '$!N;s/\n/, /;tb' file
$ more file
aaaa
bbbb
cccc
dddd
eeee

$ awk 'NR>1{printf("%s, ", p)}{p=$0}END{print p}' file
aaaa, bbbb, cccc, dddd, eeee

$ sed -e :b -e '$!N;s/\n/, /;tb' file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文