Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract value containing in a line from a text file using groovy

i want to extract a value of token in a line from a text file.

The line from the text file is:

<response>{"timestamp": "2013-11-12T14:55:43Z", "data": {"profile": null, "ticket": "f644231-6d46-44c7-add6-de3a4422871e", </response>

groovy file is:

    // read the file from path
    def file = new File('c:/tmp/response.txt')
    // for example read line by line
    def data= file.eachLine { line ->
        // check if the line contains your data 
        if(line.contains('"ticket":')){
            return line     
        }
    }
testRunner.testCase.testSuite.project.setPropertyValue('ticket',data)

So here it's not storing any value in my variable ticket. Any help, please

like image 214
Templog Log Avatar asked Nov 12 '15 18:11

Templog Log


1 Answers

Replace

def data= file.eachLine { line ->
    // check if the line contains your data 
    if(line.contains('"ticket":')){
        return line     
    }
}

With

def data= file.filterLine { line ->
    line.contains('"ticket":')
}
like image 188
tim_yates Avatar answered Sep 27 '22 19:09

tim_yates