Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed: replacement character is a single quote

Status
Not open for further replies.

bi

Technical User
Apr 13, 2001
1,552
US
Can someone tell me why the following line works when it is in a separate sed script, but doesn't when the line is part of a larger script?The script is called with the command:

sed -f sedscr input > output

And this is the line that works as I want it to:

s/ VARCHAR2(3)/,3,' '\)\|\|/

But if I have the following in a larger shell script, the single quotes won't allow the sed command to parse and I get a command garbled error message:

sed -e 's/ VARCHAR2(3)/,3,' '\)\|\|/g' input > output

But, if rather than having the single quotes, I put double quotes, it does work.

sed -e 's/ VARCHAR2(3)/,3," "\)\|\|/g' input > output

I think I tried every combination possible of trying to escape the single quotes, but never could get it to work. Does anyone have any ideas?

Thanks in advance.
 
When you have your sed commands in a file, the shell never sees them, only sed does. When the sed commands are on the command line, the shell will try to parse the line before sed ever sees the commands. The single quote has special meaning to the shell, so it's going to alter the command before it ever gets to sed.

When the shell is trying to parse the line, and it sees a single quote, it leaves everything after that untouched until it sees another single quote. The problem in the line...
[tt]
sed -e 's/ VARCHAR2(3)/,3,' '\)\|\|/g' input > output
[/tt]
...is that you have four single quotes, so that looks like two separate strings to the shell. It's basically breaking your string into two parameters as far as what sed ends up seeing. I'll underline the two strings being passed to sed...
[tt]
sed -e 's/ VARCHAR2(3)/,3,' '\)\|\|/g' input > output
[/tt]
This is incorrect syntax.

Have you tried...
[tt]
sed -e "s/ VARCHAR2(3)/,3,' '\)\|\|/g" input > output
[/tt]
I'm not sure what you're trying to do with the sed replacement, but just keep in mind that the quotes, either single or double, usually need to be matched up.

Hope this helps.


 
Hello Bi,
Sambones gave you some really good info that will probably solve your problem, but didn't give you another good tip that might help. Whenever you think the shell might muck up your code and you really need that special character to go as text, then use a backslash to tell the shell to treat it as text.

Looking at it this way your text would be modified from
sed -e 's/ VARCHAR2(3)/,3,' '\)\|\|/g' input > output
to:
sed -e 's/ VARCHAR2(3)/,3,\' \'\)\|\|/g' input > output

What Sambones also implied and should be pointed out is that double quotes tells the shell to ignore most special characters until the next double quote. Normally, the " is the hammer that tells the shell to butt out. The next problem occurs when you need it to go as text, so the backslash will fix this problem too. So echo "\"" will print a ".

Sambones statement that when using a command file, only sed will see it is SO true, therefore be careful when lifting code from a script to put into a command file and vise-versa. Just remember to take the little devil helper into account and the headaches can be kept to a minimum.
Have Fun!
Charlie Ehler
 
Thanks, Sambones and Charlieehler.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top