0

Question

I have a huge pom.xml from a Talend DI/ESB project, where it was possible to have multiple lines for the same module with different Talend specific versions (don't even ask for that :-( )

So, I would like to cleanup the pom.xml so, that all module lines will be deleted except the one with the highest number at the end. In my example the line with

jobs/routes/specialbookings_0.7

should stay, all other module lines of the same module should be deleted. The problem is, that for ex. 0.10 is less than 0.9 in a nummeric way.

I'm not a regex profi, but I think a good approach would be using sed?!

Thank you in advance.

Example part of the pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>de.example.com</groupId>
  <artifactId>code.Master</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>
  <name>Codes Master</name>
  <url>http://www.example.com/</url>
  <modules>
    ...
    <module>jobs/routes/reservation_0.5</module>
    <module>jobs/routes/reservation_0.9</module>
    <module>jobs/routes/reservation_0.10</module>
    <module>jobs/routes/reservation_0.11</module>
    <module>jobs/routes/reservation_1.0</module>
    <module>jobs/routes/reservation_1.10</module>
    <module>jobs/routes/reservation_1.11</module>
    ...
    <module>jobs/routes/specialbookings_0.1</module>
    <module>jobs/routes/specialbookings_0.2</module>
    <module>jobs/routes/specialbookings_0.3</module>
    <module>jobs/routes/specialbookings_0.4</module>
    <module>jobs/routes/specialbookings_0.5</module>
    <module>jobs/routes/specialbookings_0.6</module>
    <module>jobs/routes/specialbookings_0.7</module>
    ...
    <module>jobs/routes/employee_0.1</module>
    <module>jobs/routes/employee_0.2</module>
    <module>jobs/routes/employee_0.3</module>
    <module>jobs/routes/employee_1.0</module>
    <module>jobs/routes/employee_1.1</module>
    <module>jobs/routes/employee_1.10</module>
    <module>jobs/routes/employee_1.2</module>
    <module>jobs/routes/employee_1.3</module>
    <module>jobs/routes/employee_1.4</module>
    <module>jobs/routes/employee_1.5</module>
    <module>jobs/routes/employee_1.6</module>
    <module>jobs/routes/employee_1.7</module>
    <module>jobs/routes/employee_1.8</module>
    <module>jobs/routes/employee_1.9</module>
    <module>jobs/routes/employee_2.0</module>
    <module>jobs/routes/employee_2.1</module>
    <module>jobs/routes/employee_2.2</module>

    <module>jobs/routes/article2erpmodel_0.1</module>
    <module>jobs/routes/article2erpmodel_0.10</module>
    <module>jobs/routes/article2erpmodel_0.11</module>
    <module>jobs/routes/article2erpmodel_0.12</module>
    <module>jobs/routes/article2erpmodel_0.13</module>
    <module>jobs/routes/article2erpmodel_0.14</module>
    <module>jobs/routes/article2erpmodel_0.15</module>
    <module>jobs/routes/article2erpmodel_0.16</module>
    <module>jobs/routes/article2erpmodel_0.17</module>
    <module>jobs/routes/article2erpmodel_0.18</module>
    <module>jobs/routes/article2erpmodel_0.19</module>
    <module>jobs/routes/article2erpmodel_0.2</module>
    <module>jobs/routes/article2erpmodel_0.20</module>
    <module>jobs/routes/article2erpmodel_0.3</module>
    <module>jobs/routes/article2erpmodel_0.4</module>
    <module>jobs/routes/article2erpmodel_0.5</module>
    <module>jobs/routes/article2erpmodel_0.6</module>
    <module>jobs/routes/article2erpmodel_0.7</module>
    <module>jobs/routes/article2erpmodel_0.8</module>
    <module>jobs/routes/article2erpmodel_0.9</module>
  </modules>
</project>

UPDATE

After the comment from @RomanPerekhrest I've searched for an exception of this rule and added it as last part to the above example.

1
  • 1
    Is "the highest number always at the end" ? Nov 15, 2019 at 14:19

1 Answer 1

2

Seams like you want something like this:

vim -c:1 -c'/<modules>' -c+1 -c':mark q' \
   -c'/<.modules>' -c-1 -c':mark w' \
   -c "'q,'w"'!sort -rV | sort -t_ -k1,1 -u' pom.xml

Running this from a script is not totally safe, there are a few assumptions which it includes. Some of them is

  • every modulename contains exactly one underscore before the version number
  • the xml is formatted like the above, the module and modules tag are in separate lines, and so on. If this is not the case, you have to use some xml prettier first.

Breaking down the command:

  • line 1 go to begin of file just to be sure, mark the first module with q
  • line 2 mark the last module with w
  • line 3 reverse sort by module name and version number
  • line 3 keeping only the first line from every version
1
  • Thank you! This works perfect.
    – aw-think
    Nov 16, 2019 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.