Solved

How to work with files on TCL.

Posted on 2004-09-17
10
474 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
Comment Utility
Anyone? hello.
Looks like everyone is good in the C, HTML, Java etc sections not here.
0
 
LVL 2

Expert Comment

by:rajeshp_69
Comment Utility
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
Comment Utility
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
 
LVL 24

Expert Comment

by:fridom
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 24

Expert Comment

by:fridom
Comment Utility
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
Comment Utility
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
Comment Utility
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 400 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 88
Strange date stored 22 99
Tab names are off by one 5 49
Change owner from userA to server Local Administrators 7 51
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: 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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now