how to replace a \' with '' using sed - 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

Related

sed replace with backslash, double quote, single quote

Can you give the sed command that will find \" and replace with \\' in a file.
For example line:
LOG_FN=\"file_name\"
will become
LOG_FN=\\'file_name\\'
By using this template:
sed -i 's/old-text/new-text/g' input.txt
I tried following sed commands:
sed -i 's/\\\"/\\\\\'/g' input.txt
sed -i "s/\\\"/\\\\'/g" input.txt
Unfortunately they fail because what I am looking for is a string substitution for \" while commands I tried change individual " characters.
You can't escape a single quote inside single quotes. Your second attempt needs more backslashes: Remember, inside double quotes, the shell processes one layer of backslashes, so you have to double each backslash which should make it through to sed.
sed "s/\\\\\"/\\\\\\\\'/g" input.txt
After the shell has processed the double-quoted string, the script which ends up being executed is
s/\\"/\\\\'/g
where the first pair of backslashes produce a literal backslash in the matching regex, and each pair of backslashes in the replacement produce one literal backslash in the output.
Demo: https://ideone.com/XqfwbV

How to escape single quote in sed?

How to escape a single quote in a sed expression that is already surrounded by quotes?
For example:
sed 's/ones/one's/' <<< 'ones thing'
Quote sed codes with double quotes:
$ sed "s/ones/one's/"<<<"ones thing"
one's thing
I don't like escaping codes with hundreds of backslashes – hurts my eyes. Usually I do in this way:
$ sed 's/ones/one\x27s/'<<<"ones thing"
one's thing
One trick is to use shell string concatenation of adjacent strings and escape the embedded quote using shell escaping:
sed 's/ones/two'\''s/' <<< 'ones thing'
two's thing
There are 3 strings in the sed expression, which the shell then stitches together:
sed 's/ones/two'
\'
's/'
Escaping single quote in sed: 3 different ways:
From fragile to solid...
Note: This answer is based on GNU sed!!
1. Using double-quotes to enclose sed script:
Simpliest way:
sed "s/ones/one's/" <<< 'ones thing'
But using double-quote lead to shell variables expansion and backslashes to be considered as shell escape before running sed.
1.1. Specific case without space and special chars
In this specific case, you could avoid enclosing at shell level (command line):
sed s/ones/one\'s/ <<<'ones thing'
will work until whole sedscript don't contain spaces, semicolons, special characters and so on... (fragile!)
2. Using octal or hexadecimal representation:
This way is simple and efficient, if not as readable as next one.
sed 's/ones/one\o047s/' <<< 'ones thing'
sed 's/ones/one\x27s/' <<< 'ones thing'
And as following character (s) is not a digit, you coul write octal with only 2 digits:
sed 's/ones/one\o47s/' <<< 'ones thing'
3. Creating a dedicated sed script
cat <<eosedscript >sampleSedWithQuotes.sed
#!$(which sed) -f
s/ones/one's/;
eosedscript
chmod +x sampleSedWithQuotes.sed
From there, you could run:
./sampleSedWithQuotes.sed <<<'ones thing'
one's thing
This is the strongest and simpliest solution as your script is the most readable:$ cat sampleSedWithQuotes.sed
#!/bin/sed -f
s/ones/one's/;
3.1 You coud use -i sed flag:
As this script use sed in shebang, you could use sed flags on command line. For editing file.txt in place, with the -i flag:
echo >file.txt 'ones thing'
./sampleSedWithQuotes.sed -i file.txt
cat file.txt
one's thing
3.2 Mixing quotes AND double quotes
Using dedicated script may simplify mixing quotes and double quotes in same script.
Adding a new operation in our script to enclose the word thing in double quotes:
echo >>sampleSedWithQuotes.sed 's/\bthing\b/"&"/;'
( now our script look like:
#!/bin/sed -f
s/ones/one's/;
s/\bthing\b/"&"/;
)
then
./sampleSedWithQuotes.sed <<<'ones thing'
one's "thing"
The best way is to use $'some string with \' quotes \''
eg:
sed $'s/ones/two\'s/' <<< 'ones thing'
Just use double quotes on the outside of the sed command.
$ sed "s/ones/one's/" <<< 'ones thing'
one's thing
It works with files too.
$ echo 'ones thing' > testfile
$ sed -i "s/ones/one's/" testfile
$ cat testfile
one's thing
If you have single and double quotes inside the string, that's ok too. Just escape the double quotes.
For example, this file contains a string with both single and double quotes. I'll use sed to add a single quote and remove some double quotes.
$ cat testfile
"it's more than ones thing"
$ sed -i "s/\"it's more than ones thing\"/it's more than one's thing/" testfile
$ cat testfile
it's more than one's thing
This is kind of absurd but I couldn't get \' in sed 's/ones/one\'s/' to work. I was looking this up to make a shell script that will automatically add import 'hammerjs'; to my src/main.ts file with Angular.
What I did get to work is this:
apost=\'
sed -i '' '/environments/a\
import '$apost'hammerjs'$apost';' src/main.ts
So for the example above, it would be:
apost=\'
sed 's/ones/one'$apost's/'
I have no idea why \' wouldn't work by itself, but there it is.
Some escapes on AppleMacOSX terminals fail so:
sed 's|ones|one'$(echo -e "\x27")'s|1' <<<'ones thing'
use an alternative string seperator like ":" to avoid confusion with different slashes
sed "s:ones:one's:" <<< 'ones thing'
or if you wish to highligh the single quote
sed "s:ones:one\'s:" <<< 'ones thing'
both return
one's thing

