LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-25-2016, 11:00 AM   #1
DINESHKUMARSAKTHI
LQ Newbie
 
Registered: Oct 2016
Posts: 2

Rep: Reputation: Disabled
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>
 
Old 10-25-2016, 11:06 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
not quite following but it seems like sed would be helpful here.
 
Old 10-25-2016, 11:07 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,926

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by DINESHKUMARSAKTHI View Post
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.
 
Old 10-25-2016, 11:35 AM   #4
DINESHKUMARSAKTHI
LQ Newbie
 
Registered: Oct 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
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
Attached Files
File Type: txt ManufacturingOperDesc.txt (2.3 KB, 3 views)
 
Old 10-25-2016, 01:03 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,926

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by DINESHKUMARSAKTHI View Post
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.
 
Old 10-25-2016, 02:08 PM   #6
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 962
Blog Entries: 3

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
Quote:
Originally Posted by TB0ne View Post
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"
 
Old 10-25-2016, 02:29 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,926

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
Quote:
Originally Posted by Turbocapitalist View Post
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.
 
Old 10-25-2016, 02:53 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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'
 
Old 10-25-2016, 03:39 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 7,151

Rep: Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203
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.™

Last edited by sundialsvcs; 10-25-2016 at 03:41 PM.
 
1 members found this post helpful.
Old 10-27-2016, 07:29 AM   #10
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 805

Rep: Reputation: 186Reputation: 186
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

Last edited by AnanthaP; 10-27-2016 at 07:30 AM.
 
Old 10-27-2016, 09:13 AM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 7,151

Rep: Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203
... 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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Prompt the user for a file to open, extract the XML and write to another text file. richiep Linux - Newbie 7 10-22-2010 04:34 PM
Reading/Wirting file/parsing xml file using javascript fakhrul Programming 1 08-14-2007 06:08 PM


All times are GMT -5. The time now is 06:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration