如何替换VIM中的行结尾

Fra*_*dan 8 vim replace line-endings

如何替换大文件中的所有行结尾(> 100MB)?我试过这样做

:%s/\n/, /g

但它太慢了.

Sea*_*nor 8

所以,我经历了测试/计时其他人给出的一些答案,以及我自己的python答案.这是我得到的:

TR:

> time tr "\n" "," < lines > line
real    0m1.617s
user    0m0.100s
sys     0m1.520s
Run Code Online (Sandbox Code Playgroud)

蟒蛇:

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

AWK:

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

perl的:

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

SED:

> time sed 's/$/, /g' lines > line                                             
real    0m6.673s
user    0m5.050s
sys     0m1.630s
Run Code Online (Sandbox Code Playgroud)

这是我使用的文件:

> 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
Run Code Online (Sandbox Code Playgroud)

最初的时间是在cygwin中进行的,现在它们已经完全更新了ubuntu 9.10.此外,文本文件大小增加到100兆,线宽80个字符.你可以看到除了sed之外的任何东西都是个好主意.

  • 我非常怀疑你的awk结果.你指挥几次,而不只是一次.考虑到导入模块和东西需要时间,Python不应该比awk快 (2认同)