Shell variables in sed script
Use double quotes for the sed expression. new_db_name=$(echo “$new_db_name” | sed “s/$replace_string/$replace_with/”)
Use double quotes for the sed expression. new_db_name=$(echo “$new_db_name” | sed “s/$replace_string/$replace_with/”)
Quote sed codes with double quotes: $ sed “s/ones/one’s/”<<<“ones thing” one’s thing I don’t like escaping codes with hundreds of backslashes – hurts my eyes. Usually I do in this way: $ sed ‘s/ones/one\x27s/'<<<“ones thing” one’s thing
sed is a scripting language. You separate commands with semicolon or newline. Many sed dialects also allow you to pass each command as a separate -e option argument. sed -i ‘s/File//g;s/MINvac\.pdb//g’ /home/kanika/standard_minimizer_prosee/r I also added a backslash to properly quote the literal dot before pdb, but in this limited context that is probably unimportant. For … Read more
The problem is with slashes: your variable contains them and the final command will be something like sed “s/string/path/to/something/g”, containing way too many slashes. Since sed can take any char as delimiter (without having to declare the new delimiter), you can try using another one that doesn’t appear in your replacement string: replacement=”/my/path” sed –expression … Read more
The easiest way would be to use a different delimiter in your search/replace lines, e.g.: s:?page=one&:pageone:g You can use any character as a delimiter that’s not part of either string. Or, you could escape it with a backslash: s/\//foo/ Which would replace / with foo. You’d want to use the escaped backslash in cases where … Read more
You can use an alternative regex delimiter as a search pattern by backslashing it: sed ‘\,some/path,d’ And just use it as is for the s command: sed ‘s,some/path,other/path,’ You probably want to protect other metacharacters, though; this is a good place to use Perl and quotemeta, or equivalents in other scripting languages. From man sed: … Read more
How can I replace each newline (\n) with a space using sed?
Unfortunately the virtual machine that is sed works a certain way, and the way that it works doesn’t make it easy to: replace newlines. See How can I replace a newline (\n) using sed? for why that is. replace anything with a tab, See Replacing / with TAB using sed for more about that. Still, … Read more