Hello everyone,
I'm a beginner in shell scripting and try to solve my issues myself, but now I am at a point where I need your help.
Below is an excerpt from an xml file.
<Position>
<SKU>A/370269/10432/32D</SKU>
<Batch>00320160501</Batch>
<Description>Carlotta</Description>
<Amount>1</Amount>
<ForeignPos1>172991</ForeignPos1>
<ForeignPos2>10</ForeignPos2>
<Positionreferences>
<PosReference number="1"/>
<PosReference number="2"/>
<PosReference number="3">0</PosReference>
<PosReference number="4">0</PosReference>
<PosReference number="5"/>
<PosReference number="6">370269</PosReference>
<PosReference number="7">10432</PosReference>
<PosReference number="8">pearl grey</PosReference>
<PosReference number="9"/>
<PosReference number="10"/>
<PosReference number="11">26 INCH</PosReference>
</Positionreferences>
</Position>
<Position>
<SKU>A/370269/10432/32D</SKU>
<Batch>00520160501</Batch>
<Description>Carlotta</Description>
<Amount>6</Amount>
<ForeignPos1>172992</ForeignPos1>
<ForeignPos2>10</ForeignPos2>
<Positionreferences>
<PosReference number="1"/>
<PosReference number="2"/>
<PosReference number="3">0</PosReference>
<PosReference number="4">0</PosReference>
<PosReference number="5"/>
<PosReference number="6">370269</PosReference>
<PosReference number="7">10432</PosReference>
<PosReference number="8">pearl grey</PosReference>
<PosReference number="9"/>
<PosReference number="10"/>
<PosReference number="11">26 INCH</PosReference>
</Positionreferences>
</Position>
At the moment I use my own written script to change the lines <PosReference number="9"/> and <PosReference number="10"/> with a positional parameter.
#!/bin/bash
if [ $# -lt 2 ] ; then
echo "Usage: `basename $0` <Filename> <Datafield8>"
else
# initialize variable for filename
file=$1
# convert xml file in readable format
xmllint --format $file > $file.tmp
# split string from $2 in seperate variables
IFS="/"
Wert=$2
set -- $Wert
p9=$2
p10=$1
# simple sed replace but output to a new file
sed 's@NEW@UPD@' $file.tmp > $file.tmp2
sed 's@<PosReference number="9"/>@<PosReference number="9">'$p9'</PosReference>@' $file.tmp2 > $file.tmp3
sed 's@<PosReference number="10"/>@<PosReference number="10">'$p10'</PosReference>@' $file.tmp3 > $file.tmp4
# append prefix from original file name with "_script"
new=`echo $file | sed 's@.xml@_script.xml@'`
# check if $new is empty or not
if [ -z "$new" ]
then
:
else
cp $file.tmp4 $new
fi
# delete all temporary files
rm -f $file.tmp $file.tmp2 $file.tmp3 $file.tmp4
fi
As long as the batch is the same in the xml file everything is okay, but it often happens that there a different batches, where each batch has a different $2 value.
Now I wonder if it's possible to modify the script, that I can search for a specific batch or more and change the associated rows which are always 14 and 15 lines below.
Maybe like this:
basename $0 <Filename> <batch#1> <parameter#1> <batch#2> <parameter#2> <batch#3> <parameter#3>
Has anyone a solution or hint for me please?
Thanks in advance,
Jacko