Groovy:使用groovy从文本文件中读取一行到数组中

时间:2017-05-25 06:10:33

标签: jenkins groovy jenkins-pipeline

上下文: 我正在编写一个jenkins文件,我想在其中读取Input_params.txt文件,然后搜索关键字,将关键字的值打印到数组中,然后打印数组的每个元素。

输入参数(格式为"KEY:VALUE")文件的内容为:

sh> cat Input_params.txt
SOME_DETAILS:1234-A0;1456-B1;2456-B0;3436-D0;4467-C0

步骤1:将SOME_DETAILS存储在数组中:

Integer[] testArray = ["1234-A0", "1456-B1", "2456-B0" , "3436-D0" , "4467-C0"]

步骤2:按顺序打印数组的元素。例如:

testArray.each {
println "chip num is ${it}"
}

示例代码:

println ("Check if the "Key:Value" is present in the Input_params.txt \n");
if ( new File("$JobPath/Input_params.txt").text?.contains("SOME_DETAILS"))
{
   println ("INFO: "Key:Value" is present in the info_file.txt \n");
  >>>>> Code to write the "value" of line with key "SOME_DETAILS" into an array here.. <<<<< 
}

我需要帮助编写代码,将带有“SOME_DETAILS”键的行的“值”写入数组。

2 个答案:

答案 0 :(得分:0)

可能是这样的:

def theFile = new File("$JobPath/Input_params.txt")

def linesWithDetails = theFile.findAll {
    it.contains 'SOME_DETAILS'
}

答案 1 :(得分:0)

def testArray=[]
new File("/11/tmp/1.txt").splitEachLine("[:;]"){line->
    if(line[0]=='SOME_DETAILS')testArray=line[1..-1]
}
println testArray