Skip to content

Instantly share code, notes, and snippets.

@davidlukac
Created March 30, 2019 17:46
Show Gist options
  • Save davidlukac/d318444e10f548fc8ac36a1a77aea6c6 to your computer and use it in GitHub Desktop.
Save davidlukac/d318444e10f548fc8ac36a1a77aea6c6 to your computer and use it in GitHub Desktop.
Bash - Iterating over directory structure and replacing pattern from input list
LIST_FILENAME="input-list.txt"
SERVICES=$(cat ${LIST_FILENAME})
NO_SERVICES=$(cat ${LIST_FILENAME} | wc -l)
FILES=(`find . -type f -not -path "./.git/*" -prune -not -path "./script.sh" -not -path "./${LIST_FILENAME}" | sort`)
NO_FILES="${#FILES[@]}"
FILE_NO=1
echo "Processing ${NO_SERVICES} services and ${NO_FILES} files."
# Iterate over all files.
for F_FILE in ${FILES[@]}
do
echo "Replacing all services in file ${F_FILE}."
# Iterate over all services.
ITEM_NO=1
for SERVICE in ${SERVICES}
do
SERVICE_TRIMMED="$(echo -e "${SERVICE}" | tr -d '[:space:]')";
printf "Processing item ${ITEM_NO} of ${NO_SERVICES} - ${SERVICE_TRIMMED} - in file ${F_FILE} (${FILE_NO}/${NO_FILES})...";
sed -i -e "s/${SERVICE_TRIMMED}.foobar.com/${SERVICE_TRIMMED}.prod.foobar.com/g" ${F_FILE}
let "ITEM_NO++";
printf " done.\n"
done
echo
let "FILE_NO++"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment