Reputation: 95
I am writing a groovy method to delete new line characters from a file.
def removeEmptyLines(filePath){
cmd="""sed -i '/^\$/d' ${filePath}
"""
println cmd
result = cmd.execute()
result.waitFor()
println result.exitValue()
}
removeEmptylines("/path/to/file.txt")
my output is:
sed -i '/^$/d' /path/to/file.txt
1
the file remains unedited and obviously the exit status is non-zero. however, if i literally copy and paste the first output into command line, it works. I'm using absolute paths.
My guess is that groovy doesn't like the regex characters in my command. how do i get around this?
Upvotes: 0
Views: 462
Reputation: 37033
Remove the single quotes (you dont need to quote for the shell, because no shell is involved).
String.execute()
is usually not what you want, because it splits at whitespace. So you are better off with either ["sed", "-i", ... ].execute()
or if you need shellisms use ["sh", "-c", "sed -i '..."].execute()
.
Upvotes: 1