sed converting / to \/


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users sed converting / to \/
# 8  
Old 12-14-2006
Quote:
Originally Posted by sakthi.abdullah
Can you please mention those ..
The backquote concept is a bad design starting with mixing it in with the quoting concept. "Quoting" generally means "leave this stuff more or less untouched" until you get to backquotes which mean "run this command". That makes shell programming harder to teach and harder to learn. There are dozens of threads on this site where folks mixed up single quotes and backquotes. But no one has ever mixed up single quotes with $(... ). Running a program is a concept that should stand out as you read a shell script and it is easy to see for programs are being run due to $(...) while backquotes tend to hide program execution.

The shell has to parse its input to figure out what you want. Backquotes are recognized at the wrong time. By mixing this in with quoting, backquotes are recognized as the parameters for a command are being parsed. The $(...) is recognised much earlier. Consider a command like this:
command1 "this and that"
which produces a line of output and I want to embed that output into another string like this:
echo "the result is: $(command "this and that")"
The above syntax is guaranteed to work. But this backquote syntax is actually illegal:
echo "the result is: `command1 "this and that"`"
although modern shells often figure it out. Earlier shells would get mixed up and think the the first and second double quote are supposed to be a quoted string. To bypass that, you are supposed to protect interior double quotes with a backslash like this:
echo "the result is: `command1 \"this and that\"`"
and all shells must recognize this syntax. So when running a command inside backquotes a shell must translate \" to just " before running the command. But what if you actually want \" in your command? Well then you use \\" and now the shell must replace \\ by \ before running the command. This is why you had problems in the thread. Your \\ was being replaced by \ and you must double the number of backslashes to compensate. The fact that modern shells often figure out an expression that was not properly protected with backslashes actually raises the confusion level. The shell seems to be replacing \" with " just for the heck of it. This is why Vino was surprised above. Like many users, Vino has encountered behavior that seems surprising and inexplicable.

OK, right now you're thinking: "Well, what's wrong with few charming little ideosyncracies like these?" But where the backquote concept really gets complex is with nesting. With backquotes the same character serves to both start and end and command while the $(...) syntax has different element on each end. So if you try to imbed a backquoted command inside a backquoted command you must use \` and again the shell deletes the backslash. But while it is parsing the outer command, it also scans though the inner command replacing backslashes there as well. So with each level you must double the number of backslashes. Look at your example above. It took a sequence of 4 backslashes to get it to work. Now imbed that inside another backquoted command and you need 8 backslashes. Imbed that inside still another level and you're up to 16 backslashes. That's as far as I have ever taken it. Code up a few triple nested backquotes expressions and you'll see why I shy away from a quad. But some folks are made of sterner stuff. I've seen a few scripts where someone took it further. I'm not sure how many levels were involved though...I couldn't read the code.
# 9  
Old 12-14-2006
Just an alternative

Code:
var=`echo "/home/abc/dec" | python -c "print raw_input().replace('/','\/')"`
echo $var

output:

Code:
\/home\/abc\/dec

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help in converting

I have Excel file with the below three columns, i need your expertise in converting this to .csv file delimiter "|" Excel - Serial Number Serial Name Serial Brand 111 test sample 123 test2 sample1 134 ... (9 Replies)
Discussion started by: kiran_hp
9 Replies

2. Shell Programming and Scripting

Converting txt file into CSV using awk or sed

Hello folks I have a txt file of information about journal articles from different fields. I need to convert this information into a format that is easier for computers to manipulate for some research that I'm doing on how articles are cited. The file has some header information and then details... (8 Replies)
Discussion started by: ksk
8 Replies

3. Shell Programming and Scripting

Converting a line into a list using awk or sed

Hello, I am trying to convert a line into a list using awk or sed. Line: 345 897 567 098 123 output: 345 897 567 098 123 thanks (7 Replies)
Discussion started by: smarones
7 Replies

4. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

5. UNIX for Advanced & Expert Users

Need help converting ctlU (^U) to a \12...

I have a file that ends with a ctlU: > cat -v test.file blah,blah,blah,GEAEA*1*xx0000111xxx^UIEA*xxx0^U would like to replace the ctlU (^U) with a "\12"...due to printers or something. I believe I might be able to utilize the tr command, if I could only identify the correct... (4 Replies)
Discussion started by: mr_manny
4 Replies

6. SCO

library converting

Hi everybody Is there any sco unix command to convert .so library to .a (under sco unix openserver.5.0.6) tnx (2 Replies)
Discussion started by: javad1_maroofi
2 Replies

7. Shell Programming and Scripting

Converting \0 to a \n

Hi - I have seen some similar posts but I am a bit stumped here below is the first line of a 'od -c filename' command. I want to change the \0 to \n 0000000 l s \0 c d - \0 c d . . \0 l s I have tried a sed construct in a script......... sed... (2 Replies)
Discussion started by: ajcannon
2 Replies

8. SCO

Converting

I use Sco_Sv 3.2v5.0.5 with parellel conection using dump terminals and i want to convert them to desktop pc. Anybody knows what hardware and other thing that would be involved? (3 Replies)
Discussion started by: seeyou
3 Replies

9. Shell Programming and Scripting

Converting to Uppercase

I want to convert string into uppercase string. How can i do that ? Ex: Enter the user name: read name show=upper(name) echo $show --- This output should be the uppercase output. Thanks (3 Replies)
Discussion started by: dreams5617
3 Replies

10. UNIX for Dummies Questions & Answers

converting kb to mb

When I create filesystems in AIX i often get confused(using smit) When you specify size in aix, it is asked like this SIZE of file system (in 512-byte blocks) I never seem to grasp this, what is the equation to get say 500mb? Or is there a program anyone knows of that does this, like a... (1 Reply)
Discussion started by: csaunders
1 Replies
Login or Register to Ask a Question