hpr4763 :: Solving anagrams with bash

Ken wites a bash script to solve the Dutch Railway station anagram challange.

Thumbnail of Ken Fallon
Hosted by Ken Fallon on Wednesday, 2026-11-04 is flagged as Clean and is released under a CC-BY-SA license.
reserve, NS, anagramtr, grep, sort, printf, awk, sed. (Be the first).

Listen in ogg, opus, or mp3 format. Play now:

Duration: 00:07:03
Download the transcription and subtitles.

general.

The Dutch Railway Company occasionally has a anagram of the names of the railway stations on their services.

The image above shows which service we are on, namely the intercity direct to Breda via Schiphol Airport , and the time is 07:27.

It shows a cartoon depiction of a platform where the information sign is displaying the letters of the anagram, and today it reads "Nul Te Teer".

Under it says "Oops, the letters are mixed up. Can you guess the correct station ?"

The under it says that the next station at 07:38 will be Duivendrecht .

I wrote a script to check my guess. It consists of a function normalise_word, which takes any word, removes everything but the letters, and then converts it to lower case. Each letter is printed to it's own line, where it's sorted, and new lines are removed, to be printed as a "normalised" word.

This is used to take the List of Dutch Railway Stations from Wikipedia , to produce a list of stations and convert them to normalized names which is saved to a new file with the normalised names on the same line as the real names.

We can then just grep for the normalised version of the anagram and whatever line it was found in the normalised file, should correspond to the same line in the real file.

"Nul Te Teer" gets converted to "eeelnrttu" which is found in the normalised file, and that corresponds to " Etten-Leur "

Tell us how would you solve it ?

#!/bin/bash
# Ken Fallon - CC-0/Public Domain

station_file="ns-train-stations.txt"

# Get the station name
if [ "$#" -lt 1 ]
then
echo "ERROR: Please specify station"
exit 1
fi

function normalise_word() {
printf '%sn' "${1}" 
| tr --delete --complement 'A-Za-zn' 
| tr '[:upper:]' '[:lower:]' 
| grep --only-matching --regexp='.' 
| sort --ignore-case 
| tr --delete 'n'
printf 'n'
}

station="$( normalise_word "${1}" )"

cat ${station_file} | while read word
do 
normalise_word "${word}"
done > "${station_file%.*}_normalised.txt"

linenumber="$( grep --line-number --perl-regexp "^${station}$" "${station_file%.*}_normalised.txt" | awk -F ':' '{print $1}' )"

sed --quiet "${linenumber}p" ${station_file}


Comments

Subscribe to the comments RSS feed.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?