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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
while loop over for loop 7 119
sum67 challenge 35 128
Trying to run powershell  in a batch file. How do I do this? 8 123
Authenticate using sesu from script 7 159
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…
Suggested Courses

734 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