Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

groovy capture stdout last line

Tags:

jenkins

groovy

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

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

    stage('Generate'){
       stdout = bat([
           returnStdout: true, 
           script: 'C:/folder/generate.exe --environment %ENVIRONMENT% --optionalproperties %OPTIONALPROPS%',
           encoding: 'UTF-8'
       ]).trim();

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 12345678

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?

like image 420
user3702188 Avatar asked Sep 14 '25 22:09

user3702188


1 Answers

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

result = stdout.tokenize().last()
like image 122
Evgeny Smirnov Avatar answered Sep 17 '25 20:09

Evgeny Smirnov