How to remove some xml tag lines using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove some xml tag lines using shell script
# 1  
Old 01-05-2011
How to remove some xml tag lines using shell script

I have existing XML file as below, now based on input string in shell script on workordercode i need to create a seprate xml file
for e.g if we pass the input string as 184851 then it find the tag data from <workOrder>..</workOrder> and write to a new file and similarly next time if i pass the inout string as
184853 then it should append to the new file .. please see the New file requirement.
Can any body provide me the sample sheel script for doing the below case would be great?
-------------------------------------
Existing File:
-------------------------------------
Code:
<?xml version="1.0" encoding="utf-8" ?> 
<scbm-extract version="3.3">
<workOrderList>
<workOrder>
  <branchCode>2021:100</branchCode> 
  <workOrderCode>184850</workOrderCode> 
</workOrder>
<workOrder>
  <branchCode>2022:100</branchCode>
  <workOrderCode>184851</workOrderCode> 
</workOrder>
<workOrder>
  <branchCode>2023:100</branchCode>
  <workOrderCode>184852</workOrderCode> 
</workOrder>
<workOrder>
  <branchCode>2023:100</branchCode>
  <workOrderCode>184853</workOrderCode> 
</workOrder>
</workOrderList>
</scbm-extract>

-------------------------------------
New File
-------------------------------------
Code:
<?xml version="1.0" encoding="utf-8" ?> 
<scbm-extract version="3.3">
<workOrderList>
  <branchCode>2022:100</branchCode>
  <workOrderCode>184851</workOrderCode> 
</workOrder>
<workOrder>
  <branchCode>2023:100</branchCode>
  <workOrderCode>184853</workOrderCode> 
</workOrder>
</scbm-extract>

-------------------------------------

Last edited by Franklin52; 01-05-2011 at 04:02 AM.. Reason: Please use code tags and indent your code, thanks
# 2  
Old 01-05-2011
One of the solution would be as below..
Code:
# create a output file with the contents as
cat outfile.xml
<?xml version="1.0" encoding="utf-8" ?> 
<scbm-extract version="3.3">
<workOrderList>

Code:
#!/bin/ksh
# save as extract_xml.sh
# run script as: ./extract_xml.sh 184850

echo "<workOrder>" >> outfile.xml
WOC=$1 
grep -w -C1 "$WOC" infile.xml >> outfile.xml

# 3  
Old 01-05-2011
If xsltproc is available to you, the following script (update.sh) will do what you want assuming that the input file is extract.xml and the output file is updated.xml.
Code:
#!/bin/bash

if test -z "$1"
then
   echo "ERROR: No command-line argument provided"
   exit 1
fi
WorkOrder=$1

[[ -f updated.xml ]] || {
   cat > updated.xml <<EOF
<?xml version="1.0" encoding="utf-8" ?>
<scbm-extract version="3.3">
   <workOrderList>
   </workOrderList>
</scbm-extract>
EOF
}

cat > updatexsl.$$ <<EOF
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <!-- XSLTPROC pass in as -param name "'value'" etc -->
   <xsl:param name="woc">EMPTY</xsl:param>

   <xsl:output method="xml" indent="yes" encoding="utf-8"/>
   <xsl:strip-space elements="*"/>

   <xsl:template match="/">
      <xsl:apply-templates />
   </xsl:template>

   <xsl:template match="scbm-extract">
     <xsl:copy>
        <xsl:apply-templates select="workOrderList"/>
     </xsl:copy>
   </xsl:template>

   <xsl:template match="workOrderList">
     <xsl:copy>
        <xsl:apply-templates select="workOrder"/>
        <xsl:copy-of select="document('./extract.xml')//workOrder[workOrderCode=\$woc]" />
     </xsl:copy>
   </xsl:template>

   <xsl:template match="workOrder">
       <xsl:copy-of select = "." />
   </xsl:template>

</xsl:stylesheet>
EOF

xsltproc -param woc "'${WorkOrder}'" updatexsl.$$ updated.xml > updated.$$

