Query on using command "sed" for replacing text in bash shell

Re: Query on using command "SED" for replacing text in bash shell

While using the command "sed" for find and replace, I wanted to know how one could find a constant and replace it with a variable inside the quotation syntax of sed?

I wanted to replace constant 3 with variable name "hourid"
Here is what I tried.

sed -i 's@3@hourid$@g file_name.txt

I want to know how to provide a right syntax for replacing variable "hourid"

Your suggestions are greatly appreciated

Thanks very much!

You are using @ as a regex delimiter in sed ?

Here is a quick example of how sed is used, in practice:

sed 's/regexp/replacement/g' inputFileName > outputFileName

Your request is not too clear. Do you want to replace a constant string ("3") in your text with the expanded value of a shell variable ( $hourid )? Then, try double quotes, as single quote prevent expansion:

sed "s@2@${hourid}@g" file

Be careful not to trigger on false positives (e.g. 13, 3567, etc.)

1 Like

@Neo, I am using "@" as a delimiter since the text contains "/"
I assume one can use several delimiters such as @, # !

1 Like