How to work with files on TCL.

Posted on 2004-09-17
Last Modified: 2013-12-26
I need to know how to build tcl script for an eggdrop which utilizes file manipulation. The job being, to be able to match a certain word from a .txt file and display the line in which the word matched, or if many lines matched display the line numbers which lines matched.

Also how to read and delete specific lines from the database .txt file. Such as reading/deleting line no. 2 from the file or line 31 and etc. Like how to split the data read from the file, in how in many bytes? What is EOF? how to use it.

#I know:
set file [open $myfile "r"]
[gets $file]

#Would read only line 1, but how to read the others.

Question by:awwyeah
  • 6
  • 3

Author Comment

ID: 12091558
Anyone? hello.
Looks like everyone is good in the C, HTML, Java etc sections not here.

Expert Comment

ID: 12100626

You can access the following URL and u'll get a host of scripts useful for your purpose.


Author Comment

ID: 12103595
LOL! The owner of that site is a friend of mine and I post scripts there and on when I release them publically. I am good with lots of tcl functions only one of which I am very bad with file handling and manipulation.
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 24

Expert Comment

ID: 12149778
Well that is exactly for what Tcl/Tk is used often. Now I answer your question on how to read all the lines in a file
set file [ open "/etc/hosts" r]
while { [gets $file line]  >= 0 } {
    puts $line
close $file

What you need is a loop around the gets or you may consider using read to read in all the other bytes

If you want to match a line you have to use whatever it takes and work with $line in the given example e.g
if { $argc != 2 } {
    error "Syntax: t2.tcl <pattern> <filename>"
set file [open [lindex $argv 1] r]
set pattern [lindex $argv 0]

while {[gets $file line] >= 0}  {
    if [regexp $pattern $line] {
        puts $line
close $file

If you want to delete lines with a specific number you need to count the read line and write out those which you do not want to delete.

for I/O see e.g.

may be that you are interested in

I suggest however to search the web for tcl tutorials or the like and visit comp.lang.tcl



Author Comment

ID: 12150413
I think for the matching we should use 'lrange' instead of 'lindex' don't you agree?
List index will only try to match the first word of the line with my pattern, while we want match any word in the whole line.

set pattern [lrange $argv 0 end]

Thanks for that, how about reading a specific line? the most simplest one.
Not reading line 1 obviously its just [gets]. Reading a random line can be done by [rand limit]

But how to read line #11, #24? #378? or any line. The logic being checking first if that line exists, then only go ahead and reading it, else return.
LVL 24

Expert Comment

ID: 12150865
No I think you misunderstod what assignment to pattern does, it just looks into the first parameter of the command line. And that is what you want to use.

Reading a specific line can just be done incremental. You have to count the newlines and stop while reaching the line you want. However you may ask yourself if you must read line by line or if you can read larger chunks.


Author Comment

ID: 12154186
Yeah thanks. Actually I think I would need a for loop to count the number of lines.

for {set i 0} {incr i}
Something like this or even a while loop.

But then again how, to check if it is that line?
Would we use

!read 45

#Where !read is the trigger and the no after that is the line to be read.

### CODE ###
set database "mydata.txt"
set file [open $database "r"]
set line [lindex $arg 0]

while {([gets $file line] == [expr $line - 1])}  {
    putserv "PRIVMSG $chan :$line" -next

It would be something like this, if I am correct?

Author Comment

ID: 12154192
And how to check, if that line number exists?
Like to check if a file to exist we use:

set myfile "database.txt"
if {[file exists $myfile]} { # do stuff }

Maybe something like:
if {([gets $file line] != "")} { # do stuff }

LVL 24

Accepted Solution

fridom earned 400 total points
ID: 12157522
To read a specific line something along this should do:

if { $argc != 2} {
    error "Syntax: t3.tcl <filename> <line>"
set file [open [lindex $argv 0] r]
set line [lindex $argv 1]

set line_counter 0
while { [gets $file read_line] >= 0 } {
    incr line_counter
    if ([expr $line_counter == $line ]) {
        puts $read_line

If you do not get output then you know that there is not a number with that line

Author Comment

ID: 12157598
Thanks for the help, I will try to implement it!

By the way you seem good with tcl, can you also consider this post, no one has answered it.

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Whole sheet autoscrub still needed 19 51
lucky13 challenge 11 154
fix34  challenge 9 142
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 846
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question