Sed - How to escape variable number of "/" (slash) ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed - How to escape variable number of "/" (slash) ?
# 1  
Old 07-03-2008
Sed - How to escape variable number of "/" (slash) ?

Hi,

Can you tell me how to escape a variable number of slash characters in sed "/" ?

In the script the code looks like this:

cat $file_to_update | sed s/^$param/$param=$tab2*\#\*/1

And the $tab2 value is a path so it will have a number of "/" charracters.

Code:
# cat db.cfg | sed "s/^OutputDirectory/OutputDirectory=/var/opt/*#*/1"
sed: command garbled: s/^OutputDirectory/OutputDirectory=/var/opt*#*/1
 And also "\" does not seem to work:
# cat db.cfg | sed s/^OutputDirectory/OutputDirectory=\/var\/opt*#*/1
sed: command garbled: s/^OutputDirectory/OutputDirectory=/var/opt*#*/1

# 2  
Old 07-03-2008
The best way to deal with paths in sed is to use a character other than / as the regexp delimeter --any will do, i.e.
Code:
sed s,a,b,

is just the same as
Code:
sed s/a/b/

Then you don't have to escape the slashes in the search/replace patterns.
# 3  
Old 07-03-2008
Either you do like spirtle said or you use a separate sed script to change every "/" in the $tab2 variable to a "\/" prior to using the variables content:

Code:
tab2="(print - "$tab2" | sed 's/\//\\\//g')"

I hope this helps.

bakunin
# 4  
Old 07-03-2008
Another problem

Thank you spirtle it works with the comma.

Do you now how to properly escape backslash too?

I need to replace every "/" from a variable with "\\/" but this does not seem to work if I put it in a variable. Here is an example:

Code:
[root@europa:/]# echo $escape_slash
/fileserver/sn-cvsroot/batchpltf/
[root:/]# echo $escape_slash | sed s,/,\\\\\\\\\\/,g
\\/fileserver\\/sn-cvsroot\\/batchpltf\\/

As you can see instead of "\\" I always get a single "\" in the variable no matter how many "\" characters I place in there:

[root:/]# escape_slash_b=`echo $escape_slash | sed s,/,\\\\\\\\\\/,g`
[root@europa:/]#  echo $escape_slash_b
\/fileserver\/sn-cvsroot\/batchpltf\/

If I replace "/" with only "\/" sed gives an error on my system so I noticed that "\\/" works(but not from a variable tho, which is what I need).
# 5  
Old 07-05-2008
majormark, you missed the point of spirtle's post. If you use:
sed "s/old/new/"
then slash becomes a special character and you must escape any slashes that appear in either the old or new strings. But switch to:
sed "s=old=new="
and now slash is just another character that need not be escaped. so change that delimiter character to something that is not used in either the old or new strings. Then forget about the backslashes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capture the last record number using "awk" NR variable

Hi Team. I am trying to capture the last record number from a file using the below command ( assuming abc.txt has 21 records and I want 21 as output ) awk'{c=NR;print c}'abc.txt But it is printing all the record number. Can someone please help modify the above command? (8 Replies)
Discussion started by: chatwithsaurav
8 Replies

2. Shell Programming and Scripting

awk sub function "quote or slash"

I struggle to understand why its used slash / instead of doublequotes " in sub function of awk. This is taken from the manual of awksub(/USA/, "United States", "the USA and Canada") But this work alsosub("USA", "United States", "the USA and Canada") Is there any reason of why to select one... (2 Replies)
Discussion started by: Jotne
2 Replies

3. Shell Programming and Scripting

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line... (4 Replies)
Discussion started by: sudeep.id
4 Replies

4. UNIX for Dummies Questions & Answers

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line to... (1 Reply)
Discussion started by: sudeep.id
1 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

sed escape character for comment string "/*"

Good afternoon all, I'm hoping my newbie question can help bolster someone's street_cred.sh today. I'm trying to "fingerprint" SQL on its way into the rdbms for a benchmarking process (so I can tie the resource allocation back to the process more precisely). To do this, I'm essentially... (4 Replies)
Discussion started by: toeharp
4 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. Solaris

Solaris escape my script from "-" to "/226"

Hello everyone. I beg your guys pardon please. I try to ls -al in many path/directories. So, I put the code in text file which look like below; ls -al / ls -al /etc ls -al /etc/default ... however, when I paste it to Solaris over SecureCRT, it seems the code was escaped from "-" to... (0 Replies)
Discussion started by: Smith
0 Replies

9. Shell Programming and Scripting

Sed , Replace a "variable text" inside of a statement

Please Help... I am trying to manipulte the following line Before : <user:Account_Password>002786</user:Account_Password> the password is the "variable", i need to delete / omit the password in the file, (it occurs several thousand times) so the tag line looks like After:... (4 Replies)
Discussion started by: jackn7
4 Replies

10. UNIX for Dummies Questions & Answers

How to count number of occurrences of a "|" from a variable?

I have a variable, var="some1|some2|some3" I want to know how many "|" are in $var. When I say echo $var | grep -c '|' I am getting only 1 :confused: :confused: :confused: ? (4 Replies)
Discussion started by: jingi1234
4 Replies
Login or Register to Ask a Question