mv updated.$$ updated.xml

rm -f updated.$$
rm -f updatexsl.$$

exit 0

Here is the output for the first update (184851):
Code:
$ ./update.sh 184851
$ cat updated.xml
<?xml version="1.0" encoding="utf-8"?>
<scbm-extract>
  <workOrderList>
    <workOrder>
      <branchCode>2022:100</branchCode>
      <workOrderCode>184851</workOrderCode>
    </workOrder>
  </workOrderList>
</scbm-extract>
$

and here is the output for the second update (184852):
Code:
$ ./update.sh 184852
$ cat updated.xml
<?xml version="1.0" encoding="utf-8"?>
<scbm-extract>
  <workOrderList>
    <workOrder>
      <branchCode>2022:100</branchCode>
      <workOrderCode>184851</workOrderCode>
    </workOrder>
    <workOrder>
      <branchCode>2023:100</branchCode>
      <workOrderCode>184852</workOrderCode>
    </workOrder>
  </workOrderList>
</scbm-extract>

# 4  
Old 01-25-2011
Hi fmmurphy,

You simply awesome!!!!!!!!!!!!!

Solution what you have provided is exactly meets our requirement - Thank you for your time spent on this
As i am new to xlsproc..still trying to understand the good tool.

Could you please help us on the below case how to achieve soltuion?

Case: We have 2 XML file 1. ORIGINAL.xml file and 2. ATTRIBUTE.xml files,
In the ORIGINAL.xml we need some modification as <resourceCode>431048</resourceCode>under
<item type="Manufactured"> tag - we need to grab the 431048 value from tag and pass it to database table in unix shell script
to find the model name and in our case the model number 22V50FM1,
then we have to search in ATTRIBITE.xml file under <assignedAttributeList> tags,
if found we have to copy the entire structure <assignedAttributeList> details and replace in
ORIGINAL.xml file for the tag <assignedAttributeList></assignedAttributeList> under the resource code 431048.
The output should like MODIFIED.xml
----------------------------------------------------------------------------------------------------------------
ORIGINAL.xml
----------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<ps-persistence version="12.1.3">
<scenarioList>
<scenario>
<scenarioCode>Schedule #1</scenarioCode>
<model>
<resourceCode>LIGHT</resourceCode>
<itemList>
<item type="Manufactured">
<resourceCode>431048</resourceCode>
<saleable>yes</saleable>
<startLevel>0.000000</startLevel>
<minimumLevel>0.000000</minimumLevel>
<maximumLevel>1000000.000000</maximumLevel>
<stockoutCost>100.000000</stockoutCost>
<carryingCost>5.000000</carryingCost>
<safetyStockViolationCost>20.000000</safetyStockViolationCost>
<precision>0</precision>
<modelWorkspaceTreePath>Manufacured and Saleable</modelWorkspaceTreePath>
<relaxedMinLevel>no</relaxedMinLevel>
<relaxedMaxLevel>yes</relaxedMaxLevel>
<planningUom>Each</planningUom>
<receivingCalendarCode>Default</receivingCalendarCode>
<assignedAttributeList></assignedAttributeList>
<timeVaryingEventList></timeVaryingEventList>
</item>
</itemList>
<itemList>
<item type="Manufactured">
<resourceCode>431049</resourceCode>
<saleable>yes</saleable>
<startLevel>0.000000</startLevel>
<minimumLevel>0.000000</minimumLevel>
<maximumLevel>1000000.000000</maximumLevel>
<stockoutCost>100.000000</stockoutCost>
<carryingCost>5.000000</carryingCost>
<safetyStockViolationCost>30.000000</safetyStockViolationCost>
<precision>0</precision>
<modelWorkspaceTreePath>Manufacured and Saleable</modelWorkspaceTreePath>
<relaxedMinLevel>no</relaxedMinLevel>
<relaxedMaxLevel>yes</relaxedMaxLevel>
<planningUom>Each</planningUom>
<receivingCalendarCode>Default</receivingCalendarCode>
<assignedAttributeList></assignedAttributeList>
<timeVaryingEventList></timeVaryingEventList>
</item>
</itemList>
</model>
</scenario>
</scenarioList>
</ps-persistence>

