Replace "Double Backslash" with a single backslash using sed? - sed

How can I replace a string containing double backslash with single backslash using sed?
for example:
Orignal: a\\b\\c
Result: a\b\c

Related

How can I search and replace for two characters with a string in between and only replace the latter character using sed?

Take the following string, for example:
some random text*
- sed is actually not that easy.*
other text
How can I search for lines containing both - and *, then replace the asterisk in that line with a string?
(For Example) Using the string "test" to replace the asterik in matched lines, the output would look this:
some random text*
- sed is actually not that easy.test
other text
I tried
sed -i '' 's/\- .*\*/\n\n:::\n/g';
But the problem with that is that it replaces the whole line, instead of just the asterisk.
If you want to match and keep the hyphen at the start of the line, and replace the asterix at the end of the line, you can use a capture group \(...\) for what you want to keep and use that group in the replacement with \1
sed -i 's/^\(- .*\)\*$/\1test/' file
The contents of file will be:
some random text*
- sed is actually not that easy.test
other text
If the characters are not directly at the start or end of the string, you can remove the anchors ^ and $

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'}

I have string containing "/" character. how could i delete the line having this string in sed? [duplicate]

I am using sed in a shell script to edit filesystem path names. Suppose I want to replace
/foo/bar
with
/baz/qux
However, sed's s/// command uses the forward slash / as the delimiter. If I do that, I see an error message emitted, like:
▶ sed 's//foo/bar//baz/qux//' FILE
sed: 1: "s//foo/bar//baz/qux//": bad flag in substitute command: 'b'
Similarly, sometimes I want to select line ranges, such as the lines between a pattern foo/bar and baz/qux. Again, I can't do this:
▶ sed '/foo/bar/,/baz/qux/d' FILE
sed: 1: "/foo/bar/,/baz/qux/d": undefined label 'ar/,/baz/qux/d'
What can I do?
You can use an alternative regex delimiter as a search pattern by backslashing it:
sed '\,some/path,d'
And just use it as is for the s command:
sed 's,some/path,other/path,'
You probably want to protect other metacharacters, though; this is a good place to use Perl and quotemeta, or equivalents in other scripting languages.
From man sed:
/regexp/
Match lines matching the regular expression regexp.
\cregexpc
Match lines matching the regular expression regexp. The c may be any character other than backslash or newline.
s/regular expression/replacement/flags
Substitute the replacement string for the first instance of the regular expression in the pattern space. Any character other than backslash or newline can be used instead of a slash to delimit the RE and the replacement. Within the RE and the replacement, the RE delimiter itself can be used as a literal character if it is preceded by a backslash.
Perhaps the closest to a standard, the POSIX/IEEE Open Group Base Specification says:
[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."
When there is a slash / in theoriginal-string or the replacement-string, we need to escape it using \. The following command is work in ubuntu 16.04(sed 4.2.2).
sed 's/\/foo\/bar/\/baz\/qux/' file

Cannot include space in string in one line program mode for Perl on Windows?

The command
perl -ne "print "" """ AnyTextFile.txt
running on Windows with latest ActivePerl installed (5.020) complains Can't find string terminator '"' anywhere before EOF at -e line 1.. Other characters or variables work as expected, like
perl -ne "print ""$.-$_""" AnyTextFile.txt
I checked that double quotes are passed to perl as expected, even if it is a little weird when escape double quotes in cmd.exe. Why space cannot be shown in the above double quoted string? Using single quote could work but it loses variables interpolation functionality.
perl -ne "print \" \"" AnyTextFile.txt
Why?
A lot of programs get its arguments by means of the standard argument parser used by the C library initially used to compile the language itself, its libraries or used as a base.
For windows, in general, the "rules" for argument parsing are
Arguments are delimited by white space, which is either a space or a tab.
A string surrounded by double quotation marks is interpreted as a
single argument, regardless of white space contained within. A quoted
string can be embedded in an argument. Note that the caret (^) is not
recognized as an escape character or delimiter.
A double quotation mark preceded by a backslash, \", is interpreted as
a literal double quotation mark (").
Backslashes are interpreted literally, unless they immediately precede
a double quotation mark.
If an even number of backslashes is followed by a double quotation
mark, then one backslash () is placed in the argv array for every
pair of backslashes (\), and the double quotation mark (") is
interpreted as a string delimiter.
If an odd number of backslashes is followed by a double quotation
mark, then one backslash () is placed in the argv array for every
pair of backslashes (\) and the double quotation mark is interpreted
as an escape sequence by the remaining backslash, causing a literal
double quotation mark (") to be placed in argv.