Version 2 of How do I remove one line from a file?

Updated 2006-12-29 05:00:03 by lwv

Well, you could use sed. Or you could use something like the following code.


    set tmpname /tmp/something

    set source [open $filename]
    set destination [open $tmpname w]
    set content [read $source]
    set lines [split $content \n]
    set lines_after_deletion \
       [lreplace $lines $line_number_to_remove $line_number_to_remove]
    puts -nonewline $destination [join $lines_after_deletion \n]
    close $source
    close $destination
    file rename -force $destination $source

Note, however, that in both of these cases, what you are doing are reading the entire file and writing out the entire file, counting lines.


Category Example