Skip to content

Instantly share code, notes, and snippets.

@jorgerance
Created January 15, 2020 03:22
Show Gist options
  • Save jorgerance/2e235826265ed269e061402cfd62e444 to your computer and use it in GitHub Desktop.
Save jorgerance/2e235826265ed269e061402cfd62e444 to your computer and use it in GitHub Desktop.
[findAndReplace.sh] Recursive find files with specific extension within multiple dirs and replace multiple strings. Currently using it on Jekyll builds #jekyll #sed #find #replace #bash #ruby #helper #build
#!/bin/bash
# Source bash output formatting helper
source ${BASH_HELPERS}/msg_format.sh
# Strings to replace before building site "orignal1:::new1" "original2:::new2"
declare -a stringsToReplace=("original1:::new1" "original2:::new2")
for string in "${stringsToReplace[@]}"; do
# split original string from new string
stringsArray=(${string//:::/ })
originalString=${stringsArray[0]}
newString=${stringsArray[1]}
msg_info "Replacing string ${originalString} with ${newString}\n"
# Replace each <PATHX> occurence for a path where to look in
# Replace <EXTENSION> for the extension you're looking for
# or replace '.+\.md$' with '.+' if no filter should be applied
gfind . -type f -regextype posix-egrep -regex "\.\/(<PATH1>|<PATH2>|<PATH3>|<PATH4>|||)/.+\.<EXTENSION>$" \
-print0 | \
gxargs -0 \
gsed -i "s|${originalString}|${newString}|g"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment