Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match a single quote in sed

How to match a single quote in sed if the expression is enclosed in single quotes:

sed -e '...' 

For example need to match this text:

'foo' 
like image 389
grigy Avatar asked Sep 18 '08 09:09

grigy


People also ask

How do you use single quotes in sed?

Just escape the double quotes.

How do you escape a single quote in sed?

You don't need to escape in sed, where ' has no special significance. You need to escape it in bash. You can see here that the double quotes protect the single quote from bash, and sed does fine with it.

How do you match a character in sed?

Matches any single character in list : for example, [aeiou] matches all vowels. A list may include sequences like char1 - char2 , which matches any character between (inclusive) char1 and char2 . A leading ^ reverses the meaning of list , so that it matches any single character not in list .

Can sed use double quotes?

@DummyHead Here's another approach: if you use single quotes, the sed command is exactly as you type it. If you use double quotes, you must take into account all shell substitutions and elaborations to "visualize" what command is eventually passed on to sed.


2 Answers

You can either use:

"texta'textb" (APOSTROPHE inside QUOTATION MARKs) 

or

'texta'\''textb' (APOSTROPHE text APOSTROPHE, then REVERSE SOLIDUS, APOSTROPHE, then APOSTROPHE more text APOSTROPHE) 

I used unicode character names. REVERSE SOLIDUS is more commonly known as backslash.

In the latter case, you close your apostrophe, then shell-quote your apostrophe with a backslash, then open another apostrophe for the rest of the text.

like image 135
tzot Avatar answered Sep 30 '22 14:09

tzot


As noted in the comments to the question, it's not really about sed, but how to include a quote in a quoted string in a shell (e.g. bash).

To clarify a previous answer, you need to escape the quote with a backslash, but you can't do that within a single-quoted expression. From the bash man page:

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.

Therefore, you need to terminate the quoted expression, insert the escaped quote, and start a new quoted expression. The shell's quote removal does not add any extra spaces, so in effect you get string concatenation.

So, to answer the original question of how to single quote the expression 'foo', you would do something like this:

sed -e '...'\''foo'\''...' 

(where '...' is the rest of the sed expression).

Overall, for the sake of readability, you'd be much better off changing the surrounding quotes to double quotes if at all possible:

sed -e "...'foo'..." 

[As an example of the potential maintenance nightmare of the first (single quote) approach, note how StackOverflow's syntax highlighting colours the quotes, backslashes and other text -- it's definitely not correct.]

like image 44
TimB Avatar answered Sep 30 '22 14:09

TimB