[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to work with files on TCL.

Posted on 2004-09-17
10
Medium Priority
?
532 Views
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.

--
awyeah
0
Comment
Question by:awwyeah
  • 6
  • 3
10 Comments
 

Author Comment

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

Expert Comment

by:rajeshp_69
ID: 12100626
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
 

Author Comment

by:awwyeah
ID: 12103595
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
Industry Leaders: 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

by:fridom
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
#!/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
 

Author Comment

by:awwyeah
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.
0
 
LVL 24

Expert Comment

by:fridom
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.

Regards
Friedrich
0
 

Author Comment

by:awwyeah
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

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
 

Author Comment

by:awwyeah
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 }

0
 
LVL 24

Accepted Solution

by:
fridom earned 1200 total points
ID: 12157522
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
 

Author Comment

by:awwyeah
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.
http://www.experts-exchange.com/Programming/Programming_Platforms/Unix_Programming/Q_21134808.html
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

873 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