Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added the option -a

...

No Format
groovy -i .bak -pe "if(count == 1) println '#!/usr/bin/groovy'" *.groovy

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:

No Format

ps aux|groovy -ane "if(split[0] =~ 'root')println split[10..-1]"

Print all logins from /etc/passwd that are not commented:

No Format

groovy -a':' -ne "if(!(split[0] =~ /^#/))println split[0]" /etc/passwd

Add the first and the penultimate column of a file:

No Format

groovy -ape "split[0].toInteger()+split[-2].toInteger()" accounts.txt

For more examples or inspiration browse through the search results for Perl One Liners

...