The Groovy command line (groovy or groovy.bat) is the easiest way to start using the Groovy Language.
If you have a groovy script, you can edit and run the script immediately.
Here is an example with your own command line arguments.
However you can also run such a simple groovy program by providing the script in the command line arguments.
This may not look useful, but it fits in with the UNIX tradition of chaining simple programs together to build powerful commands. Tools like perl, sed, awk and grep do these jobs very well. But many users have limited experience with these tools' arcane syntax and will be more familiar with Java and therefore Groovy.
Because looping through STDIN or input files tends to be a common thing to do, groovy (and ruby, perl etc) provide shortcuts for this. currently broken, groovy not flushing output (still so 060927?)
-n will loop through each line of the input, and provide it to your script in the line variable.
If we definitely want to print the output of each line we can use -p and shorten it to
We can use the looping constructs along with -i, which writes the output back to the original files (and creates a backup copy with the given extension). And wreak havoc on our local file system, with wide-scale search and replace.
TIP: Never ever use the option -i without a backup extension.
Or to really get into groovy (literally)
Additionally you have access to the line number in the current file you are reading via the variable count. This can be used for a number of convenient groovy one-liners.
Let us assume you want to prefix every line in a file with the line number. Doing this requires next to no work in Groovy (we additionally create a copy of the original file with the extension .bak).
Or let us create a grep-like command that prints the line number where it found matching strings for a regular expression.
Print the first 50 lines of all files:
until one file is longer than 50 lines:
Add a Groovy-Shebang (the string '#!/usr/bin/groovy') to all Groovy files:
Another very convenient option is -a, which splits the current input line into the array split. By default the split pattern is " " (one space). The option -a optionally takes another split pattern which is then used instead.
Print processes owned by root:
Print all logins from /etc/passwd that are not commented:
Add the first and the penultimate column of a file:
For more examples or inspiration browse through the search results for Perl One Liners
Another groovy command line option is the ability to startup groovy in listen mode, which will attach groovy to a TCP port on your machine (-l <port> with a default port of 1960).
For each connection that is made to this port, groovy executes the supplied script on a line by line basis.
This oneliner will reverse every line that is thrown at it, try telnet to your machine on port 1960 to interact with this script.
you can combine the -p option from earlier, to automatically print the result of your script
The following one liner is equivalent to the one liner immediately above.
More examples of useful command line scripts in SVN