Last active
March 1, 2023 22:19
-
-
Save maxpoletaev/4ed25183427a2cd7e57a to your computer and use it in GitHub Desktop.
Cross platform sed
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/bash | |
case "$OSTYPE" in | |
darwin*) PLATFORM="OSX" ;; | |
linux*) PLATFORM="LINUX" ;; | |
bsd*) PLATFORM="BSD" ;; | |
*) PLATFORM="UNKNOWN" ;; | |
esac | |
replace() { | |
if [[ "$PLATFORM" == "OSX" || "$PLATFORM" == "BSD" ]]; then | |
sed -i "" "$1" "$2" | |
elif [ "$PLATFORM" == "LINUX" ]; then | |
sed -i "$1" "$2" | |
fi | |
} | |
replace "s/foo/bar/g" file.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment