Escape single quote - sed

The following is working as expected. I want to replace the word "me" with \'
echo "test ' this" | sed 's/'"'"'/me/g'
test me this
Expected result:
test \' this
Is it possible to escape double quotes as well in the same command?

Your question is a little confusing since there's no me in the original string to replace. However, I think I have it. Let me paraphrase:
I have a sed command which can successfully replace a single quote ' with the word me. I want a similar one which can replace it with the character sequence \'.
If that's the case (you just want to escape single quotes), you can use:
pax$ echo "test ' this" | sed "s/'/\\\'/g"
test \' this
By using double quotes around the sed command, you remove the need to worry about embedded single quotes. You do have to then worry about escaping since the shell will absorb one level of escapes so that sed will see:
s/'/\'/g
which will convert ' into \' as desired.
If you want a way to escape both single and double quotes with a single sed, it's probably easiest to provide multiple commands to sed so you can use the alternate quotes:
pax$ echo "Single '" 'and double "'
Single ' and double "
pax$ echo "Single '" 'and double "' | sed -e "s/'/\\\'/g" -e 's/"/\\"/g'
Single \' and double \"
If I've misunderstood your requirements, a few examples might help.

I have some doubts about your question, but anyway maybe my solution is useful for anyone.
Let's me say before all :
\\ that's mean one \ because when you put two together you are cancelling its meaning as metacharacter. And \x27 as single quote, at least for me is easier than working with single a double ...
Finally I put
echo "test me this" | sed 's/me/\\\x27/g'
so here you have test me ==> test \' this
echo "test \' this" | sed 's/\\\x27/me/g'
and here is the opposite.

Is this what you mean?:
# cat <<! | sed 's/["'"'"']/\\&/g'
> quote '
> double quotes "
> quote ' double quotes "
> !
quote \'
double quotes \"
quote \' double quotes \"
Or this:
# echo "test me test"|sed 's/me/\\'"'"'/g'
test \' test
# echo "test me test"|sed 's/me/\\"/g'
test \" test
I find it best to always single quote sed/awk/perl... commands on the command line and make "holes" for double quote parameters used within them.

