简体   繁体   中英

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

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.

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

you can try like this.

if ( new File("/path/to/the/file/xyz.html").text?.contains("jira.bugtracker.com")){
   println (" the changes are present\n");
} else {
   println (" the changes are not present\n");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM