Groovy:读取数组中文件的内容并grep查找内容

时间:2017-05-23 11:10:40

标签: jenkins groovy jenkins-pipeline groovyshell

我正在尝试在GROOVY脚本中实现以下但是收到错误: 读取数组中HTML文件的内容,然后grep读取该数组中的内容。

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;
}

请查看代码并提出正确的方法。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

你可以这样试试。

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");
}