用groovy中的特殊字符“#”替换文件中的所有新行字符

Dev*_*ops 2 groovy jenkins jenkins-pipeline

如何在詹金斯管道中使用 groovy 用“#”替换文本文件中出现的所有“\n”

Chi*_*ids 5

这应该有效。在 Groovy 中使用查找运算符 ~

   def parsedtext = readFile("input.groovy").replaceAll(~/\n/, "#")
   writeFile file: "output.groovy", text: parsedtext
Run Code Online (Sandbox Code Playgroud)

编辑 如果您使用的是声明式管道语法,以下是工作代码。

pipeline {
    agent any
    stages {
        stage ('Print'){
            steps {
                script {
                     def inptext = readFile file: "1.groovy" 
                     inptext = inptext.replaceAll(~/\n/, "#")       
                     writeFile file: "2.groovy", text: inptext
                }

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)