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.

removing lines from a text file

Old posts that have not been replied to for several years.
Locked
m
mdixon4az
Voice
Posts: 13
Joined: Sun Oct 12, 2003 12:12 pm

removing lines from a text file

Post by mdixon4az »

I figured out how to read and message the contents of a file to a user that types a command, but how do you search a file for a specific line and delete it?
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

what kind of search? glob/exact match or by line number?
Have you ever read "The Manual"?
m
mdixon4az
Voice
Posts: 13
Joined: Sun Oct 12, 2003 12:12 pm

Post by mdixon4az »

Exact. For example. !new.info would read the file and message the person that used that trigger each line of the file.

I want to be able to do !old.info and just cut & paste one of the lines after the trigger to get it to remove that line and resave the file.

Know how?

Thanks
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

mdixon4az wrote:Exact. For example. !new.info would read the file and message the person that used that trigger each line of the file.

I want to be able to do !old.info and just cut & paste one of the lines after the trigger to get it to remove that line and resave the file.

Know how?

Thanks
I probably know how, but I don't know what '!new.info' would do...do you? If you want exact matches, why do you want the matches in return? a count would make more sense as you already know the contents of each matching line :P
Have you ever read "The Manual"?
m
mdixon4az
Voice
Posts: 13
Joined: Sun Oct 12, 2003 12:12 pm

Post by mdixon4az »

ok, well, I guess that would work too. do !old.info 4 and it would remove line 4 from the file

how would I do that?
User avatar
CrazyCat
Revered One
Posts: 1304
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

read each line of the file, and increment a counter.
if counter != the specified line, copy the line to another file
when reached the end of file, delete the old file and rename the new as the old one...

Code: Select all

proc del:line {theline} {
   global yourfile
   set oldfile [open $yourfile r]
   set newfile [open tempfile.txt w]
   set cnt 0
   while { ![eof $oldfile] } {
      incr cnt
         gets $oldfile templine
      if { $cnt != $theline } {
         puts $newfile $templine
      } else {
         putlog "Deleted $templine"
      }
   }
   close $oldfile
   close $newfile
   file delete -force $yourfile
   file rename -force tempfile.txt $yourfile
}
m
mdixon4az
Voice
Posts: 13
Joined: Sun Oct 12, 2003 12:12 pm

Post by mdixon4az »

Great, I will add that and test it out, make sure I don't mess something up. I would also like to add a search function, can you help me with that as well?

Thanks
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

See this topic for an example.
Once the game is over, the king and the pawn go back in the same box.
m
mdixon4az
Voice
Posts: 13
Joined: Sun Oct 12, 2003 12:12 pm

Post by mdixon4az »

I have it basically working, only thing I have noticed if is that each time I use the command to find and remove a line from the file it adds an extra blank line to the new file. How can I get it to stop doing that?

Thanks
Locked