我想在 groovy 脚本上运行以下命令,然后该脚本将在 Jenkins 上运行:
def sedCmd = cat sample.txt | sed -n -e /word id=123/,/end id = 123/ p
def logs = sedCmd.execute()
文件“sample.txt”如下所示:
Thurs 20 Sep 2018 word id=123
The cat
In the hat
Bla bla
Thurs 20 Sep 2018 end id=123
Test
当我运行命令时,出现以下错误:
sed: unmatched '/'
我在终端上本地测试了相同的命令,做了一些小的更改,它按预期工作:
cat sample.txt | sed -n -e '/word id=123/,/end id = 123/ p'
答案1
执行列表比执行单个字符串要好得多:
def sedCmd = ["sed", "-n", "/word id=123/,/end id=123/ p", "sample.txt"]
def process = sedCmd.execute()
process.waitFor()
process.err.readLines().each {line -> println "Err: $line"}
process.in.readLines().each {line -> println "Out: $line"}
Out: Thurs 20 Sep 2018 word id=123
Out: The cat
Out: In the hat
Out: Bla bla
Out: Thurs 20 Sep 2018 end id=123