Warm tip: This article is reproduced from stackoverflow.com, please click
amazon-ecs groovy jenkins

String Manipulation in Groovy

发布于 2020-04-16 12:05:35

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

Questioner
Devendra Prakash Date
Viewed
53
Mark Bramnik 2020-02-04 16:22

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()}​​​​​​