[techtalk] vi technique for dos2unix

joey tsai joeytsai at cc.gatech.edu
Wed May 3 13:49:47 EST 2000


> The Quick'n'dirty way to do this in vi is as follows:
> 
> 1.  Open the dos text file in vi.
> 	vi dosfile.txt
> 2.  Type the following:
> 	:%s/^M//g
> ...where you "compose" the ^M character by pressing Ctrl+v then Ctrl+m
> 3.  Save the file:
> 	:w New.Unix.File.txt

If you have vim, you can just do
:set fileformat=unix
to format the EOLs to simply a newline.  To set it to the DOS EOL of
<CR><TL>, just set the fileformat to "dos".

But if you're like me and have a bunch of files with DOS EOLs, it's not the
smartest idea to open them all up in vi(m).  I don't have this dos2unix
program everyone's talking about, but you can achieve the same thing by
something like:

$ for f in *; do tr '\015' '\012' < $f > $f.new && mv $f.new $f; done

I suppose you might want to add a check for stray .new files, but I don't
think you'll have to worry about those unless Something Bad Happens.

	// jt






More information about the Techtalk mailing list