Last active
July 1, 2019 16:55
-
-
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
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
#!/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