I needed to use ' in a script to create a dnsmasq config file, this is the technique I used:
echo -e "\toption name *$name*"|sed s/*/\'/g
example output
option name 'GarageDoor'

Related

How to escape characters in sed

I'm trying to remove some text from multiple files using sed. This is the text I'm trying to delete:
\once override TupletBracket #'stencil = ##f
I've tried this line in sed but I can't get it to work:
sed -i '' -e 's/\\once \\override TupletBracket #'stencil = ##f//g' *ily
I've tried escaping the # symbols, the ' and the = but still no joy. Could anyone please point me in the right direction?
I think it's better to use single quotes here rather than double quotes to avoid the extra \s and other possible expansions (e.g. variables). Where you want a literal single quote, you close the quotation, add \', and then start a new quotation for the remainder.
$ cat in
before \once override TupletBracket #'stencil = ##f after
$ sed 's/\\once override TupletBracket #'\''stencil = ##f//g' in
before after
you can't use ' directly inside sed command that is quoted using '. Use a double quotes instead and to match \ you'll need to use \\\ to have \\ i.e \.
$ sed "s/\\\once override TupletBracket #'stencil = ##f//g"
\once override TupletBracket #'stencil = ##f
hello \once override TupletBracket #'stencil = ##f xyz
hello xyz
$
# and = are not RE metacharacters nor do they have any other special meaning to sed within a regexp (= does outside of a regexp) unless the regexp is delimited with one of them so there's no reason to escape them in your script. ' only has significance if the whole script is delimited with 's since in shell no script that's delimited by a given character can include that character. So here's your choices:
$ echo "seab'cd" | sed 's/b'\''c/foo/'
seafood
$ echo "seab'cd" | sed "s/b'c/foo/"
seafood
Note that if you use the second (double quotes) version then you're allowing shell variables to expand inside the script and would require double-backslashes to escape chars.
I expected using the octal representation of a ' (i.e. \047) would work too like it does in awk:
$ echo "seab'cd" | awk '{sub(/b\047c/,"foo")}1'
seafood
but it didn't:
$ echo "seab'cd" | sed 's/b\047c/foo/'
seab'cd
and I suspect that's because sed is treating \0 as a backreference. It does work with the hex representation:
$ echo "seab'cd" | sed 's/b\x27c/foo/'
seafood
but that's dangerous and should be avoided (see http://awk.freeshell.org/PrintASingleQuote).

how to replace a \' with '' using sed

I have a file like this test.sql:
'here is' and \' other for 'you'
and would like to replace the \' (escaped single quote) with '' (2 singles quotes) for postgres and leave other single quotes alone. How would I do this. I have tried:
Mon Mar 16$ sed -i.bak s/\'/''/g test.sql
but this takes out all the single quotes.
Your enemy in this is shell quoting. The string
s/\'/''/g
is mangled by the shell before it is given to sed. For the shell, '' is an empty string, and \' suppresses this special meaning of single quotes (so that the quote is an actual single quote character). What sed sees after processing is
s/'//g
...wich just removes all single quotes.
There are several ways to work around the problem; one of them is
sed -i.bak "s/\\\\'/''/g" test.sql
Inside the doubly-quoted shell string, backslashes need to be escaped (exceptions exist). This means that "s/\\\\'/''/g" in the shell command translates to s/\\'/''/g as argument to sed. In sed regexes, backslashes also need escaping, so this is, in fact, what we wanted to happen: All instances of \' will be replaced with ''.
sed "s/[\\]'/''/g" test.sql
# also work but may depend on shell
sed "s/[\]'/''/g" test.sql
same idea as Wintermute but using class to avoig multi escaping for shell than sed in double quote

how do I escape ' in sed?

I do not understand what I am doing wrong here:
$ cat fixnames.sh
#!/bin/sh
for i in *mp3
do
j=`echo $i | sed -e's/ /_/g'`
j=`echo $j | sed -e's/_(...)_/_/g'`
j=`echo $j | sed -e's/\'//g'`
echo $j
done
$ ./fixnames.sh
./fixnames.sh: 1: Syntax error: Unterminated quoted string
I guess the line /bin/sh does not like is ...
j=`echo $j | sed -e's/\'//g'`
... so how am I suppose to remove ' ?
It's shell not sed giving trouble. You can't escape single quotes in a single-quoted string.
for i in *mp3
do
j=$(echo $i | sed -e 's/ /_/g' -e 's/_(...)_/_/g' -e "s/'//g")
echo $j
done
In this context, it is sufficient to use double quotes around the expression. Other times, you need to be more careful with the double quotes (stray $ need escaping, etc), or you use the canonical sequence '\'' to embed a single quote in a single quoted string:
-e 's/'\''//g'
The '\'' sequence stops the current single quoted string, inserts an escaped single quote (effectively just a single quote), and resumes the single quoted string.
Note that I combined the 3 invocations of sed into one; I like the -e option but many people would use semicolons to separate the three commands. Also note the use of $(...) in preference to back-quotes.

Escaping single quotes

I want to replace the double quotes in the sed command in the following example with single quotes.
set new_string to do shell script "echo " & quoted form of list_string & " | sed -e 's/$/\"/' -e 's/^/\"/' -e 's/^/+/'"
However if I replace the double quotes with single quotes I get an error, is there a way to escape single quotes?
I'm no sed ninja, so any hints on how to go about this is highly appreciated.
if you want to replace " with ' using sed:
sed 's/"/\x27/g' yourFile
\x27 - single
\x22 - double
it could make code looks cleaner, and with less escape.
see the test:
kent$ cat quote.tmp
""""""
kent$ sed 's/"/\x27/g' quote.tmp
''''''
fYou had a quotation fault. Just to replace double quotes for single quotes, this is enough
set list_string to "This program said: \"Hello World!\""
set new_string to do shell script "/bin/echo -n " & quoted form of list_string & " | sed -e 's/\"/'\\''/g'"
Explaining 's/\"/'\''/g'
The \\ and \" is needed in the applescript environment and will be in the shell just \ and ". So what's entering the shell is 's/"/'\''/g'. Then what's with all the quotes? A very common mistake is thinking that quotations on the command line works the same as in programming. A single quote turns substitution on or off. So the first single quote turns substitution off which mean the next characters will be interpreted as text and has no special meanings (including the escape character). So to escape a single quote we'll need to turn the substitution on, then we can escape a single quote and turn the substitution off again.
You need to be careful about which quotes are being parsed by sed and which are being parsed by the environment invoking sed. Normal invocations of sed come from shell scripts, but (based on your tag) it appears that you're calling it from an AppleScript.
From a shell script you would say
| sed -e 's/$/'\''/' -e 's/^/'\''/' -e 's/^/+/'
But I don't know if sh-style escaping rules are in effect for you or whether you need to additionally escape the \

sed and Applescript shell script - strip character string

I'm trying to use sed in a shell script in Applescript to strip this string - ?print=1 - from this html link in the variable the_html, which is my link
but this throws an error:
set new_html to do shell script "echo " & quoted form of the_html & " | sed s=?print=1= =g'"
Do I need to escape the "="?
Edit:
Works now. Applescript didn't like an = escaped with a \, but escaping the whole string works:
sed 's/?print=1//g'
Try this:
echo 'my link' | sed 's/?print=1/ /g'
Comments:
Put the HTML in quotes or escape it property
When using sed, you generally use slashes: 's/a/b/'
This works on my Mac:
echo 'my link' | sed 's=?print\=1= =g'
So the answer is, yes, you do need to escape the = since it is used an expression delimiter.
Yes. You need to escape the characters that have a special meaning. Now there are the standard regular expression special characters, and the character that you are using as a delimiter. So if you use = as the delimiter, you'll need to escape it with \.
Usually / is used as the delimiter. The exception is when you might be searching for /, which yields some pretty crazy and hard to read expressions with all the escaping. So if you're searching for /s, I'd suggest using a different character, but otherwise, stick to /.
To answer the immediate question, you might escape the =:
sed '=print\=1= =g'
or use the standard slash, without escaping the =:
sed '/print=1/ /g'
You don't need to escape '=' if using another sed separator like '/' or ','. But if you wish to get URL parameters:
With '?' prefix:
echo 'my link' \
| sed -e 's,.*\(?.*\)\".*,\1,'
Without the '?' :
echo 'my link' \
| sed -e 's,.*?\(.*\)\".*,\1,'
The best is also to split parameters:
$ echo 'my link' \
| sed -e 's,.*?\(.*\)\".*,\1,' -e 's,&,\n,g'
print=1
convert=4
Have fun ! :)