Skip to content

Instantly share code, notes, and snippets.

@drewbrokke
Last active April 29, 2021 22:36
Show Gist options
  • Save drewbrokke/5252c81c725fc3cf518defd2766171ca to your computer and use it in GitHub Desktop.
Save drewbrokke/5252c81c725fc3cf518defd2766171ca to your computer and use it in GitHub Desktop.
#!/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 👍
@drewbrokke
Copy link
Author

This script uses gsed, which is just GNU sed installed on MacOS. If you're on a Linux distro you can just replace it with sed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment