#!/usr/bin/env bash escape() { sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$1"; } export -f escape escapeReplace() { sed 's/[&/\]/\\&/g' <<<"$1"; } export -f escapeReplace searchfile() { LINKS=$(grep -oP '(?<=\]\().+?(?=[\)| ])' "$1") for link in $LINKS; do if echo $link | grep -E '^http' >/dev/null; then continue fi if echo $link | grep -E ".md$" && find . -name "$link"; then continue fi echo $link FILES=$(find . -iname "*$(basename $link)*") echo "$FILES" | cat --number read PICK FILE=$(basename $(echo "$FILES" | head -n $PICK | tail -n 1) .md) sed -i "s/$(escape '('$link')')/\($(escapeReplace $FILE)\)/" "$1" done } export -f searchfile find . -iname "*.md" -exec bash -c 'searchfile "$0"' {} \;