| View previous topic :: View next topic |
| Author |
Message |
kenh83 Halfop
Joined: 08 Sep 2010 Posts: 61
|
Posted: Fri Apr 15, 2011 10:23 pm Post subject: Read and Match something in a file |
|
|
I'm stumped on how to read a file, to match something in the file and return the line of text that's in that file.
example.. say I have a file that contains the following information
Ted 250
John 291
Bill 301
How can I READ this file and say, I only want to match the line that contains matching text to TED -- thus it would output 'Ted 250'
Please help  |
|
| Back to top |
|
 |
willyw Revered One
Joined: 15 Jan 2009 Posts: 1175
|
Posted: Sat Apr 16, 2011 12:26 am Post subject: Re: Read and Match something in a file |
|
|
| Code: |
# Set the path and filename of text file to read
set file2read "you/need/to/edit/this/path/file_to_read.txt"
bind pub - "!readfile" readfile
proc readfile {nick uhost handle chan text} {
global file2read
if {![file exists $file2read]} {
putserv "privmsg $chan :Sorry, $file2read does not exist"
} else {
set fname "$file2read"
set fp [open $fname "r"]
set data [read -nonewline $fp]
close $fp
set lines [split $data "\n"]
if {[lsearch -exact -nocase -inline -all -index 0 $lines $text] != ""} {
putserv "privmsg $chan :[join [lsearch -exact -nocase -inline -all -index 0 $lines $text]]"
} else {
putserv "privmsg $chan :Sorry, $text not found"
}
}
}
putlog "Please see: http://forum.egghelp.org/viewtopic.php?t=18428"
# ref: http://forum.egghelp.org/viewtopic.php?t=6885
# http://www.tcl.tk/man/tcl8.5/TclCmd/lsearch.htm
|
In channel with bot, do:
!readfile Ted
for example, and bot will reply in the channel.
Tested briefly. It worked.
Am not saying that I thought of every possibility when testing.
There are probably lots of things that could be done, to improve it.
So it will be interesting to see what else gets posted here.
I hope this helps. You seemed like you just wanted a rough idea, to get you started. That's all this is. |
|
| Back to top |
|
 |
kenh83 Halfop
Joined: 08 Sep 2010 Posts: 61
|
Posted: Sat Apr 16, 2011 9:47 am Post subject: |
|
|
Willyw,
Thanks very much kind sir! |
|
| Back to top |
|
 |
|