Skip to content

Instantly share code, notes, and snippets.

@sibnerian
Created July 18, 2014 16:54
Show Gist options
  • Save sibnerian/ea8b5617bbea9571293d to your computer and use it in GitHub Desktop.
Save sibnerian/ea8b5617bbea9571293d to your computer and use it in GitHub Desktop.
Using sed to replace text in file on Mac OSX
sed_replace() {
# require all 3 parameters
if [ .$1 = . ] || [ .$2 = . ] || [ .$3 = . ]; then
echo "Usage: sed_replace [oldString] [newString] [targetFile]"
return 0
fi
local oldString=$1
local newString=$2
local targetFile=$3
local temp=`mktemp -t sed_replace.XXXXXXXXX`
chmod ug+rw $temp
sed 's/'$oldString'/'$newString'/g' $targetFile > $temp
mv $temp $targetFile
}
@Natim
Copy link

Natim commented Oct 7, 2019

sed -i "${3##*.}" s/$1/$2/g $3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment