|
|
Asia ... Documentation ... Downloads ... DSSSL ... EFL/ESL ... HTML tools ... Japanese text ... Linux ... Photography ... Python ... SGML ... Tcl/Tk ... TeX ... Tips ... Typography ... Word processing |
Document Processing
Text conversion:
1 /
2 /
3 /
4 /
5 /
6 /
7
|
A 6-way Tcl scriptThis first suggestion is my own. It was originally posted as a "2-cent Tip" in the August, 1998 issue of the Linux Gazette. Visit the Linux Gazette
Date: Thu, 30 Jul 1998 14:28:37 +0900 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 ...
To use the script, you should:
d2m d2u m2d m2u u2d u2m
These names must be exactly as shown in order for the script to work.
$ 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-----------------
Next: How-to 2 -- The answer is Perl |
|
|
Matt Gushee
Last modified: Sun Oct 17 11:26:15 EDT 1999 |