"sed: '/' 不一致" エラーを修正する方法

Jenkinsで実行されるGroovyスクリプトで次のコマンドを実行したいと思います。

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

おすすめ記事