How to work with files on TCL.

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.

--
awyeah
awwyeahAsked:
Who is Participating?
 
fridomConnect With a Mentor Commented:
To read a specific line something along this should do:

#!/usr/bin/tclsh
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
        break
    }
}

If you do not get output then you know that there is not a number with that line
Regards
Friedrich
0
 
awwyeahAuthor Commented:
Anyone? hello.
Looks like everyone is good in the C, HTML, Java etc sections not here.
0
 
rajeshp_69Commented:
Hi,

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

http://www.tclscript.com/links.shtml

Bye
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
awwyeahAuthor Commented:
LOL! The owner of that site is a friend of mine and I post scripts there and on egghelp.org 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.
0
 
fridomCommented:
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
#!/usr/bin/tclsh
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
#!/usr/bin/tclsh
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.
 http://hegel.ittc.ukans.edu/topics/tcltk/tutorial-noplugin/tutorial-11.html

may be that you are interested in
http://www.msen.com/~clif/TclTutor.html

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

Regards
Friedrich



0
 
awwyeahAuthor Commented:
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.
0
 
fridomCommented:
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.

Regards
Friedrich
0
 
awwyeahAuthor Commented:
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

INPUT:
!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?
0
 
awwyeahAuthor Commented:
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 }

0
 
awwyeahAuthor Commented:
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.
http://www.experts-exchange.com/Programming/Programming_Platforms/Unix_Programming/Q_21134808.html
0
All Courses

From novice to tech pro — start learning today.