sed command to replace string that contain blackslash,double quotes

Hi All,

I have been trying to replace a string using the sed command

string value contain blackslash and double quotes. I am not a expert writer of unix script but do try not to ask question. I have almost given up. Hope you all can give me some suggestion

I want to replace a place string in a file that contains "TEMP"\1\"/work/tmp" (values are with double quotes) with another value "TEMP"\1\"/work/${projectname}/tmp" (projectname is passed through a list from a file)

example this sting "TEMP"\1\"/work/tmp" needs to be replace like this "TEMP"\1\"/work/AST/tmp" (where is AST is the project name)

command used:-

projectname="AST"
search_string="\"TEMP\""\\1\\"\"/work/tmp\""
replace_string="\"TEMP\""\\1\\"\"/work/${projectname}/tmp\""

sed -e "s|${search_string}|${replace_string}|g" ${ARCH_DIR}/${PROJECT}.DSParams > ${PROJECT_DIR}/${PROJECT}/DSParams

I have tried with different options like using single quotes, parsing but could not find a solution

however the same command is working fine for below

search_string1="/apps/ETL/IS/8.5/Server/PXEngine/grid_enabled.4.3.8/grid_config.apt"
replace_string1=""

Please use code tags as required by forum rules!

Please post the relevant part of the input file. Why wouldn't

search_string="/work/tmp"
replace_string="/work/${projectname}/tmp"

suffice?

The actual value of search string is with double quotes

 "TEMP"\1\"/work/tmp"

and needs to be replaced with

 "TEMP"\1\"/work/${projectname}/tmp"

My code

					
search_string='"TEMP"\\1\\"\/work\/tmp"'
replace_string='"TEMP"\\1\\"\/work\/${PROJECT}\/tmp"'
sed  s#"${search_string}"#${replace_string}#g ${ARCH_DIR}/${PROJECT}.DSParams > ${PROJECT_DIR}/${PROJECT}/DSParams

---------- Post updated at 11:19 PM ---------- Previous update was at 11:18 PM ----------

Input file content

"APM_MFTDATAOUT_DIR"\1\"/work/TET/data/out/mft"
"APM_PROP_DIR"\1\"/projects/ETL/TET/utils/prop"
"APM_SCRIPTS_DIR"\1\"/projects/ETL/TET/utils/scr"
"APM_SRC_ARRAY_SIZE"\1\"2000"
"APM_TGT_ARRAY_SIZE"\1\"2000"
"TEMP"\1\"/work/tmp"
"TET_DB2_PWD"\1\"L<I@1;VH=9K=4O4M4IN<0"
"TET_DB2_SERVER"\1\"DB2T"
"TET_DB2_USER"\1\"SVTET02"
"TET_INSTNCE_STRNG"\1\"CAN,ECOM,DOM,NBD"
"TET_ODS_DSN"\1\"TET_ODS_DEV"
"TET_ODS_PWD"\1\"LEH@IHVHO93?0N6M5:N<1K0FB5=ME0Q5@UKOkFK`O2U72"
"TET_ODS_USER"\1\"svdbetltetodsdev"
"TMP"\1\"/work/tmp"
"APT_GRID_COMPUTENODES"\1\"2"

If the output you're trying to get is:

"TEMP"\1\"/work/AST/tmp"

you're very close; your only problem is that variable expansions do not occur inside single quotes. Try:

PROJECT="AST"
search_string='"TEMP"\\1\\"\/work\/tmp"'
replace_string='"TEMP"\\1\\"\/work\/'${PROJECT}'\/tmp"'    # Note the added pair of single quotes
sed  s#"${search_string}"#${replace_string}#g ${ARCH_DIR}/${PROJECT}.DSParams > ${PROJECT_DIR}/${PROJECT}/DSParams
1 Like

Did try that as well, however did not replace the string. I tried running the scrip with sh -x to get something out of it. Seem everything fine

+ search_string="TEMP"\\1\\"\/work\/tmp"
+ echo  A: Search string: "TEMP"\\1\\"\/work\/tmp"
+ 1>> /home_dir/a542885/log/Grid_DSParams_20141111101524.log
+ replace_string="TEMP"\\1\\"\/work\/A\/tmp"
+ echo  A: Replace string: "TEMP"\\1\\"\/work\/A\/tmp"
+ 1>> /home_dir/a542885/log/Grid_DSParams_20141111101524.log
+ echo "TEMP"\\1\\"\/work\/tmp"
"TEMP"\1\"\/work\/tmp"
+ echo "TEMP"\\1\\"\/work\/A\/tmp"
"TEMP"\1\"\/work\/A\/tmp"
+ sed s#"TEMP"\\1\\"\/work\/tmp"#"TEMP"\\1\\"\/work\/A\/tmp"#g /home_dir/a542885/PRJ_PARAM/A.DSParams
+ 1> /home_dir/a542885/A/DSParams

Hi.

My approach to sed situations like this is that if there is even a hint of interaction with the shell: quotes: single, double, backtick, escape: backslash, or any of the meta-symbols: dollar sign, question mark, asterisk, parens, braces, brackets, etc., then I place the commands in a file and let sed read that file thus avoiding the shell complications, i.e.

sed -f script-file ...   -or-
--file=script-file ...

I t may be the case that a preliminary pass over those commands is necessary -- it looked like you wanted a variable or two evaluated, which, in my opinion would still be easier than trying to get all the escapes, etc, in the correct number and order for the shell.

Best wishes ... cheers, drl

Apologies ...

The above suggestion given by Don Cragun is working perfectly

Thank you very much