Version 1 of dos2unix

Updated 2003-04-25 17:39:33

GPS @ April 25 2003 - A dos2unix script is usually used to convert the end of line characters in text files from DOS/Windows format to a Unix-like format. Tcl can do automatic conversion of line endings, but I also need the conversion done in Windows, because I use xedit in Cygwin XFree86, so I came up with this short script.

 #!/bin/tclsh8.4

 foreach f $::argv {
        puts ${f}...
        set fd [open $f r]
        fconfigure $fd -translation binary
        set data [read $fd]
        close $fd

        set data [string map {"\r\n" "\n"} $data]

        set fd [open $f w]
        fconfigure $fd -translation binary
        puts -nonewline $fd $data
        close $fd
 }

There are also ways of doing this with the tr utility, sed, and probably others...

KPV Below is a generalization of the above script to automatically convert from Unix, Macintosh or Windows lineend format into the native one.

 #!/bin/tclsh8.4

 if {$tcl_platform(platform) == "windows"} {
    set eol "\r\n"
 } elseif {$tcl_platform(platform) == "unix"} {
    set eol "\n"
 } else {
    set eol "\r"
 }
 set eolmap [list "\r\n" $eol "\n" $eol "\r" $eol]

 foreach f $::argv {
    puts ${f}...
    set fd [open $f r]
    fconfigure $fd -translation binary
    set data [read $fd]
    close $fd

    set data [string map $eolmap $data]

    set fd [open $f w]
    fconfigure $fd -translation binary
    puts -nonewline $fd $data
    close $fd
 }