Last active
April 29, 2021 22:36
-
-
Save drewbrokke/5252c81c725fc3cf518defd2766171ca to your computer and use it in GitHub Desktop.
This file contains 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 | |
error() { | |
echo "$1" && exit 1 | |
} | |
while getopts "k:s:t:" FLAGS; do | |
case $FLAGS in | |
k) KEYS="${OPTARG}" ;; | |
s) SOURCE_DIRECTORY="${OPTARG}" ;; | |
t) TARGET_DIRECTORY="${OPTARG}" ;; | |
*) exit 1 ;; | |
esac | |
done | |
[ -n "${KEYS}" ] || error "Add a space-separated string of language keys with '-k'" | |
[ -n "${SOURCE_DIRECTORY}" ] || error "Add a source directory with '-s'" | |
[ -n "${TARGET_DIRECTORY}" ] || error "Add a target directory with '-t'" | |
[ -d "${SOURCE_DIRECTORY}" ] || error "Source directory does not exist: ${SOURCE_DIRECTORY}" | |
[ -d "${TARGET_DIRECTORY}" ] || error "Target directory does not exist: ${TARGET_DIRECTORY}" | |
KEYS_PATTERN="^${KEYS// /|}=" | |
for SOURCE_FILE in "${SOURCE_DIRECTORY}"/Language*.properties ; do | |
TARGET_FILE="${TARGET_DIRECTORY}/${SOURCE_FILE##*/}" | |
# If the target file exists, append a new line | |
if [ -s "${TARGET_FILE}" ]; then | |
echo >> "${TARGET_FILE}" | |
fi | |
# Append all the matching lines to the target file | |
grep -E "${KEYS_PATTERN}" "${SOURCE_FILE}" >> "${TARGET_FILE}" | |
# Remove the matching lines from the source file | |
sed -Ei "/${KEYS_PATTERN}/d" "${SOURCE_FILE}" | |
# If on MacOS, install 'gnu-sed' with brew and call 'gsed' instead: | |
# gsed -Ei "/${KEYS_PATTERN}/d" "${SOURCE_FILE}" | |
done | |
echo 👍 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script uses
gsed
, which is just GNUsed
installed on MacOS. If you're on a Linux distro you can just replace it withsed
.