Peter Stuifzand

Schwartzian transform in shell and vim

A few days ago I had to create list of email addresses sorted on the domainname. All addresses with the same name should clump together. Being a Perl programmer, I quickly wanted to write a Perl script to sort this list of data. Then I tought, why not use Vim to look at the intermediate steps and do it with filtering. The following is the result.

:%!perl -pe 'm/\@([-\w]+)\./; $_="$1\#$_";'
:%!sort
:%!cut -d\# -f2

The first line parsed the domain from the data and pastes it in front of the orignal, seperating it with a ‘#’. The second line obviously sorts the new list. The final line removes the domain that I put in front. This leaves a list of the originals.

:%!perl -pe 'm/\@([-\w]+)\./; $_="$1\#$_";' | sort | cut -d\# -f2

This will do it in one line.

© 2023 Peter Stuifzand