Skip to main content
deleted 1978 characters in body
Source Link

EDIT: I solved the problem by implementing the option with a temporary root node to make it valid and then using Gizmo as suggested. For anyone stumbling upon this later, here is my solution.

I downloaded the java version of Saxon Home Edition, unpacked it in a subfolder called "Saxon" and used the following script successfully.

#!/bin/bash

#remove script and generate it with the following lines (that way I only need
#to include my one .sh file)
rm -f script
echo update //@Torque with .*1.25>script
echo save temp_updated.xml method=xml indent=yes>>script

#we do the following to all xml files in the subfolder "classes/engines/"
for filename in classes/engines/*.xml
do
    #cleanup in case they got leftover, which is important for Gizmo,
    #otherwise I get a thousand "Overwrite File" prompts, which is
    #not conducive to automating the process.
    rm -f temp.xml
    rm -f temp_updated.xml

    #make a new temp file, starting with the temporary root element,
    #append the original file and close the root element
    echo "<xml_temp>">temp.xml
    cat $filename>>temp.xml
    echo "</xml_temp>">>temp.xml

    #call Saxon with the correct codepath, start the Gizmo subroutine or whatever
    #take the temporary file as input and "script" as the commands to be executed
    java -cp "Saxon/saxon-he-10.5.jar:Saxon/jline-2.14.6.jar" net.sf.saxon.Gizmo -s:temp.xml -q:script
    #sed -i for inplace editing of the file
    #the first expression '$d' removes the last line (the closing temp root tag)
    #the second impression removes the first line (Gizmo tags on some kind of xml info
    #and the second line, which contains the starting temp root tag
    sed -i -e '$d' -e 1,2d temp_updated.xml

    #I force overwrite the old file with the new and improved xml
    cp -f temp_updated.xml $filename
    #...and clean up after myself
    rm -f temp.xml
    rm -f temp_updated.xml
    rm -f script
done

EDIT: I solved the problem by implementing the option with a temporary root node to make it valid and then using Gizmo as suggested. For anyone stumbling upon this later, here is my solution.

I downloaded the java version of Saxon Home Edition, unpacked it in a subfolder called "Saxon" and used the following script successfully.

#!/bin/bash

#remove script and generate it with the following lines (that way I only need
#to include my one .sh file)
rm -f script
echo update //@Torque with .*1.25>script
echo save temp_updated.xml method=xml indent=yes>>script

#we do the following to all xml files in the subfolder "classes/engines/"
for filename in classes/engines/*.xml
do
    #cleanup in case they got leftover, which is important for Gizmo,
    #otherwise I get a thousand "Overwrite File" prompts, which is
    #not conducive to automating the process.
    rm -f temp.xml
    rm -f temp_updated.xml

    #make a new temp file, starting with the temporary root element,
    #append the original file and close the root element
    echo "<xml_temp>">temp.xml
    cat $filename>>temp.xml
    echo "</xml_temp>">>temp.xml

    #call Saxon with the correct codepath, start the Gizmo subroutine or whatever
    #take the temporary file as input and "script" as the commands to be executed
    java -cp "Saxon/saxon-he-10.5.jar:Saxon/jline-2.14.6.jar" net.sf.saxon.Gizmo -s:temp.xml -q:script
    #sed -i for inplace editing of the file
    #the first expression '$d' removes the last line (the closing temp root tag)
    #the second impression removes the first line (Gizmo tags on some kind of xml info
    #and the second line, which contains the starting temp root tag
    sed -i -e '$d' -e 1,2d temp_updated.xml

    #I force overwrite the old file with the new and improved xml
    cp -f temp_updated.xml $filename
    #...and clean up after myself
    rm -f temp.xml
    rm -f temp_updated.xml
    rm -f script
done
added 1962 characters in body
Source Link

EDIT: I solved the problem by implementing the option with a temporary root node to make it valid and then using Gizmo as suggested. For anyone stumbling upon this later, here is my solution.

I downloaded the java version of Saxon Home Edition, unpacked it in a subfolder called "Saxon" and used the following script successfully.

#!/bin/bash

#remove script and generate it with the following lines (that way I only need
#to include my one .sh file)
rm -f script
echo update //@Torque with .*1.25>script
echo save temp_updated.xml method=xml indent=yes>>script

#we do the following to all xml files in the subfolder "classes/engines/"
for filename in classes/engines/*.xml
do
    #cleanup in case they got leftover, which is important for Gizmo,
    #otherwise I get a thousand "Overwrite File" prompts, which is
    #not conducive to automating the process.
    rm -f temp.xml
    rm -f temp_updated.xml

    #make a new temp file, starting with the temporary root element,
    #append the original file and close the root element
    echo "<xml_temp>">temp.xml
    cat $filename>>temp.xml
    echo "</xml_temp>">>temp.xml

    #call Saxon with the correct codepath, start the Gizmo subroutine or whatever
    #take the temporary file as input and "script" as the commands to be executed
    java -cp "Saxon/saxon-he-10.5.jar:Saxon/jline-2.14.6.jar" net.sf.saxon.Gizmo -s:temp.xml -q:script
    #sed -i for inplace editing of the file
    #the first expression '$d' removes the last line (the closing temp root tag)
    #the second impression removes the first line (Gizmo tags on some kind of xml info
    #and the second line, which contains the starting temp root tag
    sed -i -e '$d' -e 1,2d temp_updated.xml

    #I force overwrite the old file with the new and improved xml
    cp -f temp_updated.xml $filename
    #...and clean up after myself
    rm -f temp.xml
    rm -f temp_updated.xml
    rm -f script
done

EDIT: I solved the problem by implementing the option with a temporary root node to make it valid and then using Gizmo as suggested. For anyone stumbling upon this later, here is my solution.

I downloaded the java version of Saxon Home Edition, unpacked it in a subfolder called "Saxon" and used the following script successfully.

#!/bin/bash

#remove script and generate it with the following lines (that way I only need
#to include my one .sh file)
rm -f script
echo update //@Torque with .*1.25>script
echo save temp_updated.xml method=xml indent=yes>>script

#we do the following to all xml files in the subfolder "classes/engines/"
for filename in classes/engines/*.xml
do
    #cleanup in case they got leftover, which is important for Gizmo,
    #otherwise I get a thousand "Overwrite File" prompts, which is
    #not conducive to automating the process.
    rm -f temp.xml
    rm -f temp_updated.xml

    #make a new temp file, starting with the temporary root element,
    #append the original file and close the root element
    echo "<xml_temp>">temp.xml
    cat $filename>>temp.xml
    echo "</xml_temp>">>temp.xml

    #call Saxon with the correct codepath, start the Gizmo subroutine or whatever
    #take the temporary file as input and "script" as the commands to be executed
    java -cp "Saxon/saxon-he-10.5.jar:Saxon/jline-2.14.6.jar" net.sf.saxon.Gizmo -s:temp.xml -q:script
    #sed -i for inplace editing of the file
    #the first expression '$d' removes the last line (the closing temp root tag)
    #the second impression removes the first line (Gizmo tags on some kind of xml info
    #and the second line, which contains the starting temp root tag
    sed -i -e '$d' -e 1,2d temp_updated.xml

    #I force overwrite the old file with the new and improved xml
    cp -f temp_updated.xml $filename
    #...and clean up after myself
    rm -f temp.xml
    rm -f temp_updated.xml
    rm -f script
done
Source Link

Trying to read XML attributes from multiple files and replacing them with a number 1.25 times greater than they were before

I am trying to write a bash script or something similar, that I can use to modify all occurrences in a bunch of xml files with one click. I have 24 xml files, each containing a few dozen lines and with the attribute Torque="SOMENUMBER" inside them about 3-5 times each. I would LIKE to replace those with 1.25 times their value to simply add 25% torque to every motor in this game.

(Doing it this way would be useful, since mods and game patches will overwrite these and I can quickly experiment with other values.)

I came up with the idea to use sed -n -e 's/Torque="\(.*\)"/\1/p' <filename.xml to extract the number, put it in a variable and then I would have no idea how to get it back in there at the right place and also the sed command above outputs all occurrences of Torque at once.

xmlstarlet could probably do it from what I have googled, but it always complains about the xml files containing more than one root element. Maybe I should somehow add a temporary tag <root_temp><\root_temp> around everything somehow, let xmlstarlet run its magic, remove the tags again and save the file? It's been years, since I dabbled with bash scripts, I would also be willing to do it with python, cpp, learn the basics of a new language, I don't care xD

Sample XML:

<_templates>
    <Engine>
        <RUScoutModernEngine BrakesDelay="0.5" />
    </Engine>
</_templates>
<EngineVariants>
    <Engine
        _template="RUScoutModernEngine"
        CriticalDamageThreshold="0.7"
        DamageCapacity="120"
        DamagedConsumptionModifier="1.2"
        EngineResponsiveness="0.35"
        FuelConsumption="1.5"
        Name="ru_scout_modern_engine_0"
        Torque="70000"
        DamagedMinTorqueMultiplier="1.0"
        DamagedMaxTorqueMultiplier="0.6"
        MaxDeltaAngVel="0.01"
    >
        <GameData
            Price="1900"
            UnlockByExploration="false"
            UnlockByRank="1"
        >
            <UiDesc
                PLACE="HOLDER"
            />
        </GameData>
    </Engine>
    <Engine
        BLA="BLA"
        Torque="80000"
        BLA="BLA"
    >
        <GameData
            Price="5500"
            UnlockByExploration="true"
            UnlockByRank="1"
        >
            <UiDesc
                PLACE="HOLDER"
            />
        </GameData>
    </Engine>
    <Engine
        BLA="BLA"
        Torque="76000"
        BLA="BLA"
    >
        <GameData
            BLA="BLA"
        >
            <UiDesc/>
        </GameData>
    </Engine>
</EngineVariants>