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!

search command 1

Status
Not open for further replies.

bebig

Technical User
Oct 21, 2004
111
US
I am trying to search a string and replace it.
I can read a file, but I do not know how to search exact word and replace to another value.
Could you do me a favor?
thank you in advance.

set openfile open ["xmlpage.xml" "r"]
gets $openfile line
while {![eof $openfile]} {

#---this part, I think I need search command.
puts $line
gets $openfile line
}
}
 
Like this:
#<your code...>
set searchWord good; #for example
set replaceWord splendid; #for example
#...
gets $openfile line
set line [string map {$searchWord $replaceWord} $line]

Bob Rashkin
rrashkin@csc.com
 
I have a XML file
my code can open xml file and read it.
To search a string in XML structure, I tried to read line by line, but I could not get correct result.
is there any way??

thank you in advance.


 
I can undestand bong's idea....
um..I need to try one more time..
 
is this right algorithem?

set openfile open ["xmlpage.xml" "r"]
gets $openfile line
while {![eof $openfile]} {

set searchWord good; #for example
set replaceWord splendid; #for example

#---this part, I think I need search command.
gets $openfile line
set line [string map {$searchWord $replaceWord} $line]
puts $line

}
==== this is xml code====
<?xml version="1.0" ?>
- <america>
- <university id="446">
<name>schedule 1</name>
- <edge id="1524">
<dow id="mon" checked="checked" />
<dow id="tue" checked="checked" />
<dow id="wed" checked="checked" />
<dow id="thur" checked="checked" />
<dow id="fri" checked="checked" />
<dow id="sat" checked="unchecked" />
<dow id="sun" checked="checked" />
<edgetime>9:00</edgetime>
<stateid>2</stateid>
</edge>

this is a part of XML.
I opened this file and read lines. Then, I am trying to search " <edgetime>9:00</edgetime> ". how to approach this case?
Thank you in advance
 
What word do you want to search for?
What word do you want to replace it with?
You can search for "<edgetime>9:00</edgetime>" in each line:
set searchWord "<edgetime>9:00</edgetime>"
set ix [string first $searchWord $line]

Then "ix" will have the value of the index in "line" where the first "<" occurs. But I thought you wanted to find a particular substring in a string and replace it. Not so?

Bob Rashkin
rrashkin@csc.com
 
Yes...right...I am going to replace edgetime.
replace <edgetime>9:00</edgetime> to <edgetime>10:00</edgetime>
 
Is this really a "string" operation or an "arithmetic" operation? That is, will you always replace the string, <edgetime>9:00</edgetime>, with the string, <edgetime>10:00</edgetime> (very easy)? Or will you replace what ever string you find between <edgetime> and </edgetime> with something else (say, incremented by 1 before the ":")?

In the first case, the "string map" function works well:
set searchWord "<edgetime>9:00</edgetime>"
set replaceWord "<edgetime>10:00</edgetime>"
set line [string map {$searchWord $replaceWord} $line]




Bob Rashkin
rrashkin@csc.com
 
I have a question.
this is a code.
==============
set searchWord "<edgetime>8:00</edgetime>"
set replaceword "<edgetime>9:50</edgetime>"
set line [string map {$searchWord $replaceWord} $line]
#puts "$line"
set ix [string first $searchWord $line]
puts $ix
=============
I ran it, and I got output --> -1
what does mean that?
 
what if I want to replace what ever string you find between <edgetime> and </edgetime> with something else, how can I approach?
 
First of all, I steered you wrong in the first place. Instead of set line [string map {$searchWord $replaceWord} $line], you should use set line [string map "$searchWord $replaceWord" $line]. The curly braces delay the necessary substitution.

The -1 returned from the "string first" statement means that the search pattern was not found. In the case you list, it means the searchWord = "<edgetime>8:00</edgetime>" was not in the string, "line".

So, as I alluded previously, let's say you're going through the lines in your XML file, one by one, and you want to look for a particular tag. In your example it's "<edgetime>" but I don't want to type that anymore so let's say it's "<tag>". We'll start from the point where the XML string is the value of "line". We'll use "regexp" to get the value that follows <tag> into a variable:
regexp {(\<tag\>)([0-9]*:[0-9]*)} $line m1 m2 m3

now the variable, m1, contains "<tag>nn:nn"; m2 contains "<tag>"; and m3 contains "nn:nn" (where nn:nn is some number of digits followed by a comma followed by some number of digits).
Now manipulate $m3:
[/b]
set mlst [split $m3 :][/b]
#add 30 minutes
set mlst [lreplace $mlst 1 1 [expr [lindex $mlst 1]+30]]
#add 1 hour
set mlst [lreplace $mlst 0 0 [expr [lindex $mlst 0]+1]]
#join them back up:
set m4 [join $mlst :]
#now use regsub to replace the string:
regsub {\<tag\>[0-9]*:[0-9]*} $line "\<tag\>$m4" line


Bob Rashkin
rrashkin@csc.com
 
why did you add 30 min and 1hour?
 
08 is an invalid octal number
while excuting
"expr [lindex $mlst 1]+30"

how to fix this problem??

thank you so much..
 
1. [/b] is part of a TGML tag. I made a mistake.
2. adding 30 minutes and 1 hour were just examples of what I thought you might want to do.
3. to avoid the leading zero causing the string to be evaluated as an octal number, scan it first:
scan $a1 %u a2

Bob Rashkin
rrashkin@csc.com
 
what if I just replace value
set a "09:10"

regexp {(\<edgetime\>)([0-9]*:[0-9]*)} $line m1 m2 m3
set mlst $m3


#add 30 minutes
set mlst1 [lreplace $mlst 0 $a]
scan $mlst1 %u mlst1
puts $mlst1


#now use regsub to replace the string:
regsub {\<edgetime\>[0-9]*:[0-9]*} $line "\<edgetime\>$mlst1" line

is it correct??
 
Simpler than that:
Code:
set a 09:10

regsub {\<edgetime\>[0-9]*:[0-9]*} $line "\<edgetime\>$a" line

Since you know what you want to stick in, and it's independent of the value that's there, you don't need to pull the value into a variable at all. You just replace whatever's after the tag with the value you want.

Bob Rashkin
rrashkin@csc.com
 
regexp {(\<edgetime\>)([0-9]*:[0-9]*)} $line m1 m2 m3

set a 09:20


#add 30 minutes
set mlst2 [lreplace $m3 0 0 $a]
scan $mlst2 %u mlst2
puts $mlst2

#add 1 hour

#join them back up:


regsub {\<edgetime\>[0-9]*:[0-9]*} $line "\<edgetime\>$mlst2" line

#now use regsub to replace the string:

puts $line

is this correct?
 
I ran this code.
I got 9 instead of 09:20
would you please tell me why I could not get correct result?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top