A: 

http://groovy.codehaus.org/JN1525-Strings -- it seems that the python style quoting ie "'" (') and """"""" (") you expect doesn't exist in groovy so ' or " in strings literals must be written as \' and \"

Dan D
Thanks, but backslash quoting doesn't seem to help, see the updated example.
Charlie Martin
+1  A: 

When you execute commands with String.execute(), they don't get parsed by a command shell. The quotes are passed through to the actual commands executed; in this case, grep and awk.

This is illustrated by replacing grep and awk with echo:

print " 1: " ; "echo something".execute().text.eachLine {println it }          ; println ""
print " 2: " ; "echo 'something'".execute().text.eachLine {println it }        ; println ""
print " 3: " ; """echo something""".execute().text.eachLine {println it }      ; println ""
print " 4: " ; """echo "something" """.execute().text.eachLine {println it }   ; println ""
print " 5: " ; """echo 'something'""".execute().text.eachLine {println it }    ; println ""

Which results in:

 1: something

 2: 'something'

 3: something

 4: "something"

 5: 'something'

A simple workaround is to build the command line as a list of strings:

["awk", 'BEGIN { print "hello" }'].execute().text

If you need finer control over the how the command is parsed, take a look at ProcessBuilder, the Java class String.execute() is built around.

ataylor