Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of replacing placeholders in multiple files with sed
#!/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
You can’t perform that action at this time.