Last active
August 18, 2022 05:29
-
-
Save jimkang/4c636302815924e5aa995ccfecc068a8 to your computer and use it in GitHub Desktop.
Example of replacing placeholders in multiple files with sed
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 | |
# Assuming $title and $name are read from input. | |
# ... | |
# Escaping space characters is only necessary within the shell script. | |
# If you're running the find...sed command directly in the shell, you don't need to escape spaces. | |
cleanedtitle="${title// /\\ }" | |
# Replace the placeholders with the title (with escape characters inserted). | |
find . -type f \( -name '*.html' -o -name '*.js' \) | xargs sed -i "s/__PROJECTTITLE__/$cleanedtitle/g" | |
find . -type f \( -name '*.md' -o -name '*.js' -o -name '*.json' -o -name 'Makefile' \) | xargs sed -i "s/__PROJECTNAME__/$name/g" | |
# On OS X, the '' is needed by OS X sed: | |
# find . -type f \( -name '*.html' -o -name '*.js' \) | xargs sed -i '' "s/__PROJECTTITLE__/$cleanedtitle/g" | |
# find . -type f \( -name '*.md' -o -name '*.js' -o -name '*.json' -o -name 'Makefile' \) | xargs sed -i '' "s/__PROJECTNAME__/$name/g" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment