Sedify It
Something I found on the internet which became quite useful. Unfortunately I can’t remember from where I got it, so if you recognize it - happy to give the creds.
Anyway, the problem I was trying to solve is to come up with a more or less generic enough function in bash which would allow me to pass a string with sort of any character without having to worry about escaping it.
This is what worked for me:
sedify() {
# start with the original pattern
escaped="$1"
# escape all backslashes first
escaped="${escaped//\\/\\\\}"
# escape slashes
escaped="${escaped//\//\\/}"
# escape asterisks
escaped="${escaped//\*/\\*}"
# escape full stops
escaped="${escaped//./\\.}"
# escape [ and ]
escaped="${escaped//\[/\\[}"
escaped="${escaped//\[/\\]}"
# escape ^ and $
escaped="${escaped//^/\\^}"
escaped="${escaped//\$/\\\$}"
# remove newlines
escaped="${escaped//[$'\n']/}"
#sed should be fine now. probably not for -r though (extended regex)
}
Read other posts