Skip to content

Instantly share code, notes, and snippets.

@dedeibel
Last active July 1, 2019 16:55
Show Gist options
  • Save dedeibel/bb69574318fc9d4427f3 to your computer and use it in GitHub Desktop.
Save dedeibel/bb69574318fc9d4427f3 to your computer and use it in GitHub Desktop.
apply given command to file and replacing it with it's content - sponge is used to replace it inline without copying
#!/bin/bash
set -e
if [ "$#" -lt 2 ]; then
>&2 echo "Usage: $0 FILE COMMAND [ARGS...]"
exit 1
fi
target="$1"
shift
"$@" < "$target" | sponge "$target"
#
# Example:
#
# # remove lines containing the word three
# apply_inline testfile.txt grep -v three
#
# # iconv-ert all files inline
# find . -type f -name "*yaml" -exec apply_inline "{}" iconv -f latin1 -t utf8 \;
#
# # remove lines containing the word "three" from matching files
# find . -type f -name "*txt" -exec apply_inline "{}" grep -v three \;
#
# Works with nested folders.%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment