4

We have a jenkins server with too many jobs and most of the jobs have Build trigger as Poll SCM. I want to remove that trigger for all the jobs. Looking for a easy way to do that. I see that whenever the build trigger property is set the config.xml has this-

<triggers>
    <hudson.triggers.SCMTrigger>
      <spec>
        # poll infrequently and rely on Stash webhooks to trigger builds
        @daily
      </spec>
      <ignorePostCommitHooks>false</ignorePostCommitHooks>
    </hudson.triggers.SCMTrigger>
</triggers>

Whenever the Poll SCM flag is set to false this same element in config.xml is as follows-

 <triggers/>

I am looking for an easy way using sed to replace the above triggers tag with this but am not able to get it using sed.

I tried this from the jobs folder and it does not work-

sudo find . -name "config.xml" -print0 | sudo xargs -0 sed -i '' -e 's|<triggers>.*<\/triggers|<triggers\/>|g'

So basically I want to replace the entire contents of triggers element above along with start and end triggers tag with only this

<triggers/>
datastax
  • 144
  • 1
  • 2
  • 13
  • Where's the `sed` attempts you've tried so far? – l'L'l Jul 07 '17 at 21:39
  • Tried this and it did not work- sudo find . -name "config.xml" -print0 | sudo xargs -0 sed -i '' -e 's|.*<\/triggers||g' – datastax Jul 07 '17 at 21:40
  • It's unclear what exactly you want to change (from -> to); maybe add that to your question. From the looks of your regex pattern it seems you want to wipe out everything between the triggers tags and replace with ``? – l'L'l Jul 07 '17 at 21:47
  • 1
    One more question, is the number of lines between the tags always the same? – l'L'l Jul 07 '17 at 21:51
  • If the Poll SCM tag is set with that condition then its always the same set of lines. If the condition differs then the content of the element will change but rest of the child xml elements will stay the same. – datastax Jul 07 '17 at 22:05
  • 1
    Great opportunity to refer once again to [... the pestilent slithy ... h̵i​s un̨ho͞ly radiańcé destro҉ying all enli̍̈́̂̈́ghtenment ...](https://stackoverflow.com/a/1732454/1744774). – Gerold Broser Jul 08 '17 at 01:00

1 Answers1

7

sed is a stream editor and reads the file line by line. Your pattern spans multiple lines in the file, which is why it never matches.

You should either make sed read the whole file into the pattern space before attempting the substitution...

sed ':a;N;$!ba; s|<triggers>.*<\/triggers>|<triggers\/>|g' example.xml

... or use another tool (preferably suited for XML files, like xmlstarlet):

xmlstarlet ed -O -u /triggers -v '' example.xml
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • 1
    The sed example worked perfectly fine. Thanks @Lev Levitsky for suggesting this. This was very useful to me and tons of jobs are changed in a blink. Thanks. – datastax Jul 07 '17 at 22:14