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.

How to overwrite/delete a line from .txt

Help for those learning Tcl or writing their own scripts.
Post Reply
D
DarkGhost
Voice
Posts: 6
Joined: Mon Jan 15, 2007 8:52 pm

How to overwrite/delete a line from .txt

Post by DarkGhost »

Hello I am making a login script with in a file.txt I have "username host password access online|offline" and when someone logs in i want to make the offline to online or when someone wants to change there password I want to change password but I am unsure how to do this please help thanks.
D
DarkGhost
Voice
Posts: 6
Joined: Mon Jan 15, 2007 8:52 pm

Post by DarkGhost »

Hello, I have seen alot of people look at this if you cant overwrite/delete lines atleast tell me :)
r
r0t3n
Owner
Posts: 507
Joined: Tue May 31, 2005 6:56 pm
Location: UK

Post by r0t3n »

A quick example to help you out:

Code: Select all

set file [open file.txt w+]
foreach line [split [read -nonewline $file] \n] {
  if {$line == ""} { return }
  if {[string match -nocase "USERNAME" [lindex [split $line] 0]]} {
    # To remove the line from file
    # return
    # To edit the line data
    # regexp -nocase {(.+)\s(.+)\s(.+)\s(.+)\s(.+)} $line -> username host password access status
    # To change the password use:
    # set password "NEWPASS"
    # To change online/offline status use:
    # set status "ONLINE or OFFLINE"
    # Write the modified data back to file
    # puts $file "$username $host $password $access $status"
  } else {
    # We don't need to delete/modify this line, write the line back to file
    puts $file "$line"
  }
}
close $file
r0t3n @ #r0t3n @ Quakenet
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

DarkGhost wrote:Hello, I have seen alot of people look at this if you cant overwrite/delete lines atleast tell me :)
It's probably one of those FAQ questions, and you could've found the answer yourself by searching the forum (I know I've written quite a few examples of this and didn't feel like doing it again.)
D
DarkGhost
Voice
Posts: 6
Joined: Mon Jan 15, 2007 8:52 pm

Post by DarkGhost »

Thanks it works great only problem is it doesnt delete the line its editing :(
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

DarkGhost wrote:Thanks it works great only problem is it doesnt delete the line its editing :(
what "it"? Tosser's code would not work as it will truncate the file before attempting to read from it, so I'm guessing you made something yourself based on what you read in the faq...show us your code.
Have you ever read "The Manual"?
User avatar
DragnLord
Owner
Posts: 711
Joined: Sat Jan 24, 2004 4:58 pm
Location: C'ville, Virginia, USA

login to what?

Post by DragnLord »

What type of login are you trying to create this for?
If this is for checking if someone is logged into the bot, it would be easier to check with a script using the dcc command "whom".
Changing password for eggdrop users is easiest done by using private message to the bot using "pass <oldpass> <newpass>"
(example: /msg botnick pass currentpass newpass)

If this is for some sort of website portal, then you will need a much more complicated approach.
D
DarkGhost
Voice
Posts: 6
Joined: Mon Jan 15, 2007 8:52 pm

Post by DarkGhost »

user wrote:
DarkGhost wrote:Thanks it works great only problem is it doesnt delete the line its editing :(
what "it"? Tosser's code would not work as it will truncate the file before attempting to read from it, so I'm guessing you made something yourself based on what you read in the faq...show us your code.
lol your so SMART! i had to change some stuff around but here

Code: Select all

putserv "PRIVMSG $nick :You are now logged in to $userName"
set file [open test.txt r+]
foreach line [split [read -nonewline $file] \n] {
  if {$line == ""} { return }
  if {[string match -nocase "$userName" [lindex [split $line] 0]]} {
     regexp -nocase {(.+)\s(.+)\s(.+)\s(.+)\s(.+)} $line -> username host password access
    set status "ONLINE"
    close $file
set file [open test.txt w+]
 puts $file "$username $host $password $access $status"
  } else {
    puts $file "$line"
  }
Oh and by the way i got it to work perfectly thanks.
Post Reply