#!/bin/bash

# ideone boilerplate: run in temp dir 
t=$(mktemp -d -t ideone.XXXXXXXX) || exit
trap 'rm -rf "$t"' ERR EXIT
cd "$t"

printf '%s\0' \
    b s n \
    a d q \
    a x w \
    aa y not \
    c p f \
    a z m >file

cat <<\: >prog.py
import sys

with open(sys.argv[1], 'rb') as fh:
    seq = fh.read().split(b'\x00')
items = [b'\x00'.join(seq[i:i + 3]) for i in range(0, len(seq)-1, 3)]
print(b'\x00'.join(sorted(items)).decode('us-ascii'))
:

python3 prog.py file | xxd

perl -0 -ne 'push @rec, $_;
  if ($#rec == 2) { push @items, join("", @rec); @rec = (); }
    END { print(join("", sort @items)) }' file |
xxd

