Google Answers Logo
View Question
 
Q: Escaping Meta Characters in SED ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: Escaping Meta Characters in SED
Category: Computers > Operating Systems
Asked by: zapzapzoo-ga
List Price: $5.00
Posted: 18 Mar 2005 13:53 PST
Expires: 17 Apr 2005 14:53 PDT
Question ID: 496910
How do I use sed to replace " marks in a file with ' ?  I can't seem
to figure out how to escape the ' as a metacharacter.

Request for Question Clarification by maniac-ga on 19 Mar 2005 19:39 PST
Hello Zapzapzoo,

Which shell are you using to run sed with (csh, sh, or something else)?

For the most part, the way to escape a quote character is to use a \
in front of it. It is also greatly complicated by the fact that you
want to use both ' and " in the command line.

What you want sed to receive (after all the processing the shell does)
is something like...
  s/"/'/
[I suggest you fire up ed (not sed), read in a file, and try this to
be sure your sed / ed supports this type of replacement]

If the above is correct, this would require something like
  s/\"/\'/
for the shell. So
  echo s/\"/\'/
should print
  s/"/'/

[alas - I don't have access to a Unix / Linux system to check right
now but this should work. If it does, please post a clarifiation so I
can prepare a more complete answer.]
  --Maniac

Clarification of Question by zapzapzoo-ga on 20 Mar 2005 06:32 PST
Hi.  

I'm using bash for my shell.

While the \ character works great in escaping metacharacters for
everything else, sed and awk don't seem to pay attention to it that
way and consider both to be some sort of string endmark.

For example, let us consider a file that contains the following line:


'singlequotes' "double quotes"


Let's say that I wanted to convert the double quotes to single quotes.
 You would think that

sed 's/\"/\'/g' filename > output

But the shell just gives you a 

>

prompt, expecting more input.

Clarification of Question by zapzapzoo-ga on 20 Mar 2005 06:42 PST
Following your example explicity, I see that they backslash DOES work
if I don't encapsulate my string.  I hadn't considered doing that
because I frequently have spaces in my replacement strings, but
backslashing those works just fine too.

So, while it doesn't resolve all possible questions, it does meet my needs.

I knew the solution would be simple, although it does still seem odd
that it wouldn't work in quotes.

In any case, my needs are solved.  Thanks.

I've not used this [google answers] before.  I assume you have to post
an "answer" in the "answer" box before a "pay responder" button
appears on my side.  You can copy/paste or just answer "foo" and I'll
post it.

Thanks for your time.
Answer  
Subject: Re: Escaping Meta Characters in SED
Answered By: maniac-ga on 21 Mar 2005 19:31 PST
Rated:5 out of 5 stars
 
Hello Zapzapzoo,

I am glad that you found a solution based on the request for question
clarification. Not using quotes but using \ for all special characters
does work. It is also easier to explain than the quoting rules in
bash.

Using your example:
  sed 's/\"/\'/g' filename > output

and reviewing the quoting rules from the bash man page at
  http://www.die.net/doc/linux/man/man1/bash.1.html
it states in part:
  Enclosing characters in single quotes preserves the literal
  value of each character within the quotes. A single quote
  may not occur between single quotes, even when preceded by
  a backslash.
The combination of \' actually terminates the string above. The
remainder of the line is a second string (not terminated).

The interpretation of that command is basically...
  sed is the first (or 0th) parameter
  's/\"\' is the contents of the first quoted string
  /g is between the first and second quoated string
  ' filename > output  (and any more text until another ') is the
second quoted string
The prompt you see (>) is actually bash requesting more input. Enter a
' to that prompt to finish the command to see what I mean (but it
still won't work quite right...)

You could rewrite that command line as:
  sed "s/\"/'/g" filename > output
to get the behavior I believe you expect.

This is because a double quote string preserves the literal value of
all characters except \, $, and `. The backslash has its special
meaning only if followed by ", $, `, \, and newline.

I hope this additional explanation of the quoting rules in bash will
help you in this (and any future) task. Good luck with your work.

  --Maniac
¬
zapzapzoo-ga rated this answer:5 out of 5 stars and gave an additional tip of: $1.00

Comments  
Subject: Re: Escaping Meta Characters in SED
From: phil_mackracken-ga on 28 Mar 2005 11:57 PST
 
Awesome. Thanks.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy