在BASH 中,我运行以下一行代码,以便在给定字符上分割后获得单独的列/字段(如果使用AWK,也可以使用if他们想要在多个字符上分割,即以任何顺序在单词上分割,确定)。如何获得给定列以外或之前或之后的有序列,定义列或所有列
#This will give me first column i.e. 'lori' i.e. first column/field/value after splitting the line/string on a character '-' here
echo "lori-chuck-shenzi" | cut -d'-' -f1
# This will give me 'chuck'
echo "lori-chuck-shenzi" | cut -d'-' -f2
# This will give me 'shenzi'
echo "lori-chuck-shenzi" | cut -d'-' -f3
# This will give me 'chuck-shenzi' i.e. all columns after 2nd and onwards.
echo "lori-chuck-shenzi" | cut -d'-' -f2-
注意上面的最后一个命令,我怎样才能做到在Groovy中相同的最后cut
命令狗屎?
对于例如:如果内容是在一个文件中,他们看起来像:
1 - a
2 - b
3 - c
4 - d
5 - e
6 - lori-chuck shenzi
7 - columnValue1-columnValue2-columnValue3-ColumnValue4
我尝试以下Groovy代码,但它不是给我lori-chuck shenzi
(即忽略了第六子弹,第一次出现后,在-
的,我希望我的输出为lori-chuck shenzi
和下面的脚本将返回我只是lori
(这是givning我正确的输出作为我的指标是在下面的代码[1]
,所以我知道这一点)。
def file = "/path/to/my/file.txt"
File textfile= new File(file)
//now read each line from the file (using the file handle we created above)
textfile.eachLine { line ->
//list.add(line.split('-')[1])
println "Bullet entry full value is: " + line.split('-')[1]
}
// return list
另外,如果我可以使用Groovy代码在分割后更改列的顺序,即颠倒Python的顺序,我们可以在上面的文件的最后一行中找到简单的方法[1:],[ :1],[:-1]等。或者以某种方式
如果你愿意,你也可以调用.reverse()。例如:line.split(' - ')[1 ..- 1] .reverse() – Daniel
实际上安静的关闭,但现在我在主动选择插件的可用选择列表中获得的是,而不是获得STRING,即'lori-chuck shenzi,我得到了'[lori,chuck,shenzi]'即它创建了一个LIST,这不是我正在寻找的。我也尝试过'return list.toString()',但现在我在Jenkins中看不到任何Active Choice插件(我在运行这个常规代码的地方)。 –