[英]How can you store a specific line from a file in an array in Groovy?
I am interested if there is a easy way to store a specific line from a file into an array in Groovy (i need it for GroovyAxis in Jenkins).我很感兴趣,如果有一种简单的方法可以将文件中的特定行存储到 Groovy 中的数组中(我需要它用于 Jenkins 中的 GroovyAxis)。 The file would look like this:该文件将如下所示:
var1="value1 value2 etc"
var2="a b etc"
var3="test1 test2 test3 etc"
I would need test1 test2 test3 etc from var3 to be stored in an array.我需要将 var3 中的 test1 test2 test3 等存储在数组中。 Right now i use this:现在我用这个:
def words = []
new File( '/home/workstation/jenkins/params' ).eachLine { line ->
words << line
}
But it stores each line i have into an array, so i have to heavily workaround the config file to get the job done.但是它将我拥有的每一行存储到一个数组中,因此我必须大量解决配置文件才能完成工作。
Thank you very much非常感谢
You are very close!你非常接近!
def words = [:]
new File( '/home/workstation/jenkins/params' ).eachLine { line ->
(var, value) = line.split('=')
words << [(var): value.split(' ')]
}
The result is a map of arrays.结果是数组映射。 The key is the variable name and the value is an array.键是变量名,值是一个数组。
Oh, it's a property file...哦,这是一个属性文件...
Properties properties = new Properties()
File propertiesFile = new. File('/home/workstation/jenkins/params')
propertiesFile.withInputStream { properties.load(it) }
def result = properties.var3.split(' ').collect { item.minus('"') }
return result
ConfigSlurper
can be useful to load properties/configurations : ConfigSlurper
可用于加载属性/配置:
def config = new ConfigSlurper(new File("my.conf").toURL())
assert config.var2 == "a b etc"
//cat /tmp/words.txt
//It's must be file Format
//SOME_DETAILS:1234-A0;1456-B1;2456-B0;3436-D0;4467-C0
stage("test"){
def testArray=[]
new File('/tmp/words.txt').splitEachLine("[:;]"){line->
if(line[0]=='SOME_DETAILS')testArray=line[1..-1]
}
println testArray
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.