This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Read and Match something in a file

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
k
kenh83
Halfop
Posts: 61
Joined: Wed Sep 08, 2010 11:22 am

Read and Match something in a file

Post by kenh83 »

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 :)
w
willyw
Revered One
Posts: 1203
Joined: Thu Jan 15, 2009 12:55 am

Re: Read and Match something in a file

Post by willyw »

Code: Select all

# 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.
k
kenh83
Halfop
Posts: 61
Joined: Wed Sep 08, 2010 11:22 am

Post by kenh83 »

Willyw,

Thanks very much kind sir!
Post Reply