LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need Modification in XML file by reading text file (https://www.linuxquestions.org/questions/linux-newbie-8/need-modification-in-xml-file-by-reading-text-file-4175592192/)

DINESHKUMARSAKTHI 10-25-2016 10:00 AM

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>

schneidz 10-25-2016 10:06 AM

not quite following but it seems like sed would be helpful here.

TB0ne 10-25-2016 10:07 AM

Quote:

Originally Posted by DINESHKUMARSAKTHI (Post 5622765)
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>

Please read the "Question Guidelines" link in my posting signature, and please use CODE tags around code samples.

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.

DINESHKUMARSAKTHI 10-25-2016 10:35 AM

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
do
export var1=$(echo $line | cut -f1 -d#)
export var2=$(echo $line | cut -f2 -d#)
perl -i -pe 'BEGIN{$/="<operation>"} if(/$ENV{'var1'}<\/operationCode/){s/<operationName>/<operationName>$ENV{'var2'}/g}' Manufacturing.xml
done < ManufacturingOperDesc.txt

But for some Operation code, Multiple Operation values gets updated in single operation tag.

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

TB0ne 10-25-2016 12:03 PM

Quote:

Originally Posted by DINESHKUMARSAKTHI (Post 5622775)
Sorry, This is the first time Im posting in this forum.

Regardless of the forum, topic, or place, unless you provide details, NO ONE, ANYWHERE will be able to help you with anything.
Quote:

Linux version: Red Hat Enterprise Linux Server release 5.11 (Tikanga)
And are you PAYING FOR RHEL???
Quote:

I tried with Perl option for this . Below is the command I used
Code:

while read line
do
export var1=$(echo $line | cut -f1 -d#)
export var2=$(echo $line | cut -f2 -d#)
perl -i -pe 'BEGIN{$/="<operation>"} if(/$ENV{'var1'}<\/operationCode/){s/<operationName>/<operationName>$ENV{'var2'}/g}' Manufacturing.xml
done < ManufacturingOperDesc.txt

But for some Operation code, Multiple Operation values gets updated in single operation tag.
Because you're not really using perl at all...that's a bash script, which is feeding a perl one-liner to perform a sed operation. You could leave perl out totally and use sed alone:
Code:

sed 's/<operationName>/<operationName>$SOMEVARIABLE/g'
Instead of defining var1 and var2 as environment variables, what's the point??? Set them to bash variables, and use them directly. Perl has MANY options for parsing/reading/writing XML, but all you're after is a search/replace.

Turbocapitalist 10-25-2016 01:08 PM

Quote:

Originally Posted by TB0ne (Post 5622818)
Perl has MANY options for parsing/reading/writing XML, but all you're after is a search/replace.

In perl, either the XML::TreeBuilder or XML::TreeBuilder::XPath module will reliably and easily do a search and replace in XML. The only constraints on input are that it be well-formed XML. Otherwise, it tolerates a lot of variability that would choke "sed" or "awk"

TB0ne 10-25-2016 01:29 PM

Quote:

Originally Posted by Turbocapitalist (Post 5622837)
In perl, either the XML::TreeBuilder or XML::TreeBuilder::XPath module will reliably and easily do a search and replace in XML. The only constraints on input are that it be well-formed XML. Otherwise, it tolerates a lot of variability that would choke "sed" or "awk"

Absolutely...but the OP just seems to need something pretty simple. For tag processing or something more exotic, I'd go full-bore like this too.

grail 10-25-2016 01:53 PM

As most of the question has already been answered, the part that has slipped through is:
Quote:

But for some Operation code, Multiple Operation values gets updated in single operation tag.
The reason for this is because you told perl (or sed) to replace it occurrences when you you put the 'g' at the end after the last '/'. If you only want to replace the first found then
simply remove the 'g'

sundialsvcs 10-25-2016 02:39 PM

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:

Actum Ne Agas: Do Not Do A Thing Already Done.™
As Perl programmers love to say: ("Tim Toady" ...)
Quote:

TMTOWTDI: There's More Than One Way To Do It.™

AnanthaP 10-27-2016 06:29 AM

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

sundialsvcs 10-27-2016 08:13 AM

... 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.