Last active
August 11, 2024 13:48
-
-
Save duggan/5765629 to your computer and use it in GitHub Desktop.
Using sed to to find/replace in a file in a portable way.
This file contains 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/sh | |
# Usage: replace "foo" "bar" "/etc/omg.conf" | |
replace() { | |
local pattern=$1 | |
local replacement=$2 | |
local file=$3 | |
local tempfile="$(mktemp -t temp.XXXXX)" | |
sed "s/$pattern/$replacement/" ${file} > ${tempfile} && mv ${tempfile} ${file} | |
} |
sed -i does in place replace, no need for manual rename
sed -i does in place replace, no need for manual rename
but then it's not portable
https://stackoverflow.com/questions/4247068/sed-command-with-i-option-failing-on-mac-but-works-on-linux
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great! Thanks