HavenRock.com: home
 
Asia ... Documentation ... Downloads ... DSSSL ... EFL/ESL ... HTML tools ... Japanese text ... Linux ... Photography ... Python ... SGML ... Tcl/Tk ... TeX ... Tips ... Typography ... Word processing

A 6-way Tcl script

This first suggestion is my own. It was originally posted as a "2-cent Tip" in the August, 1998 issue of the Linux Gazette.

Date: Thu, 30 Jul 1998 14:28:37 +0900
From: Matt Gushee, mgushee@havenrock.com

Well, I had some text files that I needed to convert from UNIX to DOS format. Downloaded the 'unix2dos' program ... and discovered to my horror that it was an A.OUT BINARY! Thought they'd purged all of those from the archives ;-) But seriously, I couldn't run the program, so I came up with a Tcl script to do the job. It can convert text files in any direction between UNIX, DOS and Mac formats. It has only been tested w/ Tcl 8.0, but since it's very simple, I imagine it'll work with earlier versions too. It has a small bug: when converting from DOS format, it adds one extra newline at the end of the file.

Why Tcl? Well ...

  • I'm sure it's possible to do these conversions with sed or even bash; it might even be simple once you know the trick. But after several hours of reading man pages and experimenting, I couldn't figure out how.
  • Didn't feel like dusting off my old Perl book.
  • Tcl is cool.
  • It turned out to be really easy to do this in Tcl.

To use the script, you should:

  1. If necessary, edit the pathname for tclsh.
  2. Save it wherever you want to, with any name (I call it textconv.tcl), and make it executable.
  3. symlink it to any or all of the following names, depending on which conversions you want to do, in a directory in $PATH:
    d2m	   d2u	  m2d	 m2u	u2d    u2m
	      

These names must be exactly as shown in order for the script to work.

  • To use, type the appropriate command with a source file and destination file as arguments. For example, to convert a Mac text file to UNIX format:
    $ m2u macintosh.txt unix.txt
	      

That's it! Hope you find it useful.

  ------ cut below this line ---------------------
  #!/usr/bin/tclsh

  # capture the command name that invoked us and the
  # source and destination filenames
  set convtype $argv0
  set infile [lindex $argv 0]
  set outfile [lindex $argv 1]

  set inchannel [open $infile "r"]
  set outchannel [open $outfile "w"]

  # according to the command name, set the\
  # end-of-line and end-of-file characters to the\
  # appropriate values
  switch -glob -- $convtype {

     *2d {
	   fconfigure $outchannel -translation \
          "crlf" -eofchar "\x1a"
     }

     *2m {
	   fconfigure $outchannel -translation cr
     }

     *2u {
 	   fconfigure $outchannel -translation lf \
             -eofchar ""
     }

     default {
	   error "Invalid command name. This script \
     must be\n invoked through a symbolic link with\n\
     one of the following names:\n d2m, d2u, m2d, \
     m2u, u2d, or u2m."
     }
  
  }

  while {[gets $inchannel line] >= 0} {

  # if converting from DOS, \
  # lose the end-of-file character
     if {[string match "*d2*" $convtype]} {
	   set line [string trimright $line "\x1a"]
     }

     puts $outchannel $line

  }

  close $inchannel
  close $outchannel
  #------------ end Tcl script-----------------
	      

	      
Matt Gushee

Last modified: Sun Oct 17 11:26:15 EDT 1999