Tuesday, February 1, 2022

[SOLVED] Print text between two non-static strings

Issue

I want to automate a script that parses an XML file and copy a section of it. I searched and found a way to do that but its working only with fields like

<title> .... </title>

My aim is to copy this

<datasource enabled="true" jndi-name="java:/db_namePostgresDS" jta="true" pool-name="db_namePostgresDS" spy="false" use-ccm="false" use-java-context="true">
    THINGS AND FIELDS IN HERE
</datasource>

and paste it just after </datasource>. Then I will change values with sed. But I basically want to double that section.

I just can't get how to do it, and maybe it's an XY Problem. Any help?

EXAMPLE:

I have

<datasource enabled="true" jndi-name="java:/db_namePostgresDS" jta="true" pool-name="db_namePostgresDS" spy="false" use-ccm="false" use-java-context="true">
THINGS AND FIELDS IN HERE

and I want to have

<datasource enabled="true" jndi-name="java:/db_namePostgresDS" jta="true" pool-name="db_namePostgresDS" spy="false" use-ccm="false" use-java-context="true">
    THINGS AND FIELDS IN HERE
</datasource>

<datasource enabled="true" jndi-name="java:/MODIFIED_NAME_HERE_PostgresDS" jta="true" pool-name="db_namePostgresDS" spy="false" use-ccm="false" use-java-context="true">
    MODIFIED THINGS AND FIELDS IN HERE
</datasource>

Important: I need to avoid installing new software on the machine (explicit customer request). XML parsers, if not built-in, aren't the way.


Solution

I would use sed to extract the multiline xml tag :

orig_datasource=$(sed -n '/<datasource/{: l;N;/<\/datasource>/!bl;p}' your_input_file)

This command starts aggregating lines once it encounters the opening <datasource tag and prints the result once it has aggregated up to the closing </datasource> tag. *

The XML tag would be captured in a orig_datasource variable that I could then both use as-is and modified :

modified_datasource=$(echo "$orig_datasource" | sed 's/something/else/');
echo "$orig_datasource

$modified_datasource" > target_file

* : There are a lot of ways it could fail (i.e. < datasource> is a valid tag opening that wouldn't be understood as such by the sed command), but since it looks like you're working on a configuration file from JBoss EAP or Wildfly you should be safe since these tools reformat their configuration file at launch. Still, it's safer and easier to use an XML parser when possible than to parse the data as text.



Answered By - Aaron
Answer Checked By - Robin (WPSolving Admin)