Using sed command replace date variable in unix

I need to use a shell script, using sed command how to replace date variable value in following format.

04/18/2012 11:38:55

Because the sed is treating the '/' as a parameter instead of the value of a variable, and hence there is the message as

sed: command garbled: s/insert/04/18/2012

PLease help me as I am new to this scripting.

Thanks
Suresh

Use another separator, eg:

sed 's!<pattern>!04/18/2012 11:38:55!'

Thanks Franklin for the reply.

Well, the actual command I use in the script is like this..

sed "s/insert/'$STR1'/g" filename

where insert should be replaced with the value of the parameter 'STR1' which is eg. 04/18/2012 11:38:55

I am getting the following error message.

sed: command garbled: s/insert/'04/18/2012 11:38:55'/g

Hence could you please tell me exactly how can I use it in the script. Thanks in advance, Suresh.

i am agree with Franklin52.You can use any character that is not used in the entire expression for any other purpose as a separator instead of slash /.But there is another way here-
shell can be made to ignore special meaning of embedded slashes and treat them as literals by using backslash.So,use \ for each separating slash in date field like dd\/mm\/yyyy.

try like this :wink:

# sed "s#insert#$STR1#g"

the correct format of using the substitution command in sed is:

sed 's/pattern1/pattern2/g' filename

and inside '....' for variable evaluation use "$variable".

I hope the error should not come now.

You cannot substitute variables inside single quotes, must be double quotes.

I meant this:

$$ sed 's/insert/"$STR1"/g' filename

you can use the different delimiters/separator in the almost all of the seds.(all available for GNU sed but forget the special means of thats for shells.)
but we prefer / delimiter for more readable..
but if your string contains same chars with sed's delimeter then we can try change it.
some ones ,:|;#@!%

if u can use "/" then you have to escape the slahs

# sed "s/insert/$(echo ${STR1//\//\\\/})/g" file

and usage single/double quotes may like this in te sed expr.

--> double quotes usefull when expand variable (especially variable contains special meaning variables [white spaces,slashes,the other variables with back slahes..)

sed "s|insert|$STR1|g" file

--> single quotes usefull when our variable does not contain these chars but we have to use double quotes at the front of the STR1 because of escape for these special variables..

sed 's;insert;'"$STR1"';g' file

--> if the STR1="04/18/2012" may we can use only single quotes, because of STR1 is a string without space

# sed 's|insert|'$STR1'|g' file

regards
ygemici

Thanks ygemici(sed_shell@LNU)

The following script worked well for me.

sed "s#insert#insert = $STR1#g" filename

resulting as

insert = 04/18/2012 11:38:55

Thanks
Suresh

---------- Post updated at 07:56 PM ---------- Previous update was at 07:42 PM ----------

I tested sed "s/insert/$(echo ${STR1//\//\\\/})/g" file
as well, working fine to me. Giving the same result as sed "s#insert#$STR1#g" file

Thanks to ygemici & all.
Thanks|Suresh

---------- Post updated at 09:06 PM ---------- Previous update was at 07:56 PM ----------

Hi,
Thanks for your previous posts. I have another requirement with little different output. The sed "s#IncludeDateTime =#IncludeDateTime =$STR1#g" file1.
This is appending the new value to the existing value. what I want is the existing value removed and assign new value. eg. $$IncludeDateTime =04/18/2012 11:38:55 01/01/2011 10:55:37. So what I want from the above command is $$IncludeDateTime =04/18/2012 11:38:55 and should not append the exising value 01/01/2011 10:55:37.

Or exising entire line should be removed and new line to be added with the new results.

Can any one please help in this regard.
Heartful Thanks to all in advance. Thanks | Suresh