Last active
March 12, 2026 20:30
-
-
Save Fordi/126b41a872fbb3bea6b687820d9daa43 to your computer and use it in GitHub Desktop.
Script to in-place modify any file using a pipe transform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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