----------------------------------------------------------------------------------------------------------------
ORIGINAL.xml
----------------------------------------------------------------------------------------------------------------
<assignedAttributeList>
<assignedAttribute>
<attributeCode>CrimperType</attributeCode>
<attributeValueCode>2</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Dia</attributeCode>
<attributeValueCode>18"</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Base</attributeCode>
<attributeValueCode>22V50FM1</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Fuel</attributeCode>
<attributeValueCode>Gas</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Module</attributeCode>
<attributeValueCode>ME05</attributeValueCode>
</assignedAttribute>
</assignedAttributeList>
<assignedAttributeList>
<assignedAttribute>
<attributeCode>CrimperType</attributeCode>
<attributeValueCode>2</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Dia</attributeCode>
<attributeValueCode>19"</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Base</attributeCode>
<attributeValueCode>22V50FM2</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Fuel</attributeCode>
<attributeValueCode>Gas</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Module</attributeCode>
<attributeValueCode>ME05</attributeValueCode>
</assignedAttribute>
</assignedAttributeList>

----------------------------------------------------------------------------------------------------------------
MODIFIED.xml
----------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<ps-persistence version="12.1.3">
<scenarioList>
<scenario>
<scenarioCode>Schedule #1</scenarioCode>
<model>
<resourceCode>LIGHT</resourceCode>
<itemList>
<item type="Manufactured">
<resourceCode>431048</resourceCode>
<saleable>yes</saleable>
<startLevel>0.000000</startLevel>
<minimumLevel>0.000000</minimumLevel>
<maximumLevel>1000000.000000</maximumLevel>
<stockoutCost>100.000000</stockoutCost>
<carryingCost>5.000000</carryingCost>
<safetyStockViolationCost>20.000000</safetyStockViolationCost>
<precision>0</precision>
<modelWorkspaceTreePath>Manufacured and Saleable</modelWorkspaceTreePath>
<relaxedMinLevel>no</relaxedMinLevel>
<relaxedMaxLevel>yes</relaxedMaxLevel>
<planningUom>Each</planningUom>
<receivingCalendarCode>Default</receivingCalendarCode>
<assignedAttributeList>
<assignedAttribute>
<attributeCode>CrimperType</attributeCode>
<attributeValueCode>2</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Dia</attributeCode>
<attributeValueCode>18"</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Base</attributeCode>
<attributeValueCode>22V50FM1</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Fuel</attributeCode>
<attributeValueCode>Gas</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Module</attributeCode>
<attributeValueCode>ME05</attributeValueCode>
</assignedAttribute>
</assignedAttributeList>
<timeVaryingEventList></timeVaryingEventList>
</item>
</itemList>
<itemList>
<item type="Manufactured">
<resourceCode>431049</resourceCode>
<saleable>yes</saleable>
<startLevel>0.000000</startLevel>
<minimumLevel>0.000000</minimumLevel>
<maximumLevel>1000000.000000</maximumLevel>
<stockoutCost>100.000000</stockoutCost>
<carryingCost>5.000000</carryingCost>
<safetyStockViolationCost>30.000000</safetyStockViolationCost>
<precision>0</precision>
<modelWorkspaceTreePath>Manufacured and Saleable</modelWorkspaceTreePath>
<relaxedMinLevel>no</relaxedMinLevel>
<relaxedMaxLevel>yes</relaxedMaxLevel>
<planningUom>Each</planningUom>
<receivingCalendarCode>Default</receivingCalendarCode>
<assignedAttributeList>
<assignedAttribute>
<attributeCode>CrimperType</attributeCode>
<attributeValueCode>2</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Dia</attributeCode>
<attributeValueCode>19"</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Base</attributeCode>
<attributeValueCode>22V50FM2</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Fuel</attributeCode>
<attributeValueCode>Gas</attributeValueCode>
</assignedAttribute>
<assignedAttribute>
<attributeCode>Module</attributeCode>
<attributeValueCode>ME05</attributeValueCode>
</assignedAttribute>
</assignedAttributeList>
<timeVaryingEventList></timeVaryingEventList>
</item>
</itemList>
</model>
</scenario>
</scenarioList>
</ps-persistence>
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving XML tag/contents after specific XML tag within same file

