Contents
When to use sed substitution for a variable?
Variable substitution should be outside sed expression and ‘$’ should be escaped; in your case something like this: Anyway in your place I would avoid using $ to match placeholders in a template file, because it’s causing confusion with BASH variables, use a different pattern instead (for instance @URL@).
What is the fallback character for sed substitution?
Note that I initially used / as the separator in the substitute operations; however, as DarkDust rightly points out, that won’t work since there are slashes in the URLs. My normal fallback character is % – as now shown – but that can appear in some URLs and might not be appropriate.
How to substitute / opt / Omni / lbin in SED?
Example 1 – sed @ delimiter: Substitute /opt/omni/lbin to /opt/tools/bin When you substitute a path name which has ‘/’, you can use @ as a delimiter instead of ‘/’. In the sed example below, in the last line of the input file, /opt/omni/lbin was changed to /opt/tools/bin.
How does SED get matched string in replacement?
Sed ‘&’ Get Matched String The precise part of an input line on which the Regular Expression matches is represented by &, which can then be used in the replacement part. In the above example ‘&’ in the replacement part will replace with /usr/bin which is matched pattern and add it with /local.
When to use pipe instead of pipe in SED?
That is, the first $ needs to be escaped because you want it literally. Then, instead of / I suggest you use | (pipe) as delimiter since it’s not used in your strings. Finally, use ” to ensure the content is interpreted as string by the shell.
Why does SED see something other than what you intended?
You also need to learn how to do multiple operations in a single pass, and/or how to use pipes. Your problem is that the shell expanded the ‘$URL’ in your command line (probably to nothing), meaning that sed got to see something other than what you intended. By escaping the $ with the \\, sed gets to see what you intended.
Can a variable be expanded within single quote in SED?
Shell variables will not get expanded within single quotes. Therefore, the quick fix would be using double quotes in the sed command to allow shell variable expansion: Now, let’s test the solution.sh script once again: Good! We’ve filled the date and time in the right place. Next, let’s fill the JAVA_HOME path in the file.
How to replace a string with quote in SED?
When you require the quote character in the replacement string, you have to precede it with a backslash which will be interpreted by the shell. In the following example, the string quote me will be replaced by “quote me” (the character & is interpreted by sed): sed -i “s/quote me/\\”&\\”/” “$file”.
Is it possible to substitute a variable in Bash?
Variables inside ‘ don’t get substituted in Bash. To get string substitution (or interpolation, if you’re familiar with Perl) you would need to change it to use double quotes ” instead of the single quotes: