sed command to replace string that contain blackslash,double quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to replace string that contain blackslash,double quotes
# 1  
Old 11-10-2014
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:-
Code:
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

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


Last edited by Don Cragun; 11-10-2014 at 02:05 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 11-10-2014
Please use code tags as required by forum rules!

Please post the relevant part of the input file. Why wouldn't
Code:
search_string="/work/tmp"
replace_string="/work/${projectname}/tmp"

suffice?
# 3  
Old 11-10-2014
The actual value of search string is with double quotes

Code:
 "TEMP"\1\"/work/tmp"

and needs to be replaced with

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

My code

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

Code:
"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"

# 4  
Old 11-10-2014
If the output you're trying to get is:
Code:
"TEMP"\1\"/work/AST/tmp"

you're very close; your only problem is that variable expansions do not occur inside single quotes. Try:
Code:
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

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-11-2014
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

Code:
+ 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

# 6  
Old 11-11-2014
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.
Code:
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
# 7  
Old 11-11-2014
Apologies ...

The above suggestion given by Don Cragun is working perfectly

Thank you very much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace double quotes inside the string data for all the columns

Please use code tags Hi, I have input data is below format and n of column in the multiple flat files. the string data has any double quotes(") values replaced to double double quotes for all the columns{""). Also, my input flat file each column string data has carriage of new line too.... (14 Replies)
Discussion started by: SSrini
14 Replies

2. UNIX for Beginners Questions & Answers

sed command to replace consecutive double quotes

I need to replace consecutive double quotes in a csv file, the data in the file is enclosed in double quotes but there are some places where the quotes are repeating Example is below Incoming data is : "Pacific Region"|"PNG"|"Jimmy""|""| Need output as: "Pacific... (10 Replies)
Discussion started by: abhilashnair
10 Replies

3. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

4. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

5. Shell Programming and Scripting

Replace newline character between a double quotes to a space

Hi Guys, I have a file with content as below aj.txt "Iam allfine" abcdef abcd "all is not well" What I'm trying to say is my data has some new line characters in between quoted text. I must get ride of the newline character that comes in between the quoted text. output must be:... (8 Replies)
Discussion started by: ajahuja
8 Replies

6. Shell Programming and Scripting

HELP with AWK or SED. Need to replace the commas between double quotes in CSV file

Hello experts, I need to validate a csv file which contains data like this: Sample.csv "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 I just need to check if all the records contain exactly the number of... (5 Replies)
Discussion started by: shell_boy23
5 Replies

7. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

8. Shell Programming and Scripting

Replace double double quotes using AWK/SED

Hi, I have data as "01/22/97-"aaaaaaaaaaaaaaaaa""aaa""aabbbbbbbbcccccc""zbcd""dddddddddeeeeeeeeefffffff" I want to remove only the Consequitive double quotes and not the one which occurs single. My O/P must be ... (2 Replies)
Discussion started by: Bhuvaneswari
2 Replies

9. UNIX for Dummies Questions & Answers

How to replace double quotes in to ascii value?

Hi, I am getting the data from text file and it contains comma and double quootes. Eg: text file like this: Travelling up Cattai Ridge Rd, going through the "S" bends at the top. Felt a "pull" and pain in groin area, on right side after lifting at work. Repeat "twisting" while working manual... (4 Replies)
Discussion started by: rajesh4851
4 Replies

10. Shell Programming and Scripting

Replace multiple blanks within double quotes

I have various column names within double quotes, separated by commas. Example: "column one", "column number two", "this is column number three", anothercolumn, yetanothercolumn I need to eliminate the double quotes and replace the blanks within the double quotes by underscores, giving: ... (5 Replies)
Discussion started by: jgrogan
5 Replies
Login or Register to Ask a Question