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.

txt files

Old posts that have not been replied to for several years.
Locked
R
Rich

txt files

Post by Rich »

Heya i was wondering how to script something that can search through a text file find a specific line then delete that line and the 7 lines after that any ideas, clues, examples would be greatly appreciated messed about myself for a week trying to do it ... am sick and bored of it now lol
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

First thing, we don't like to go overboard, and give the full script SPLAT. We like to get the person doing it themselves, with helping pointers, and code snippets. We usualy ask, that a person, post what code they have, and any other bits they may have tried.

Anyway.

The first thing you should be doing, is opening and reading the file contents. This way, we can read the information into memory, using open, read and close.

Read will read all the data from the file, up to a EOF, in one line string (remember the word string).

To search for a particular line, we need a way, to split it up into smaller chunks, IE, search it line by line. As such, you can split ths string, into a list (remember list as well).

TO do so, use the split command, using a split character of \n. This will create 1 element in a list, for each line in the file, by spliting into 1 chunk, for every newline character.

Now, because we have a list, we have some handy list commands, that can pick, one specific line, remove a specific line and even search, line by line.

You didn't say, how you picked the specific line. Is it an exact match, a wildcard match or a regexp? The lsearch command, can support them all.

OK, using this command, it will search the list for your entry, and return the index of the match, or -1 if it isn't in the file.

This index, can be used on commands like lindex, lrange, lreplace. Lreplace can be used to remove lines from the list, and in this case, it can even be used to remove ranges, such that, 1 lines, plus 7 after it are removed.

IE, lreplace $list $matchedidx [expr $matchedidx + 7]

This will delete the line, plus seven after it.

You can then open the file, use a foreach loop, to cycle through the new list, and write the contents tot he file.
R
Rich

Post by Rich »

thxs man ill have a further play tonite and paste what got up 2 :p
R
Rich

Post by Rich »

ok im a retard or summin cos i just cant get it to work :)
This is what im trying to do.. making a script to fully automate pisg for an eggy. (and b4 u mention the fact that its a windrop, i know, i know) :p
Addstats works just although i prob cud tidy it up a bit :)
#
set dir "C:/Windrop/test.txt"

bind pub m !Addstat pub_addstat
proc pub_addstat {nick host handle channel arg} {
global botnick dir
set chan [string trimleft $channel #]
set file [open $dir a+]
set info "
<channel=\"$channel\">
Logfile=\"c:\\windrop\\logs\\$chan.log\"
Format=\"eggdrop\"
Network=\"Quakenet\"
Maintainer=\"$botnick\"
OutputFile=\"$chan.php\"
PageHead =\"html.txt\"
</channel>"
puts $file $info
close $file
return 1
}

bind pub m !rmvstat pub_rmvstat
proc pub_rmvstat {nick host handle channel arg} {
global dir
set info "<channel=\"$channel\">"
set found 0
set file [open $dir a+]
while {![eof $file]} {
gets $file list
if {$list == $info} { set found 1 }
}
close $file
if {$found == 1} {
putserv "NOTICE $nick :Channel stats Removed for $channel"
} else {
putserv "NOTICE $nick :Removing Channel stats failed for $channel"
}}
#
^ that was my original attempt at finding a line and at least getting it to tell me it had found it :/ didnt work oh well after your advice i tried this ...
#
bind pub m !rmvstat pub_rmvstat
proc pub_rmvstat {nick host handle channel testes} {
global dir
set info "<channel=\"$channel\">"
set found 0
set file [open $dir a+]
set buffer [read $file]
set contents [split $buffer "\n"]
foreach line $contents {
if {$line != ""} {
if {$contents == $info} { set found 1 }
}
close $file
if {$found == 1} {
putserv "NOTICE $nick :Channel stats Removed for $channel"
} else {
putserv "NOTICE $nick :Removing Channel stats failed for $channel"
}}}
#
Surely i must be getting closer by now .. i hope ... although it still doesnt work :/
ive tried a few other different ways of doing it however i doubt it wud be appreciated if i pasteded pages upon pages of txt :/ plz help poor me :/
Last edited by Rich on Thu Oct 17, 2002 11:36 pm, edited 1 time in total.
R
Rich

Post by Rich »

i jsut found a prob dunno if this is an eggy/tcl prob or just what i read was wrong but isnt the command
a+ when opening a file meant to make it read and write ? when i try to read using it, it doesnt work :/
mega cause of all my probs :/
Locked