简体   繁体   English

Groovy:使用groovy将文本文件中的一行读入数组

[英]Groovy: Read a line from a text file into an array using groovy

Context: I'm writing a jenkins file in which i want to read an Input_params.txt file, search for a keyword then, print the values of the keyword into an array and then print each element of the array. 上下文:我正在编写一个jenkins文件,在其中我想读取Input_params.txt文件,然后搜索关键字,将关键字的值打印到数组中,然后打印数组的每个元素。

The contents of input params (format is "KEY:VALUE" ) file are: 输入参数(格式为"KEY:VALUE" )文件的内容为:

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

Step-1: Store the SOME_DETAILS in an array: 步骤1:将SOME_DETAILS存储在数组中:

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

Step-2: print the elements of the array sequentially. 步骤2:依序列印阵列中的元素。 For example: 例如:

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

Sample code: 样例代码:

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

I need help in writing the Code to write the "value" of line with key "SOME_DETAILS" into an array. 在编写代码以将键“ SOME_DETAILS”行的“值”写入数组时,我需要帮助。

Maybe something like: 也许像:

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.

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