Home > Back-end >  Need help in regular expression using sed
Need help in regular expression using sed

Time:10-17

I have following code:-

test -z "$(echo ${JIRA_DETAIL} | jq '.fields.status.name' | sed -r "s/\"(Done|Completed|Closed)\"//")" && echo "Found valid Jira" || echo "Not a valid Jira (Closed/Completed/Done)" 

The code works. When the status is one of the among (done, completed or closed) then it prints "Found valid Jira". I want the "Not In" condition of it i.e. whenever the status comes out to be any of these then it should say "Found valid Jira".

Jira details response that I get is as follows:-

{
   "expand":"renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
   "id":"10000",
   "self":"https://mycloudbox.atlassian.net/rest/api/2/issue/10000",
   "key":"MYC-1",
   "fields":{
      "status":{
         "self":"https://mycloudbox.atlassian.net/rest/api/2/status/3",
         "description":"This issue is being actively worked on at the moment by the assignee.",
         "iconUrl":"https://mycloudbox.atlassian.net/images/icons/statuses/inprogress.png",
         "name":"In Progress",
         "id":"3",
         "statusCategory":{
            "self":"https://mycloudbox.atlassian.net/rest/api/2/statuscategory/4",
            "id":4,
            "key":"indeterminate",
            "colorName":"yellow",
            "name":"In Progress"
         }
      }
   }
}

I tried going through with docs but it is confusing. I'm not good in regex hence checking. How to put ^ or Not In condition on this?

[Updated defective Code using Linux If-Else] It is going every time in else condition and printing Valid JIRA Id found!. Pls suggest as I want to try out using Linux If-Else only, thanks

- |
      if [[ "$JIRA_STATUS" == "^(Done|Completed|Closed)$" ]] 
       then 
         echo "Invalid JIRA (Done/Completed/Closed) found!"
         exit 1
       else echo "Valid JIRA Id found!" 
      fi 

CodePudding user response:

if (.fields.status.name | IN("done", "completed", "closed")) 
    then "Found valid Jira" 
    else "Found INVALID Jira" 
end

Will output Found valid Jira if the .field.status.name is found in one of the params passed to IN(), otherwise, print Found INVALID Jira

JqPlay Demo


Note that IN() is case-sensitive, use ascii_downcase to convert the name to lowercase, to simplify the cases:

if (.fields.status.name | ascii_downcase | IN("done", "completed", "closed")) 
    then "Found valid Jira" 
    else "Found INVALID Jira" 
end

CodePudding user response:

Why not do this with jq directly?

if .fields.status.name | IN("Done","Completed","Closed")
then "Found valid Jira"
else "Not a valid Jira (Closed/Completed/Done)"
end

Complete example:

printf '%s' "$JIRA_DETAIL" | jq -r 'if .fields.status.name | IN("Done","Completed","Closed")
then "Found valid Jira"
else "Not a valid Jira (Closed/Completed/Done)"
end'

Or shorter, but more obscure:

(.fields.status.name | select(IN("Done","Completed","Closed")) | "Found valid Jira")
// "Not a valid Jira (Closed/Completed/Done)"
{
    true: "Found valid Jira",
    false: "Not a valid Jira (Closed/Completed/Done)"
}[.fields.status.name | IN("Done","Completed","Closed") | tostring]

Another option is to have a map from your status names to outputs (more flexible, but for this simple case it uses quite a lot of repetition):

{
    Done: "Found valid Jira",
    Completed: "Found valid Jira",
    Closed: "Found valid Jira"
}[.fields.status.name] // "Not a valid Jira (Closed/Completed/Done)"
  • Related