regex - shell script to recognise jira key -


below lines in jenkins job configuration execute shell retrieves jira key

jira_key=$(curl --request "http://jenkins-server/job/myproject/job/mysubproject/job/mycomponent/${build_number}/api/xml?xpath=/*/changeset/item/comment" | sed -e "s/<comment>\(.*\)<\/comment>/\1/")  jira_key=$(echo $jira_key | cut -c10-17) 

but in case if text doesn't start jira key per current logic assign text in range of 10-17. need store empty string "" in variable jira_key when jira key not present in <comment>, how can that?

here xml

<freestylebuild _class="hudson.model.freestylebuild">     <changeset _class="hudson.plugins.git.gitchangesetlist">         <item _class="hudson.plugins.git.gitchangeset">             <comment>                 jra-1011 commit                  message.             </comment>         </item>     </changeset> </freestylebuild> 

as mentioned in comment section not clear output need, based on assumptions, please try following , let me know on same. i- if need strings between run following.

awk '/<\/comment>/{a="";next}/<comment>/{a=1;next}a'  input_file 

ii- if need find jra string between following.

awk '/<\/comment>/{a="";next}/<comment>/{a=1;next} && /jra/{match($0,/[a-za-z]+[^ ]*/);print substr($0,rstart,rlength)}'  input_file 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -