egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Read and Match something in a file

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests
View previous topic :: View next topic  
Author Message
kenh83
Halfop


Joined: 08 Sep 2010
Posts: 61

PostPosted: Fri Apr 15, 2011 10:23 pm    Post subject: Read and Match something in a file Reply with quote

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 Smile
Back to top
View user's profile Send private message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1175

PostPosted: Sat Apr 16, 2011 12:26 am    Post subject: Re: Read and Match something in a file Reply with quote

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. Smile

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
View user's profile Send private message
kenh83
Halfop


Joined: 08 Sep 2010
Posts: 61

PostPosted: Sat Apr 16, 2011 9:47 am    Post subject: Reply with quote

Willyw,

Thanks very much kind sir!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber