#!/usr/bin/env bash
# kwic -- KWIC concordancer
# Usage: kwic <pattern> <file> [context_words]
pattern="${1:?Usage: kwic <pattern> <file> [context_words]}"
file="${2:?Provide a file}"
ctx="${3:-5}"
grep -oP "(\S+\s+){0,$ctx}\S*${pattern}\S*(\s+\S+){0,$ctx}" "$file" | \
    sed "s/${pattern}/\x1b[1;31m&\x1b[0m/g"
