user3702188 user3702188 - 3 months ago 57
Groovy Question

groovy capture stdout last line

I have issues extracting stdout to get only the result, last(second line).

i have jenkins pipeline using groovy script which executes the following:

stdout = bat([
returnStdout: true,
script: 'C:/folder/generate.exe --environment %ENVIRONMENT% --optionalproperties %OPTIONALPROPS%',
encoding: 'UTF-8'

if i pass echo stdout, to capture what this command generated, i get stdout as -

C:\folder2\folder2>C:/folder1/generate.exe --environment PROD --optionalproperties something

So my result is in new line, 12345678. I need to capture only this.

I used before to do this with:
result = stdout.readLines().drop(1).split(" ")
and i was getting just the 12345678. But it stopped working somehow.

I managed to find a workaround with this:

result = stdout.reverse().take(8).reverse()

which takes last 8 numbers and extracts them. But it's not good solution as i might have more or less amount of numbers generated, so i need a proper way to extract it.

Any advise guys what i could try else as i dont get why readLines() fails to get me any result, though the batch command didnt change?

Answer Source

In other words you need to get last word of output. So you can do:

result = stdout.tokenize().last()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download