[英]Groovy: Read a line from a text file into an array using groovy
上下文:我正在编写一个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”行的“值”写入数组时,我需要帮助。
也许像:
def theFile = new File("$JobPath/Input_params.txt")
def linesWithDetails = theFile.findAll {
it.contains 'SOME_DETAILS'
}
def testArray=[]
new File("/11/tmp/1.txt").splitEachLine("[:;]"){line->
if(line[0]=='SOME_DETAILS')testArray=line[1..-1]
}
println testArray
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.