Search code examples
windowstext-filesdos2unix

What's the best way of doing dos2unix on a 500k line file, in Windows?


Question says it all, I've got a 500,000 line file that gets generated as part of an automated build process on a Windows box and it's riddled with ^M's. When it goes out the door it needs to *nix friendly, what's the best approach here, is there a handy snippet of code that could do this for me? Or do I need to write a little C# or Java app?


Solution

  • Here is a Perl one-liner, taken from http://www.technocage.com/~caskey/dos2unix/

    #!/usr/bin/perl -pi
    s/\r\n/\n/;

    You can run it as follows:

    perl dos2unix.pl < file.dos > file.unix
    

    Or, you can run it also in this way (the conversion is done in-place):

    perl -pi dos2unix.pl file.dos
    

    And here is my (naive) C version:

    #include <stdio.h>
    
    int main(void)
    {
       int c;
       while( (c = fgetc(stdin)) != EOF )
          if(c != '\r')
             fputc(c, stdout);
       return 0;
    }
    

    You should run it with input and output redirection:

    dos2unix.exe < file.dos > file.unix