Special characters in Sed's replacement space [duplicate] - sed

This question already has answers here:
Is it possible to escape regex metacharacters reliably with sed
(4 answers)
Closed 3 years ago.
Reading sed manual, It seems only \ and & are to be watch out for in replacement space when using the s command in sed.
Are there any other characters having special meaning in replacement space? (Affecting the replacement)

Short Answer: You need to quote '\' '&' and new line.
Long Answer: The 'sed' man page indicates
To include a literal '\', '&', or newline in the final replacement,
be sure to precede the desired '\', '&', or newline in the REPLACEMENT
with a '\'.
Implying all other characters are valid literals in the REPLACEMENT string.

Related

Using sed -e to replace slash

I am trying to understand what this below command does with -e in sed and exclamatory marks in the command,
sed -e "s!VPC_CIDR!"$(get_cluster_vpc_cidr)"!g" "templates/network-policies-${ns}.yaml"
This command helped to replace VPC_CIDR with 1.2.3.4\16.
Could someone through light on this please?
-e option just tells sed that the next argument is the script to execute. "s!VPC_CIDR!"$(get_cluster_vpc_cidr)"!g" is the script.
The " usage is strange. I would just "s!VPC_CIDR!$(get_cluster_vpc_cidr)!g". Because $(get_cluster_vpc_cidr) is not within " quotes, the result will undergo word splitting and filename expansion. Ie. it will fail on spaces and * or ? characters may work strangely.
The "s!VPC_CIDR!"$(get_cluster_vpc_cidr)"!g" is a sed script. The s command does, from man 1 sed:
s/regexp/replacement/
Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may con‐
tain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to
the corresponding matching sub-expressions in the regexp.
But you think - och ! is not /! But, as man 1 sed tells us This is just a brief synopsis of sed commands to serve as a reminder to those who already know sed. The POSIX sed or man 7 sed page will shed some more light:
[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 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>.
Any character. You can evey pass byte 0x01, like sed $'s\x01BRE\x01replacement\x01' and it's a valid script.
So s!VPC_CIDR!$(get_cluster_vpc_cidr)!g command replaces every occurence (ie. the g global flag) of the VPC_CIDR string (the string is literal, there are no special regex expressions there) for the output of $(get_cluster_vpc_cidr) (except that & and \1 and such are interpreted specially in replacement part).

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.

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.

What does this line do in Perl? [duplicate]

This question already has answers here:
How to Replace white space in perl
(3 answers)
Closed 5 years ago.
What does this line do in Perl?
s/\s//g;
I'm looking at a script that is used to search and count certain characters in an input file and I understand everything in the code except for this line. I was wondering what this line did for the script?
s/\s//g;
is short for
$_ =~ s/\s//g;
It is a substitution operator bound to $_. It replaces all sequences in $_ that match the regex pattern \s with nothing. (Without g, it would only replace the first.)
\s matches a character of whitespace.

What are the matlab equivalent for '\n' '\t' and other escape characters? [duplicate]

This question already has answers here:
Escape characters in Matlab
(2 answers)
Closed 5 years ago.
I am reading a file using fileread() which returns me the entire file. Now I need to read line by line and convert them into process the data. Can I know how I would be able to detect the newline character in Matlab? I tried '\n' and '\r\n' and it doesn't work.
Thanks in advance
I already replied here (please avoid posting the question twice)Escape characters in Matlab
For special acharacters either use the char function with the character code (http://www.asciitable.com/) or sprintf (my preferred way for better readability. For example you are looking for sprintf('\n') or sprintf('\r\n')
char(13) is carriage return \r char(10) is new line \n