[英]grep adds new line if splitted by groovy?
My goal is to get the names of x .conf files in a directory called "conf".我的目标是在名为“conf”的目录中获取 x .conf 文件的名称。 My code looks like this:我的代码如下所示:
commandOutput = sshCommand remote: getRemote(), command: getPathCmd(type) + "cd conf; ls | grep '.conf' | grep -Eo '^[a-zA-Z0-9_-]+'"
def instances = commandOutput.split("\n") as String[]
return instances
If i print the variable commandOutput
(with two files in the directory) it shows me:如果我打印变量commandOutput
(目录中有两个文件),它会显示我:
name1
name2
But when i make a for loop to print every slot of the array, i get this:但是当我创建一个 for 循环来打印数组的每个插槽时,我得到了这个:
name1
*empty line*
and和
name2
I checked commandOutput[0]
with .endsWith("\\n")
and returned false
so i dont know where the new line is coming from.我用.endsWith("\\n")
检查commandOutput[0]
并返回false
所以我不知道新行来自哪里。 Is there an obvious reason for that behaviour?这种行为有明显的原因吗?
Edit: It also happens here:编辑:它也发生在这里:
command = getPathCmd(type) + getScript(type, "status") + "| grep -Eo '[0-9]{4,5}' | sort -u | grep -v '${params.xyID}\\|" + globalID + "'"
try{
commandOutput = sshCommand remote: getRemote(), command: command
def PIDs = commandOutput.split("\n") as String[]
return PIDs
}
catch(Exception e){
println("XY")
return "XY"
}
Print of commandOutput
: commandOutput
打印:
1234
5678
For loop print of PIDs
: For PIDs
循环打印:
1234
*empty line*
and和
5678
stringXY = stringXY.replaceAll("[\\\\\\r\\\\\\n]+","");
did the trick.成功了。 It replaces all new lines.它替换所有新行。
tr '\\n' '\\0'
as suggested in this thread wouldn't work here, because it would be harder to find out where one name ends and the next one starts.此线程中建议的tr '\\n' '\\0'
在这里不起作用,因为要找出一个名称的结束位置和下一个名称的开始位置会更困难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.