I do have a file like this:

<receiver name="anything" type="com.mycompany.plugin.Timer">
  <property name="interval">5000</property>
  <handler name="ReplaceThis" type="com.mycompany.handler.FileBuilderHandler">
    <property name="buildSomething">true</property>
    <handler name="AlsoReplaceThis" type="com.mycompany.handler.FileExporter">
      <property name="overide">true</property>

<receiver name="anything/other" type="com.mycompany.plugin.Timer">
  <property name="intervall">1000</property>
  <handler name="ThisMustBeReplaceAsWell" type="com.mycompany.handler.FileWatcher">
    <property name="interval">2000</property>
    <handler name="ThisMustBeReplaceAsWell" type="com.mycompany.converter.CSConverter">
      <property name="mandant">CE</property>
      <handler name="RemoveThis" type="com.mycompany.handler.FileExporter">
        <property name="outputDir">/usr1/import</property>

Now I want all lines with the following beginning

 <handler name="

to be replace from

 <handler name="ReplaceThis" type="com.mycompany.handler.FileBuilderHandler">

to someting like this

 <handler name="FileBuilderHandler" type="com.mycompany.handler.FileBuilderHandler">

So the attribute "name" should have the Classname (last part of the type attribute)

Does anyone has an idea how to solve that with an sed, awk, grep command?

  • XML is not regular; using regular expressions to attempt to parse them is the beginning of a descent into madness. Use a tool designed to faff about with XML (e. g. a DOM parser such at, for instance, xmlstarlet) to perform operations such as this. See stackoverflow.com/a/1732454/1421075
    – DopeGhoti
    Nov 15, 2018 at 15:26

1 Answer 1


For input structured identical to your sample, this might work:

$ sed -E 's/(handler name=")[^"]*"( *type="([^.]*\.)*([^.]*)">)/\1\4"\2/' file

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.