Open Source Consultant, Software Developer and System Administrator

If you are interrested in hiring a consultant for the effective use of open source software on an enterprise grade, take a look around in the About section to see, what I have to offer.

Blog and snippets

Various snippets or code parts I found useful, so I keep them here for reference.

TIL: Using the Groovy CLI like Perl/AWK/...

While most of the time using groovysh as a REPL or groovyc to quickly compile a few files, I never realised that groovy comes with several features mimicking features from classic CLI tools like Perl.

The scripts to run on the input can either be passed as -e <s> where <s> is an argument with the actual Groovy code - or just pass a file name to load as script.

Filter lines like Perl

It's easy to write single line filters. The same works with groovy -ne 'script...'. The script gets line (the full line) and count (the current line number starting at 1). E.g.:

# head -n3 /etc/passwd | groovy -ne 'println "${count}. ${line}"'
1. root:x:0:0:root:/root:/bin/bash
2. daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
3. bin:x:2:2:bin:/bin:/usr/sbin/nologin

There is also an option -a <s> to split the lines. If <s> is not given, a space is used. The array will be available as split in the script. E.g.:

# head -n3 /etc/passwd | groovy -n -a : -e 'println "${split[4]} uses ${split[6]}"'
root uses /bin/bash
daemon uses /usr/sbin/nologin
bin uses /usr/sbin/nologin

Begin and end AWK like

If the script to run contains a method begin and/or end, then those will be called at the beginning or end of processing. E.g.:

// stuff.groovy
def begin() {
    println "Start"

def end() {
    println "End"

println line
# head -n1 /etc/passwd | groovy -n stuff.groovy

Transforming file content SED like

Given the flag -i <.suffix>, any arguments passed as files will be processed by the given script (and a <file.suffix> backup is stored. E.g.:

# echo World > 1.txt                               
# groovy -i .bak -p -e 'println "Hello ${line}"' 1.txt
# cat 1.txt                                           
Hello World
# cat 1.txt.bak 

Process data from sockets

Given the flag -l <port>, a socket server will be spawned and will run the given script against the data send from clients. E.g. start the server:

# groovy -l 5000 -e 'println "Hello, ${line}"'
groovy is listening on port 5000

And then send data to it:

# echo "World" | nc -w 1 localhost 5000
Hello, World

REST-REPL fully integrated into WREPL

WREPL is a tool I have wrote and used for a long time now. It grew out of the frustration to be able to quickly compose Clojure REPLs for different tasks or projects and provide an solid developer experience for an interactive REPL (no editor integration etc needed, because once we are in Clojure project land, all I need is the editor integration).

Another reason for initially starting this project was to integrate the REST-REPL in it. The REST-REPL is a tool I use very often especially in projects outside the world of Clojure, because it gives very quick feedback in web services. The only downside with it was, that there is not much to configure or even expand (there is the clumsy way of adding more jars to the classpath and running code in your external files).

Long story short: it's now possible to build your own REST-REPL inside WREPL.