Skip to content

Instantly share code, notes, and snippets.

@Fordi
Last active March 12, 2026 20:30
Show Gist options
  • Select an option

  • Save Fordi/126b41a872fbb3bea6b687820d9daa43 to your computer and use it in GitHub Desktop.

Select an option

Save Fordi/126b41a872fbb3bea6b687820d9daa43 to your computer and use it in GitHub Desktop.
Script to in-place modify any file using a pipe transform
#!/usr/bin/env bash
FILE="$1"; shift
SUB="${SUB:-{}}"
if [[ -z "$FILE" || $# -eq 0 || "$1" == "-h" || "$1" == "--help" ]]; then cat >&2 << EOF
Revise a file in-place. Basically, sed's \`-i\` flag, but for any command.
Usage:
revise ๐˜ง๐˜ช๐˜ญ๐˜ฆ ...๐˜ค๐˜ฎ๐˜ฅ
the content of ๐˜ง๐˜ช๐˜ญ๐˜ฆ will be passed to ๐˜ค๐˜ฎ๐˜ฅ on stdin, or if \`${SUB}\` is present in the argument list,
the filename will be passed. The substitution string can be changed by setting the SUB
environment variable.
examples:
# Revise \`data.json\` by running \`jq '.foo = "bar" data.json'\` and capturing its output
revise data.json jq '.foo = "bar"' {}
# Same, but feeding the content of \`data.json\` through stdin instead of by filename reference.
revise data.json jq '.foo = "bar"'
EOF
exit; fi
CMD=();
PIPE=(cat "$FILE")
while [[ $# -gt 0 ]]; do
ARG=("${1/"$SUB"/"$FILE"}"); shift;
[[ "$ARG" != "$1" ]] && PIPE=(echo -n)
done
cat <<< $("${PIPE[@]}" | "${CMD[@]}") > "${FILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment