How to replace line-endings in VIM
line-endings, replace, vim
Solution
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.
Problem
How can I replace all line-endings in big file (>100MB)? I have tried to do `:%s/\n/, /g` but it's too slow.