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.

function similar grep for unix

Old posts that have not been replied to for several years.
Locked
r
raid3n

function similar grep for unix

Post by raid3n »

Hi, i'm looking for a function that find a string in a txt file.

If in my raiden.txt i have a string "hello", is in tcl a function that return 0 if it exists ?

Excuse my bad english.

Regards
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

there was a tread like this one just some days ago
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

is it to create @find cause i got a script that does that ..

script below is like smal sample script you could use for that havn't tested it yet tho so let me know if it does ..

Code: Select all

#Usage: [find:text <filename.txt> <search string>]

proc find:text {filename arg} {
  set count "0"
  set rfile [open "$filename" "r"]
  while {![eof $rfile]} {
    gets $rfile line
    if {[string match "*[string map {\x20 *} $arg]*" $line]} {
      putserv "PRIVMSG $nick :$line"
      set count [expr $count+1]
      return 1
    }
  }
  close $rfile
  if {$count == 0} {
    return 0
  }
}
XplaiN but think of me as stupid
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Ofloo wrote:

Code: Select all

#Usage: [find:text <filename.txt> <search string>]
proc find:text {filename arg} {
  set count "0"
  set rfile [open "$filename" "r"]
  while {![eof $rfile]} {
    gets $rfile line
    if {[string match "*[string map {\x20 *} $arg]*" $line]} {
      putserv "PRIVMSG $nick :$line"
      set count [expr $count+1]
      return 1
    }
  }
  close $rfile
  if {$count == 0} {
    return 0
  }
}
There's no variable called 'nick' in that proc. Also, returning from inside the loop will leave the file open (you must have meant 'break' :) ).
Have you ever read "The Manual"?
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Have a look at this post.
Once the game is over, the king and the pawn go back in the same box.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Alternativly, use the fileutil package that comes with TclLib
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

This should do the trick ;)

Code: Select all

#Usage: [find:text <nick> <filename.txt> <search string>] 

proc find:text {nick filename arg} { 
  set count "0" 
  set rfile [open "$filename" "r"] 
  while {![eof $rfile]} { 
    gets $rfile line 
    if {[string match "*[string map {\x20 *} $arg]*" $line]} { 
      putserv "PRIVMSG $nick :$line" 
      set count [expr $count+1] 
    } 
  } 
  close $rfile 
  if {$count == 0} { 
    return 0 
  }
  if {$count != 0} { 
    return 1
  }  
}
forgot a return breaked a loop :/ sorry hehe and forgot about the nick var
puts serv is just optional stuff so is the return its for usage in other procs depends how your using it .. just to give an idea on how you could do it .. you also can set max results by setting if $count < 10 for example .. and so on if $count > 10 then putserv to mutch results .. or something
XplaiN but think of me as stupid
Locked