Hi Forum. I have an XML file with the following requirement to move the <AdditionalAccountHolders> tag and its content right after the <accountHolderName> tag within the same file but I'm not sure how to accomplish this through a Unix script. Any feedback will be greatly appreciated. ... (19 Replies)
Discussion started by: pchang
19 Replies

2. Shell Programming and Scripting

Read xml tags and then remove the tag using shell script

<Start> <Header> This is header section </Header> <Body> <Body_start> This is body section <a> <b> <c> <st>111</st> </c> <d> <st>blank</st> </d> </b> </a> </Body_start> <Body_section> This is body section (3 Replies)
Discussion started by: RJG
3 Replies

3. Shell Programming and Scripting

How to remove html tag which has multiple lines in SHELL?

I want to clean a html file. I try to remove the script part in the html and remove the rest of tags and empty lines. The code I try to use is the following: sed '/<script/,/<\/script>/d' webpage.html | sed -e 's/<*>//g' | sed '/^\s*$/d' > output.txt However, in this method, I can not... (10 Replies)
Discussion started by: YuhuiFeng
10 Replies

4. Shell Programming and Scripting

Print a closing XML tag shell script

I have a shell script that does everything I need it to do. But, when I was testing it I realized it doesn't print the closing XML tag.... Does anyone know how to incorporate printing the XML tag with my script? I am using AWK any help would be appreciated. (4 Replies)
Discussion started by: risarose87
4 Replies

5. Shell Programming and Scripting

Modify XML tag using shell script

Hi All Need some help with a unix shell script. I have a XML file as shown below: <Root> <Service> <endPoint type="SOAP" protocol="http"> <provider>ABCD</provider> <urlRewrite>/service/xyz/getAccountDetails</urlRewrite> <timeout>30</timeout> </endPoint> </Service> <Service> <endPoint... (3 Replies)
Discussion started by: abhilwa
3 Replies

6. Shell Programming and Scripting

How can I remove some xml tag lines using shell script?

Hi All, My name is Prathyu and I am working as a ETL develper. I have one requirement to create a XML file based on the provided XSD file. As per the Datastage standards Key(repeatable) field does not contain any Null values so I am inserting some dummy tag line to that XML file. ... (14 Replies)
Discussion started by: Prathyu
14 Replies

7. UNIX for Advanced & Expert Users

Shell Script to read XML tags and the data within that tag

Hi unix Gurus, I am really new to Unix Scripting. Please help me to create a shell script which reads the xml file and from that i need to fetch a particular information. For example <SOURCE BUSINESSNAME ="" DATABASETYPE ="Teradata" DBDNAME ="DWPROD3" DESCRIPTION ="" NAME... (2 Replies)
Discussion started by: SmilePlease
2 Replies

8. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

9. Shell Programming and Scripting

shell command to remove some XML tag is needed

Hi all, I have a file which i have to remove some line from it, the lines that i have to remove from my file is as below: </new_name></w"s" langue="Fr-fr" version="1.0" encoding="UTF-8" ?> <New_name> and it is finding at the middle of my file, is there any command line in linux to do it or do... (10 Replies)
Discussion started by: id_2pc
10 Replies

10. Shell Programming and Scripting

How to remove xml namespace from xml file using shell script?

I have an xml file: <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> and I have to remove the portion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" only. I tried using sed... (10 Replies)
Discussion started by: Gary1978
10 Replies
Login or Register to Ask a Question