How can I use a * as text in a SED? - sed

I am trying to replace CEName.objects.create(**name_data) with self.create() like this:
sed 's/CEName.objects.create(**name_data)/self.create()/g'
Except it doesn't like the *. How can I escape them to make this work?

. and * needs to be interpreted literally,so the characters . and * need to be quoted by preceding them by a backslash \.
Check here: What characters do I need to escape when using sed in a sh script?
sed 's/CEName\.objects\.create(\*\*name_data)/self\.create()/g'
Output:
self.create()

Related

confused about what must be escaped for sed

I want to replace specific strings in php files automatically using sed. Some work, and some do not. I already investigated this is not an issue with the replacement string but with the string that is to be replaced. I already tried to escape [ and ] with no success. It seems to be the whitespace within the () - not whitespaces in general. The first whitespaces (around the = ) do not have any problems. Please can someone point me to the problem:
sed -e "1,\$s/$adm = substr($path . rawurlencode($upload['name']) , 16);/$adm = rawurlencode($upload['name']); # fix 23/g" -i administration/identify.php
I already tried to shorten the string which should be replaced and the result was if I cut it directly behind $path it works, with the following whitespace it does not. Escaping whitespace has no effect...
what must be escaped for sed
The following characters have special meaning in sed and have to be escaped with \ for the regex to be taken literally:
\
[
the character used in separating s command parts, ie. / here
.
*
& only replacement string
Newline character is handled specially as the end of the string, but can be replaced for \n.
So first escape all special characters in input and then pass it to sed:
rgx="$adm = substr($path . rawurlencode($upload['name']) , 16);"
rgx_escaped=$(sed 's/[\\\[\.\*\/&]/\\&/g' <<<"$rgx")
sed "s/$rgx_escaped/ etc."
See Escape a string for a sed replace pattern for a generic escaping solution.
You may use
sed -i 's/\$adm = substr(\$path \. rawurlencode(\$upload\['"'"'name'"'"']) , 16);/$adm = rawurlencode($upload['"'"'name'"'"']); # fix 23/g' administration/identify.php
Note:
the sed command is basically wrapped in single quotes, the variable expansion won't occur inside single quotes
In the POSIX BRE syntax, ( matches a literal (, you do not need to escape ) either, but you need to escape [ and . that must match themselves
The single quotes require additional quoting with concatenation.

Replacing escape sequences with Sed

With the following text file, test.txt:
{\x22order\x22:\x22161332\x22,\x22name\x22:\x22chiller\x22,\x22code\x22:\x22chiller\x22}
{\x22order\x22:\x22161332\x22,\x22name\x22:\x22chiller\x22,\x22code\x22:\x22chiller\x22}
{\x22order\x22:\x22161332\x22,\x22name\x22:\x22chiller\x22,\x22code\x22:\x22chiller\x22}
How do I replace occurrences of \x22 with single quotation marks in Sed?
I've tried this with no avail: sed -i "s#\x22#'#g" test.txt
You need to escape the backslash to make it literal and you need to use '\'' to represent a single quote in a single-quote-delimited (which they all should be unless absolutely necessary to be otherwise) string or script
$ sed 's/\\x22/'\''/g' file
{'order':'161332','name':'chiller','code':'chiller'}
{'order':'161332','name':'chiller','code':'chiller'}
{'order':'161332','name':'chiller','code':'chiller'}

Replace special character "/" using sed command [duplicate]

This question already has an answer here:
Escaping forward slashes in sed command [duplicate]
(1 answer)
Closed 4 years ago.
I want to replace special character "/" with a special character "*" using sed command.
EXAMPLE-
I / YOU.
I * YOU.
As detailed in the comment Escaping forward slashes in sed command
You can use instead of
sed "s/target/replacement/" file
either
sed "s|target|replacement|" file
or
sed "s#target#replacement#" file
Command:
$ echo "I / YOU." | sed 's#/#*#'
I * YOU.
More generally when looking at the sed accepted syntax:
sed
[2addr] s/BRE/replacement/flags
Substitute the replacement string for instances of the BRE in the
pattern space. Any character other than backslash or newline can be
used instead of a slash to delimit the BRE and the replacement. Within
the BRE and the replacement, the BRE delimiter itself can be used as a
literal character if it is preceded by a backslash.
You can also go for another approach in which you do not change the separators but you use the hexadecimal value of the character you want to replace, this will also avoid ambiguity. (http://www.asciitable.com/)
$ echo "I / YOU." | sed 's/\x2F/*/'
I * YOU.

How to use sed to substitute subject to conditions?

I have a text file line:
110254,TRCN,"OJSC "Transcontainer",LSE,1,2014-05-21,2014-11-28,,,2,,,0,
I would like to change the middle qoute character from " into a \", but not the ones following or preceding a comma.
More generally, I would like to use sed to do the following:
Ignore /, *"/
Ignore /" *,/
Ignore /^ *"/
Ignore /" *$/
Substitute all other " characters for \"
How do I go about doing this with sed?
If I understood the requirements, I believe that you can use :
sed 's/\([^,]\+\)"\([^,]\+\)/\1\\"\2/g'
It matches any " not followed nor preceeded by a comma, but with any other text, and replaces it with \". For this I used ^ to negate the comma.
> echo '110254,TRCN,"OJSC "Transcontainer",LSE,1,2014-05-21,2014-11-28,,,2,,,0,' | sed 's/\([^,]\+\)"\([^,]\+\)/\1\\"\2/g'
110254,TRCN,"OJSC \"Transcontainer",LSE,1,2014-05-21,2014-11-28,,,2,,,0,
This might work for you (GNU sed):
sed ':a;s/\([^,\\]\)"\([^,\\]\|$\)/\1\\"\2/g;ta' file

Basic SED question for Linux

Temp file has only the number 22.5 in it.
I use
sed 's/.//' Temp
and I expect 225 but get 2.5
Why?
The dot is a special character meaning "match any character".
$ sed s/\\.// temp
225
You would think that you could do sed s/\.// temp, but your shell will escape that single backslash and pass s/.// to sed.. So, you need to put two backslashes to pass a literal backslash to sed, which will properly treat \. as a literal dot. Or, you could quote the command to retain the literal backslash:
$ sed "s/\.//" temp
225
The reason you get 2.5 when you do s/.// is that the dot matches the first character in the file and removes it.
Because '.' is a regular expression that matches any character. You want 's/\.//'
. is a wildcard character for any character, so the first character is replaced by nothing, then sed is done.
You want sed 's/\.//' Temp. The backslash is used to escape special characters so that they regain their face value.
'.' is special: it matches any single character. So in your case, the sed expression matches the first character on the line. Try escaping it like this:
s/\.//
you can also use awk
awk '{sub(".","")}1' temp