command line changing of fileformat

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

command line changing of fileformat

jkilbour
I have a set of large text files with UNIX line endings that I would like
to chnage into files with dos line endings. Is there a more automated
approach than opening each one and typing ":set fileformat=dos"? Some of
the files are so large they take a few minutes to open.

Thanks


Reply | Threaded
Open this post in threaded view
|

Re: command line changing of fileformat

Stephen R Laniel
On Mon, Aug 22, 2005 at 03:57:27PM -0400, [hidden email] wrote:
> I have a set of large text files with UNIX line endings that I would like
> to chnage into files with dos line endings. Is there a more automated
> approach than opening each one and typing ":set fileformat=dos"? Some of
> the files are so large they take a few minutes to open.

I don't know of a vim way to do this over a large set of
files, but I'm pretty sure the following trick will do it.
Assume all the files you want are stored in the directory
~/foo, and they all end in .txt. Then do

find ~/foo -type f -iname '*.txt' -exec sed -i -e 's/\n/\r\n/g' '{}' \;

This will use sed to convert every instance of '\n' (the
UNIX line-ending) to '\r\n' (the DOS one).

I'm nearly certain that will do what you want, but it's
untested. So make a backup copy of your files first. :-)

--
Stephen R. Laniel
[hidden email]
+(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: command line changing of fileformat

A.J.Mechelynck
In reply to this post by jkilbour
----- Original Message -----
From: <[hidden email]>
To: <[hidden email]>
Sent: Monday, August 22, 2005 9:57 PM
Subject: command line changing of fileformat


>I have a set of large text files with UNIX line endings that I would like
> to chnage into files with dos line endings. Is there a more automated
> approach than opening each one and typing ":set fileformat=dos"? Some of
> the files are so large they take a few minutes to open.
>
> Thanks

    :set nohidden autowriteall
    " replace the following line by whatever you need
    " if the files to change are not "all files in the current dir"
    :args *
    :argdo set ff=dos
    :x

see
    :help :args
    :help :argdo
    :help 'autowriteall'


Best regards,
Tony.

Reply | Threaded
Open this post in threaded view
|

Re: command line changing of fileformat

Wes Potts
In reply to this post by jkilbour
If you have access to a linux (or Unix) machine, check out the commands:

unix2dos
mcopy (part of the mtools package)

I would be surprised if you could not find unix2dos or a similar tool
for Windows.

Wes

On 8/22/05, [hidden email] <[hidden email]> wrote:
> I have a set of large text files with UNIX line endings that I would like
> to chnage into files with dos line endings. Is there a more automated
> approach than opening each one and typing ":set fileformat=dos"? Some of
> the files are so large they take a few minutes to open.
>
> Thanks
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: command line changing of fileformat

Tim Chase-2
In reply to this post by Stephen R Laniel
> find ~/foo -type f -iname '*.txt' -exec sed -i -e 's/\n/\r\n/g' '{}' \;

While Tony gave the pretty straight-forward Vim solution,
Stephen's suggestion of using Sed might be a tad kinder on system
resources if you've got lots of massive files.

I had some trouble with Stephen's sed statement but managed to
get this to work:

        sed -i 's/$/\r/'

If you want backups of the originals, you can use

        sed -i.bak 's/$/\r/'

which will create backups as *.bak (or your readily replaceable
extension of choice).

You can use the "find" syntax above, or at least GNU sed is smart
enough to properly handle filespecs, so you can just do

        sed -i.bak 's/$/\r/' *.txt

It doesn't descend into subdirs like the above "find" command
does, but it's a little less cumbersome to type.

To strip them back out, you can either use "tr" if you want to
blow away all the <CR> characters:

        tr -d '\r' < infile > outfile

or if you want to ensure that you're not blowing away embedded
<CR> while still nuking the EOL usage, you can use sed again:

        sed -i 's/\r$//' *.txt

Just a few other ideas to add to the mix.

-tim