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.

one more 'basic' question on this

Old posts that have not been replied to for several years.
Locked
n
netux
Voice
Posts: 19
Joined: Fri Jul 30, 2004 8:42 pm

Post by netux »

i, nice place :D
could you extend your explanation with:

-add/replace/del a midle line (special replace)
-replace a word in a line (let's say file as 3 lines and you want to change second word of second line)
-read a specific word on a line (could be same as abose 2 of 2)
tks extend this faq and make a guide/tutorial hould be great, nice and simple explenations here.
N
NewzUK
Master
Posts: 200
Joined: Mon Nov 18, 2002 3:10 am
Location: Auckland, New Zealand
Contact:

Post by NewzUK »

Hi - one more 'basic' question on this (I've searched the forums but still havn't got it) - how would you go about reading a specific line number (as there's nothing unique in the text I can identify it with)
eg here's the script but using a string match, but instead, I want to get line number 13...

Code: Select all

bind pub - .test pub:test
proc pub:test { nick uhost handle channel arg } {
	global testurl
	set url $testurl
	set file scripts/test.txt
	exec lynx -preparsed -nolist -dump -width=5000 $url > $file
	set textf [open $file r] 
	set data [read $textf]
	foreach line [split $data \n] {
	if {[string match *word* $line]} {
	set text $line
	putserv "PRIVMSG $c :$text"
 }
}
	catch {close $file}
}
appreciate in advance any help!
#Newsroom - Where News & Markets Connect
http://www.inewsroom.net
#Newsroom on irc.othernet.org
User avatar
arcane
Master
Posts: 280
Joined: Thu Jan 30, 2003 9:18 am
Location: Germany
Contact:

Post by arcane »

well, just drop the "foreach" part and use lindex on [split $data \n] :)

e.g. for line 13:

Code: Select all

bind pub - .test pub:test 
proc pub:test { nick uhost handle channel arg } { 
   global testurl 
   set url $testurl 
   set file scripts/test.txt 
   exec lynx -preparsed -nolist -dump -width=5000 $url > $file 
   set textf [open $file r] 
   set data [split [read $textf] \n] 
   set text [lindex $data 12] 
   putserv "PRIVMSG $c :$text" 
   catch {close $file} 
} 
always use "lindex <yournumber>-1" because the first line is index 0
aVote page back online!
Check out the most popular voting script for eggdrop bots.

Join the metal tavern!
[
[Zod]
Voice
Posts: 4
Joined: Thu Dec 23, 2004 11:35 pm

Post by [Zod] »

How do you read from a file into a list, but allow strings with spaces to be on one line?
User avatar
arcane
Master
Posts: 280
Joined: Thu Jan 30, 2003 9:18 am
Location: Germany
Contact:

Post by arcane »

just don't split on " " ;)
aVote page back online!
Check out the most popular voting script for eggdrop bots.

Join the metal tavern!
o
oioifatty
Voice
Posts: 7
Joined: Sun May 08, 2005 7:53 pm

pattern matching and deleting.

Post by oioifatty »

Just like to state this is a really great forum, actually the most imformative i've been on, btw not brown nosing. lol

To my question:

I am attempting to write a TCL/TK firewall gui on my fedora core box, just having bit of a problem with deleting strings from a file, I want to pattern match a string, and if it exists in a particular file i would like to delete it. I understand how to delete lines from a file (from the examples above), but just don't know how to go about matching a string and removing it. any help would be very appreciated.

Cheers to anyone who responds.
"if it's in a book, it's gotta be true"
o
oioifatty
Voice
Posts: 7
Joined: Sun May 08, 2005 7:53 pm

Post by oioifatty »

sorry that was proberly a bit vague,
############################
#this is the check button command#
############################
if {$http == 1} {
set a [open /iptab a+]
puts $a "iptables -A OUTPUT -o \$internet -p tcp" <--Inputs iptables command into file
close $a
} else {
#########################################
# this is the bit where i'd like the pattern matching #
#########################################
so, for example if "iptables -A OUTPUT -o $internet -p tcp" existed in the file, i'd like to remove it from the file.
so, basically selecting the tick box turns a feature on, deselecting it turns it off
}

Sorry if its a particulary difficult post to answer
"if it's in a book, it's gotta be true"
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

Code: Select all

if [string match "iptables -A OUTPUT -o \$internet -p tcp" $line] { # it's a match, delete..
o
oioifatty
Voice
Posts: 7
Joined: Sun May 08, 2005 7:53 pm

Post by oioifatty »

gb wrote:

Code: Select all

if [string match "iptables -A OUTPUT -o \$internet -p tcp" $line] { # it's a match, delete..
yeah thats the thing i want to acheive, but just don't know how to go about the deletion part. i've been using the examples of how to open a file, extract the text, and delete specified numbered lines.

but what about a specified string of text?
example of my code:

Code: Select all

set fname "/iptab"
set fp [open $fname "r"]
set data [read -nonewline $fp]
close $fp
foreach lines [split $data "\n"] {   
if {[string match "iptables -A OUTPUT -o \$internet -p tcp" $line]} {   
	set line_to_delete $line}
	#so line_to_delete contains the string i want to delete, but where from here??
}

set fp [open $fname w]
puts $fp [join $lines "\n"]
close $fp } }
Cheers,
Scott
"if it's in a book, it's gotta be true"
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

Code: Select all

set fname "/iptab"
set fp [open $fname "r"]
set data [read -nonewline $fp]
close $fp
set lines [split $data "\n"]

set linenum 0
foreach line $lines {
 if [string match -noc "*iptables -A OUTPUT -o \$internet -p tcp*" $line] {
  set lines [lreplace $lines $linenum $linenum]
  set fp [open $fname "w"]
  puts $fp [join $lines "\n"]
   close $fp
 }
 incr linenum
}
please don't send me any more private msg's.
o
oioifatty
Voice
Posts: 7
Joined: Sun May 08, 2005 7:53 pm

Thank you very much.

Post by oioifatty »

Thank you for the response, it works a treat.

p.s. sorry gb if i caused offence with the pm.
"if it's in a book, it's gotta be true"
User avatar
Thunderdome
Op
Posts: 187
Joined: Tue Mar 15, 2005 4:42 pm

Post by Thunderdome »

GREAT WORK :)
what about also some info on using eggHTTP.tcl to read "external" files?
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

that doesnt direclty relate to this topic, so keep it out of the faq section.
o
oioifatty
Voice
Posts: 7
Joined: Sun May 08, 2005 7:53 pm

another Question about file search

Post by oioifatty »

Hi, i've got a smal tcl code that reads the contents of a file for devices, eth0, eth1 etc...
the devs file contains all of the items in the list, but it only returns the last item that matches in the dev file, i know its something to do with the IF's, but i just can't get my head arounfd the correct layout. could any suggest the correct layout?

Code: Select all

set devices "/devs"
set file [ open $devices r ]
set data [read -nonewline $file]    
close $file
set lines [split $data "\n"]
foreach line $lines {
	if [string match "*lo:*" $line] {
		set lo lo
		} else { set lo ""}
 	if [string match -noc "*eth0:*" $line] {
		set eth0 eth0
		} else { set eth0 ""}
	if [string match -noc "*eth1:*" $line] {
		set eth1 eth1
		} else { set eth1 ""} 
	if [string match -noc "*ppp0:*" $line] {
		set ppp0 ppp0
		} else { set ppp0 ""}
	if [string match -noc "*ppp1:*" $line] {
		set ppp1 ppp1
		} else { set ppp1 ""}
}
"if it's in a book, it's gotta be true"
User avatar
^DooM^
Owner
Posts: 772
Joined: Tue Aug 26, 2003 5:40 pm
Location: IronForge
Contact:

Post by ^DooM^ »

This should of been asked in the Script Help section NOT the FAQ section. Please dont post your script help questions in this section.
The lifecycle of a noob is complex. Fledgling noobs gestate inside biometric pods. Once a budding noob has matured thru gestation they climb out of their pod, sit down at a PC, ask a bunch of questions that are clearly in the FAQ, The Noob is born
Locked