簡體   English   中英

Groovy:使用groovy將文本文件中的一行讀入數組

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM