Code: Select all
#!/usr/bin/tclsh
proc out {text} {
puts stdout $text
}
proc usage {} {
global argv0
out "Usage: $argv0 \[InputFile\] \[OutputFile\] <MaxLen>"
out "\[InputFile\] : File to filter for duplicate or long entries"
out "\[OuputFile\] : File to create with filtered output"
out "<MaxLen> : If lines should be a maximum length, specify the limit here (optional). 0 or not given disables this"
exit
}
if {$argc < 2} { usage }
set in [lindex $argv 0]
set out [lindex $argv 1]
set max 0
if {$argc >= 3} { set max [lindex $argv 2] }
if {[regexp -- {[^0-9]} $max]} {
out "Invalid max length given"
usage
}
if {(![file exits $in]) || ([file exists $out])} {
out "Input file doesn't exist, or output is allready there"
usage
}
set fp [open $in r]
set buf [read $fp]
set outbuf [list]
close $fp
out "Scanning file"
set idx 1
foreach line $buf {
if {[lsearch -exact [lrange $buf $idx end] $line] >= 0} {
incr idx
continue
}
if {($max) && ([string length $line] > $max)} {
incr idx
continue
}
lappend outbuf $line
incr idx
}
out "Writting output buffer"
se fp [oepn $out w]
foreach a $outbuf {
puts $fp $a
}
close $fp
out "Output complete. Discarded [expr [llength $buf] - [llength $outbuf]] record(s)"
The above two are different.This is a test by PPSlim!
This is a test by ppslim!
Code: Select all
[irc@delta irc]$ tclsh sort.tcl trivia.wri result.wri 0
bad option "exits": must be atime, attributes, channels, copy, delete, dirname, executable, exists, extension, isdirectory, isfile, join, lstat, mtime, mkdir, nativename, owned, pathtype, readable, readlink, rename, rootname, size, split, stat, tail, type, volumes, or writable
while executing
"file exits $in"
(file "sort.tcl" line 1)
Code: Select all
[irc@delta irc]$ tclsh sort.tcl trivia.wri result.wri 0
Scanning file
list element in quotes followed by ";" instead of space
while executing
"foreach line $buf {
if {[lsearch -exact [lrange $buf $idx end] $line] >= 0} {
incr idx
continue
}
if {($max) && ([string length $line] > $max)} {..."
(file "sort.tcl" line 39)
Code: Select all
se fp [oepn $out w]
Code: Select all
set fp [open $out w]
Code: Select all
#!/usr/bin/tclsh
proc out {text} {
puts stdout $text
}
proc usage {} {
global argv0
out "Usage: $argv0 \[InputFile\] \[OutputFile\] <MaxLen>"
out "\[InputFile\] : File to filter for duplicate or long entries"
out "\[OuputFile\] : File to create with filtered output"
out "<MaxLen> : If lines should be a maximum length, specify the limit here (optional). 0 or not given disables this"
exit
}
if {$argc < 2} { usage }
set in [lindex $argv 0]
set out [lindex $argv 1]
set max 0
if {$argc >= 3} { set max [lindex $argv 2] }
if {[regexp -- {[^0-9]} $max]} {
out "Invalid max length given"
usage
}
if {(![file exists $in]) || ([file exists $out])} {
out "Input file doesn't exist, or output is allready there"
usage
}
set fp [open $in r]
set buf [split [read $fp] \n]
set outbuf [list]
close $fp
out "Scanning file"
set idx 1
foreach line $buf {
if {[lsearch -exact [lrange $buf $idx end] $line] >= 0} {
incr idx
continue
}
if {($max) && ([string length $line] > $max)} {
incr idx
continue
}
lappend outbuf $line
incr idx
}
out "Writting output buffer"
se fp [open $out w]
foreach a $outbuf {
puts $fp $a
}
close $fp
out "Output complete. Discarded [expr [llength $buf] - [llength $outbuf]] record(s)"
Code: Select all
se fp [open $out w]
Code: Select all
set fp [open $out w]