Groovy: Read contents of a file in an array and grep for something

Yash picture Yash · May 23, 2017 · Viewed 9.3k times · Source

I am trying to implement following in GROOVY script but getting errors: Read contents of an HTML file in an array and then grep for something in that array.

def file1 = new File("/path/to/the/file/xyz.html");
def lines = file1.readLines()
if ((-e "/path/to/the/file/xyz.html") and (!(grep /jira.bugtracker.com/, lines))
{
    println (" the changes are present\n");
    exit 0;
}
else
{
    println (" the changes are not present\n");
    exit 1;
}

Please review the code and suggest the correct method.

Answer

daggett picture daggett · May 23, 2017
def file1 = new File("/path/to/the/file/xyz.html" )
def lines = file1.readLines()
def found = lines.find{ line-> line =~ /jira.bugtracker.com/ }
println ("the changes are ${ found ? '' : 'not ' }present")
return found ? 0 : 1