I am writing a groovy script which will return me the list of Task-Definition in AWS ECS service, Here is the code snippet for the same
def p = 'aws ecs list-task-definitions --family-prefix test'.execute() | 'jq .taskDefinitionArns[]'.execute()
p.waitFor()
print p.text
and the output is
"arn:aws:ecs:us-west-2:496712345678:task-definition/test:2"
"arn:aws:ecs:us-west-2:496712345678:task-definition/test:3"
Now I want to capture only the last part of the result, i.e test:2, test:3 and so on without double quotes
How can I do that using Groovy language as I have to use it in Jenkins's active choice reactive parameter plugin
Assuming:
def text = "arn:aws:ecs:us-west-2:496712345678:task-definition/test:2" + "\n" + "arn:aws:ecs:us-west-2:496712345678:task-definition/test:3"
Try :
text.split("\n").collect {c -> c.split("/").last()}
This prints a list of [test:2, test:3]
If you want it in one line and not in an list, use:
text.split("\n").collect {c -> c.split("/").last()}.join(",")
This prints: test:2,test:3
Update
Due to OP's comment, the answer after all should look something like:
def p = 'aws ecs list-task-definitions --family-prefix test'.execute() | 'jq .taskDefinitionArns[]'.execute()
p.waitFor()
def text = p.text
println text.split("\n").collect {c -> c.split("/").last()}
def p = 'aws ecs list-task-definitions --family-prefix test'.execute() | 'jq .taskDefinitionArns[]'.execute() p.split("\n").collect {c -> c.split("/").last()}.join(",")
but I am getting the following errorCaught: groovy.lang.MissingMethodException: No signature of method: java.lang.UNIXProcess.split() is applicable for argument types: (String) values: [ ]
In the question there is p.waitFor() and p.text to get a text. save it in variable and then try the snippet I've provided in the answer
I've updated the answer in any case
It worked !!! I have got the following result
[test:2", test:3"]
but I want it liketest2 test3
below each otherThen use: text.split("\n").collect {c -> c.split("/").last()}.join("\n")