Need Modification in XML file by reading text file
I have text file with two variable seperating by # (ManufacturingOperDesc.txt)
Example(operationCode#operationName) Example(Variable1#Variable2) Example(495789281::160:246163:100#Chrom 3 Setup Operations: ) I need to modify the XML tag <operationName>(variable2) (with respect to ManufacturingOperDesc.txt) whereever the variable1 comes in XML files. In xml file, it should be Before Modification: <operation> <operationCode>495789281::160:246163:100</operationCode> <operationName>160:100</operationName> After Modification: <operation> <operationCode>495789281::160:246163:100</operationCode> <operationName>Chrom 3 Setup Operations:160:100</operationName> |
not quite following but it seems like sed would be helpful here.
|
Quote:
Now that we know what you WANT, you need to show us what YOU, PERSONALLY, have written/done/tried to make this happen. We are happy to help you, but we WILL NOT write your code/scripts for you. You say nothing about version/distro of Linux, what programming language you want to use, etc. We can't guess. |
1 Attachment(s)
Sorry, This is the first time Im posting in this forum.
Linux version: Red Hat Enterprise Linux Server release 5.11 (Tikanga) I tried with Perl option for this . Below is the command I used Code:
while read line Attached the original ManufacturingOperDesc.txt , Manufacturing.xml and modified Manufacturing.xml using above perl method. Please give some ideas on this. Im ok with any methods like perl, sed or awk etc Thanks, Dinesh |
Quote:
Quote:
Quote:
Code:
sed 's/<operationName>/<operationName>$SOMEVARIABLE/g' |
Quote:
|
Quote:
|
As most of the question has already been answered, the part that has slipped through is:
Quote:
simply remove the 'g' |
If you are using Perl, then you should also be using one of its extremely powerful "XML manipulation" libraries, available from CPAN.
One of these provides very painless access to what is pretty-much the library for XML manipulation: libxml.so. (Or the corresponding Windows .DLL, or "whatever it is called on your OS.") Every other scripting/programming language provides similar capability, either built-in or readily available: "the ability to manipulate XML documents." Therefore, you do n-o-t have to "re-invent any wheels." You specifically should n-o-t treat the XML document "as a text file." Even if you are building "a shell script," Linux/Unix does not require you to use "Bash scripting." You can use any higher-level language that happens to be installed ... and of course, this being Linux, you can install any language(s) that you wish. Quote:
Quote:
|
Lets assume that the XML file is named XML.FILE Changes are going to be made to it based on the contents of the name-value pairs in ManufacturingOperDesc.txt which file can be thought of as the master. So XML.FILE is the data file.
My preference is for awk and I will pass the two files in succession to it. First the master file ManufacturingOperDesc.txt and then the data file XML.FILE. I would depend on the values of NR and FNR to differentiate between them. From the master file ManufacturingOperDesc.txt, I would build an associative array. From the data file: 1. Bypass all records not starting with <operationCode> 2. For records starting with <operationCode> 2a. Look up the OpCode in the associative array and write the value contained in it. Above is the pseudo code. OK |
... and you are still writing very complicated code to do "a thing already done."
For instance, you might be able to simply use XSLT technology to specify the XML manipulation that you want to have done, without writing any "program" at all. XML is a sometimes-tricky data format that has been thoroughly implemented by a set of well-trusted and well-tested libraries, such that you can simply use them to do whatever it is that you want to do. In my humble, this is a-l-w-a-y-s the "right" way to do it. I have over these many years debugged I-don't-know how-many "incomprehensible scripts" that did not do "quite" the right thing. They had absorbed enormous amounts of staff time. I would either rewrite them (or, as the case may be, direct that they be rewritten) to use the technologies that were built for the task ... and the problems went away, for good. |
All times are GMT -5. The time now is 11:39 AM. |