Substitute with backslash in sed

I want to replace word with \word{sth} with sed.
I type in
sed -i s#word#\\word{sth}
but i am getting is word{sth} instead of \word{sth}
I tried with 1 slash also in the command
you should add four backslashes.
you need two to escape the backslash by the terminal, and two to escape it for sed. 2*2=4.
$ echo word|sed s#word#\\\\word{sth}#gi
\word{sth}
Consider enclosing sed expression with single-quotes '
sed -i 's#word#\\word{sth}#' file

sed replacing single quotes with backslash single quote for openssl config file

The context of the problem is that i am trying to pass passphrases that have single quotes in them to the input_password parameter of an openssl configuration file.
Taken from openssl configuration file manual:
It is possible to escape certain characters by using any kind of quote
or the \ character. By making the last character of a line a \ a value
string can be spread across multiple lines. In addition the sequences
\n, \r, \b and \t are recognized
I assumed that single quotes can be escaped. The problem is i am having difficulty producing escaped single quotes with sed.
With this, i mean that the following expressions do not yield the same results if ran from a script or from the command line. Take the following commands:
cut -d: -f2 "$EMPLOYEE_CREDENTIALS_FILE"| sed -e "s|'|\\\'|g"
Where "$EMPLOYEE_CREDENTIALS_FILE" is a file with formated like
username:password
When i run this command directly in bash it produces the output i assumed openssl.conf tolerates:
tentacle caravan sovereignty appraisal sass\'s
And when i run it in the script it just produces the normal unescaped passphrase:
tentacle caravan sovereignty appraisal sass's
I would like to know what is it that i am doing wrong.
I have finally solved the problem.
I am now in a position to confirm that openssl configuration files allow escaped single quotes to appear in the input_password field. Not only that but the actual password used by openssl is unescaped as intended. An end user would never know that the password was escaped in the configuration file.
I had quite some trouble making sed work inside a script because double quotes, from what i understood, consume the backslashes but not the single quotes. On the other hand, single quotes don't consume the backslashes and because of that can't interpret escaped characters. This means i can't escape single quotes inside single quotes.
This took me to a solution
cut -d: -f2 "$EMPLOYEE_CREDENTIALS_FILE"| sed -e "s|'|"'\\\'"'|g"
This is similar to the answer found in this stackoverflow question but it doesn't work for my case.
The reason 3 backslashes are need are also found in the answer:
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.

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 \