Not entirely sure what you are trying to do with sed
there. Is this what you are looking for?
printf "\x27 \x60\n"
# prints
# ' `
printf "%x %x\n" "''" "'\`"
# prints
# 27 60
Taken from BashFAQ 071
Edit
sed
is perfectly fine with either `
or '
in its regex. Where you are running into problems is getting those characters through the shell interpreter since both single quote and backtick have special meaning to the shell, and must be quoted if they are to make it to sed without bash throwing a syntax error or mangling the input.
There are three ways to quote special characters: single quote '
, double quote "
, and backslash \
. They all behave a bit differently. '
quotes everything except itself, so you can use this to quote `
but not '
. "
quotes everything except \
, `
, and $
(irrelevant here), so you can use "
to quote '
but not `
. \
quotes everything so you could use that for both. It is possible to nest these methods as long as you keep the quoting rules straight (they are interpreted from left to right).
To make things more concrete, suppose you wanted s/`'/replacement/
in your sed
regex. There are many ways to do this. Two different examples are given below.
# concatenation of single-quoted s/`, backslash-quoted ', and
# single-quoted remainder of the command
sed -e 's/`'\''/replacement/'
# one double-quoted string quotes the single-quote, but an
# additional backslash is needed to quote the backtick
sed -e "s/\`'/replacement/"
Just keep the quoting rules in mind and use the most readable version that gets the job done. See the QUOTING
section in the bash manual for a complete explanation and reference.
As a final note, I'd avoid using the special \d
, \x
, \o
etc. escapes because
- They are GNU extensions to
sed
which may be less portable than properly quoted patterns.
- It's a bit harder to read. How many people know the ASCII values for
'
and `
off the top of their heads?
1FYI ` is a "backtick", not tilde.
~
is a tilde. – jw013 – 2011-08-04T06:38:34.177@jw013 you can edit the post buy clicking the edit link. Plus you score +2 rep for every edit you make that gets accepted. Thanks for all your help with the two problems I had. Thank you. – nelaaro – 2011-08-04T08:03:03.947
1
For future reference when converting characters... http://www.asciitable.com
– Joe Internet – 2011-08-04T08:03:54.120