Skip to content

Instantly share code, notes, and snippets.

@albfan
Created February 14, 2018 15:15
Show Gist options
  • Save albfan/63761ddae10f301afcc1f52b3d132c36 to your computer and use it in GitHub Desktop.
Save albfan/63761ddae10f301afcc1f52b3d132c36 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
seenReplace=''
for arg in "$@"; do
if test "$arg" == '--replace'; then
seenReplace='true'
break
fi
done
if test -z "$seenReplace"; then
echo 'You must specify the --replace argument!'
exit 1
fi
currentFile=''
didChange=''
(
rg \
--context 999999 \
--with-filename --heading --null \
--color=never --no-line-number \
"$@"
echo -e '\n\0'
) |
{
while IFS= read -r -d '' part; do
if test -n "$currentFile"; then
echo "$currentFile"
head -n -2 <<< "$part" > "$currentFile"
didChange='true'
fi
currentFile="$(tail -n 1 <<< "$part")"
done
if test -z "$didChange"; then
echo "No files were changed."
exit 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment