PS: in the future, start a new thread for a new subject.
PPS. Nesting expansions rarely works, the example above may not work, you may need an intermediate variable.
edit: nesting substitutions like I did above does not work, you need to trim the beginning and end seperately.
It sounds like you want to just ignore lines that do not have those tags. If the data conveniently has line feeds after every close or all element tag and not after any interesting open element tags, just egrep for those tags. It's not xml, just text filtering. You can use sed, awk or perl to adjust the lines if they do not ffit that mold and the line feeds are not real data. PERL, C++ and JAVA can parse xml, and probably that other p named script language I keep blocking on. XPATH XQUERY sorts of queries rely on real xml parsing.
thanks for ur reply.
PFA the xml file.
actually i want to extract value this way.
Example:
if i give AdminInQueue in my command it will give back its <tns:value> only. here it will give WBIA.SMRPSC1.ADINQ.
means i want to extract the particular <tns:value> by giving its name only.
and i will store it one variable.
can u please suggest something?
thanks in advance.
One approach is to make tuple lines, where the name and value are in that order on the line with a delimiter between: space, comma, tab, colon, pipe long vertical mark. Then another command can filter the lines that you want, allowing you to use wild cards in your key.
Many Many thanks.
it works now. Now i want to use that value for replacing a <tns:value> having same <tns:name> but in the different file,contains same tns:name but different tns:value.
[i saved the tns:value into a variable but couldn't replace the other <tns:value> for different xml file. Both files have same <tns:name> but different values. i want to make both files same.]
can u pls help me on this.
PFA 2 attachments. Suppose one file is source and another one ur target xml. Both have same <tns:name> but different <tns:value>s. i want to change all <tns:value>s of target file to source file's <tns:value>s only.
no.<tns:name> not the only single key. i want to replace the <tns:value>s using source file only.that's it.
i want to know the command how can i change the <tns:value> of my target xml by using source xml.
You can crudely parse the source file finding name and value pairs and put them in a bash or awk associative array (hash map) as a resource when processing the second file, replacing all values where the name looks up in the array.
awk '
BEGIN {
F = "source.xml"
while ((getline line < F) > 0)
{
if ( line ~ /tns:name/ )
{
R = line
gsub(/[ \t]*<tns:name>|<\/tns:name>[ \t]*/, X, R)
f = 1
}
if ( line ~ /tns:value/ && f )
{
gsub(/[ \t]*<tns:value.*\">|<\/tns:value>[ \t]*/, X, line)
T_V[R] = line
f = 0
}
}
close(F)
F = "target.xml"
while ((getline line < F) > 0)
{
if ( line ~ /tns:name/ )
{
R = line
gsub(/[ \t]*<tns:name>|<\/tns:name>[ \t]*/, X, R)
f = 1
}
if ( line ~ /tns:value/ && f )
{
V = line
gsub(/[ \t]*<tns:value.*\">|<\/tns:value>[ \t]*/, X, V)
if ( R in T_V )
{
if ( V != T_V[R] )
gsub(V, T_V[R], line)
}
}
print line
}
close(F)
} ' /dev/null
The code is not working for me. i didn't get any changes in my target file
eg
awk -F'[<>]' ' /tns:value/ { c = $97 ; print c ; exit 1 } ' source.xml
it can also fetch one value from my source file. i want to replace the same pos i.e;"$97"th positon value for target.xml by my "c" value of source.xml.
$ cat source.xml
<tns:property>
<tns:name>AdminOutQueue</tns:name>
<tns:type>String</tns:type>
<tns:subtype>QueueName</tns:subtype>
<tns:value xml:space="preserve">WBIA.SMRSFA2.ADOUTQ</tns:value>
<tns:description>The logical queue that will be used by the connector to write admin messages to the broker</tns:description>
<tns:updateMethod>component restart</tns:updateMethod>
<tns:location>
<tns:reposController>false</tns:reposController>
<tns:reposAgent>true</tns:reposAgent>
<tns:localConfig>true</tns:localConfig>
</tns:location>
<tns:isEncrypted>false</tns:isEncrypted>
</tns:property>
<tns:property>
<tns:name>AgentTraceLevel</tns:name>
<tns:type>Integer</tns:type>
<tns:subtype />
<tns:value xml:space="preserve">5</tns:value>
<tns:description>Trace level for the connector agent</tns:description>
<tns:updateMethod>component restart</tns:updateMethod>
<tns:location>
<tns:reposController>false</tns:reposController>
<tns:reposAgent>true</tns:reposAgent>
<tns:localConfig>true</tns:localConfig>
</tns:location>
<tns:isEncrypted>false</tns:isEncrypted>
</tns:property>
Target XML
$ cat target.xml
<tns:property>
<tns:name>AdminOutQueue</tns:name>
<tns:type>String</tns:type>
<tns:subtype>QueueName</tns:subtype>
<tns:value xml:space="preserve">DUMMY</tns:value>
<tns:description>The logical queue that will be used by the connector to write admin messages to the broker</tns:description>
<tns:updateMethod>component restart</tns:updateMethod>
<tns:location>
<tns:reposController>false</tns:reposController>
<tns:reposAgent>true</tns:reposAgent>
<tns:localConfig>true</tns:localConfig>
</tns:location>
<tns:isEncrypted>false</tns:isEncrypted>
</tns:property>
<tns:property>
<tns:name>CharacterEncoding</tns:name>
<tns:type>String</tns:type>
<tns:subtype />
<tns:value xml:space="preserve">ascii7</tns:value>
<tns:description>The connector agent will use the character encoding</tns:description>
<tns:updateMethod>component restart</tns:updateMethod>
<tns:location>
<tns:reposController>false</tns:reposController>
<tns:reposAgent>true</tns:reposAgent>
<tns:localConfig>true</tns:localConfig>
</tns:location>
<tns:isEncrypted>false</tns:isEncrypted>
</tns:property>
Code O/P:
$ ./guha
<tns:property>
<tns:name>AdminOutQueue</tns:name>
<tns:type>String</tns:type>
<tns:subtype>QueueName</tns:subtype>
<tns:value xml:space="preserve">WBIA.SMRSFA2.ADOUTQ</tns:value>
<tns:description>The logical queue that will be used by the connector to write admin messages to the broker</tns:description>
<tns:updateMethod>component restart</tns:updateMethod>
<tns:location>
<tns:reposController>false</tns:reposController>
<tns:reposAgent>true</tns:reposAgent>
<tns:localConfig>true</tns:localConfig>
</tns:location>
<tns:isEncrypted>false</tns:isEncrypted>
</tns:property>
<tns:property>
<tns:name>CharacterEncoding</tns:name>
<tns:type>String</tns:type>
<tns:subtype />
<tns:value xml:space="preserve">ascii7</tns:value>
<tns:description>The connector agent will use the character encoding</tns:description>
<tns:updateMethod>component restart</tns:updateMethod>
<tns:location>
<tns:reposController>false</tns:reposController>
<tns:reposAgent>true</tns:reposAgent>
<tns:localConfig>true</tns:localConfig>
</tns:location>
<tns:isEncrypted>false</tns:isEncrypted>
</tns:property>
Please note that AWK does not change the content of target.xml file, you have to redirect the result to a new file to save the changes.