Skip to content

Instantly share code, notes, and snippets.

@simonewebdesign
Last active March 3, 2022 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonewebdesign/d28237829a2511fa0b462555dd28f2d4 to your computer and use it in GitHub Desktop.
Save simonewebdesign/d28237829a2511fa0b462555dd28f2d4 to your computer and use it in GitHub Desktop.
Easy Search & Replace on an entire file (or STDIN) using sed

Even better is probably to have a script.sh file which looks like this (same thing but more readable):

#!/bin/bash
sed "s/FNAME/Simone/g;\
     s/LNAME/Vittori/g;\
     s/EMAIL/myemail@foobar.com/g;"

Just chmod +x script.sh and then use it like:

cat example-template.html | bin/script.sh > compiled.html ; and open compiled.html

The best solution I could find

If you need to replace the placeholder with contents from a file:

FILE3=$(<src/template.html)
FILE2=$(<src/main.js)
FILE1=$(<src/style.css)
OUT1="${FILE2//STYLESHEET/$FILE1}"

echo "${OUT1//HTML_TEMPLATE/$FILE3}" > dist/main.js

Put the above in compile.sh, chmod +x compile.sh and run with ./compile.sh.

Source: https://unix.stackexchange.com/a/164284/25941

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example Document</title>
</head>
<body>
<!-- This example is using Mandrill template syntax -->
<p>Hello *|FNAME|*,</p>
<p>*|MANAGER|* has sent you this file, please do have a look.</p>
<p>Best regards,<br />Board Intelligence</p>
</body>
</html>
cat example-template.html | sed "s/*|FNAME|\*/Simone/g; s/*|MANAGER|\*/John/g"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment