Skip to content

Instantly share code, notes, and snippets.

@jimkang
Last active August 18, 2022 05:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimkang/4c636302815924e5aa995ccfecc068a8 to your computer and use it in GitHub Desktop.
Save jimkang/4c636302815924e5aa995ccfecc068a8 to your computer and use it in GitHub Desktop.
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