user3702188
user3702188

Reputation: 69

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:

    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?

Upvotes: 4

Views: 5510

Answers (2)

Vipul Sonar
Vipul Sonar

Reputation: 51

def last_text = .tokenize().last()

echo ${last_text} //for printing last text value

Upvotes: 0

Evgeny Smirnov
Evgeny Smirnov

Reputation: 3016

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

result = stdout.tokenize().last()

Upvotes: 5

Related Questions