-2

I am attempting to add configuration to an xml file inside docker (which I believe is unimportant) I have the configuration pieces I want to insert into the xml file in another file.

The configuration file looks a little like this

<datasources>
                <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true" statistics-enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}">
                    <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
                    <driver>h2</driver>
                    <security>
                        <user-name>sa</user-name>
                        <password>sa</password>
                    </security>
                </datasource>
</datasources>

The file conataining the text I wish to insert looks a little like this:

<datasource jndi-name="java:/PostgresDS" pool-name="PostgresDS">
                    <connection-url>jdbc:postgresql://localhost:5432/tabzpoc</connection-url>
                    <driver>postgresql-42.4.2.jar</driver>
                    <security>
                        <user-name>sa</user-name>
                        <password>sa</password>
                    </security>
                </datasource>

I want the contents of the second file to be inserted after the <datasources> tag in the first file. I have tried a few separate ways with sed and awk but do not seem to be able to nail it. If someone could point me in the right direction, I would be grateful

Damien Cooke
  • 599
  • 10
  • 20
  • Hi what did you try so far? From the way your files look, a simple awk or sed line might do. However, since you are dealing with xml, it might be beneficial to search for an `xmlstarlet` solution. [This question](https://stackoverflow.com/questions/57645561) has a rather acceptable answer that might point you in the correct direction. – kvantour Aug 19 '22 at 09:51
  • This might help: https://stackoverflow.com/questions/16715373/insert-contents-of-a-file-after-specific-pattern-match – kvantour Aug 19 '22 at 15:25

2 Answers2

-1

If you don't want to use any external xml parsing library, you could replace '<datasources>' with '<datasources>' + the contents of the second file and write the result in a new file.

Code:

first_file=$(cat first.xml)

second_file=$(cat second.xml)

# In first_file, we search the "<datasources>" string and we replace it with "<datasources>"+second_file
result_file=${first_file/<datasources>/<datasources>$second_file}

echo $result_file > result_file.xml
Davide
  • 1,931
  • 2
  • 19
  • 39
  • 1
    I would not recommend this solution at all. there are about an infinite ways how this can go seriously wrong. – kvantour Aug 19 '22 at 09:45
  • 1
    We're not speaking about the best solution here but a functional one. In fact I even wrote "if you don't want to use xml libraries". In any case, of course it is not the best solution, but reading that it is a PoC the solution must be functional in the first place, and then it can find the best solution itself. Of course for future users who come to stackoverflow and copy and paste this code it is slightly problematic – Davide Aug 19 '22 at 10:00
-1

Don't know if this will answer your question or not as it isn't too specific. There are a few things to consider. Depending on your operating system, mac uses a different sed than linux does. the linux version of sed is gsed on mac devices, it can be downloaded through brew. I use sed on linux and gsed on mac to write paths of config files so I can sync them all to github.

https://github.com/CatInAHatIsBack/Linux_Scripts_Config.git

The script for linux is under /scripts/configSync.sh and for mac is M1_Conf/scripts/M1_Conf.sh

It isnt documented, but it does give an example of how i use sed, and might be helpful. I could write some docs if you are interested.

example usecase is:

~/.config/htop Con -f htoprc -i config

Con is just a bash alias for the script with this written to what is in my case a text file:

Directory config:
/Users/name/.config/htop/htoprc

It supports inserting multiple times under same directory, just pushing lines down

Cat Hat
  • 35
  • 6