Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removeing lines that start with # out of a string

Status
Not open for further replies.

bobertperry

Programmer
Dec 12, 2003
2
US
Hello,
I am reading in a file into a string and then using that to set all sorts of vars. I am trying to add a way to be able to comment lines out with string start, string end, and string replace but I am getting all sorts or weird results. Would anyone please post an example?

Here is the code I want to insert it into. (not that it matters)
set number_of_routers 0

set inpid [open $IPs "r"]
for {set i 0} {![eof $inpid]} {incr i } {
gets $inpid regel
# edit coments out of $regel
foreach {this_name this_ip this_pass this_en_pass} $regel {}
set ipaddr($this_name) $this_ip
set routers_name($number_of_routers) $this_name
# set routers_ip($number_of_routers) $this_ip
set routers_pass($number_of_routers) $this_pass
if {$routers_pass($number_of_routers)==""} then {set routers_pass($i) $pass }
set routers_en_pass($number_of_routers) $this_en_pass
if {$routers_en_pass($number_of_routers)==""} then {set routers_en_pass($i) $en_pass }
incr number_of_routers

}

Thanks,
Rob
 
I'm not sure if there's a reason you're doing it this way but I think there's an easier one. First, you seem to be reading a single line in "gets $inpid regel" then parsing it with "foreach". That implies to me that the fields (words) in "regel" are space delimited already. I like to do it this way:
#read in the whole file as a list of lines
set linelist [split [read $inpid] \n]
#now operate on each line
foreach line $linelist {
#now, what do you want to do? "scan" will parse the line
scan $line %s%s%s%s this_name this_ip this_pass this_en_pass
}

OK. Now I don't know what you want to do with the strings. Are you just taking out whole lines if the data is wrong?
&quot;if {[string first <bad word> $line] >-1} break&quot;

Bob Rashkin
rrashkin@csc.com
 
Sweet!
Thanks for the help :)
heres exactly what I did.


#read in the whole file as a list of lines
set linelist [split [read $inpid] \n]
#now operate on each line
foreach line $linelist {
if {[string first $comment $line] == -1} {
scan $line %s%s%s%s this_name this_ip routers_pass($number_of_routers) routers_en_pass($number_of_routers)
set ipaddr($this_name) $this_ip
set routers_name($number_of_routers) $this_name
incr number_of_routers
}
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top