Escaping plus in sed regular expression

Congenital Optimist
:

There is a file with following text:

CXX_FLAGS = -fPIC -Wall -Wextra -Wno-missing-braces -ffloat-store -pthread -std=gnu++17

To replace the string "-std=gnu++17" with "-std=c++17 -std=gnu++17", I tried:

sed -i -e 's/\-std\=gnu\+\+17/\-std=c\+\+17 \-std=gnu\+\+17/g' filename

That however does not work, until I remove the \ escape from frst + sign in search expression. So these seem to be OK:

sed -i -e 's/\-std\=gnu++17/\-std=c\+\+17 \-std=gnu\+\+17/g' filename
sed -i -e 's/\-std\=gnu+\+17/\-std=c\+\+17 \-std=gnu\+\+17/g' filename
sed -i -e 's/\-std\=gnu..17/\-std=c\+\+17 \-std=gnu\+\+17/g' filename

I understand the + must be escaped when not in character class, but I thought one can prefix any character with backslash in regex. Why does escaping the + sign here cause the search-replace to fail?

The OS is Ubuntu 20.04 LTS.

Wiktor Stribiżew
:

You have not used -r nor -E option, so you tell sed to parse the regex pattern as a POSIX BRE expression. In GNU sed, in a POSIX BRE expression, \+ is a quantifier matching 1 or more occurrences of the quantified pattern. Run sed -e 's/\-std\=gnu\+\+17/\-std=c\+\+17 \-std=gnu\+\+17/g' <<< '-std=gnuuuu17' and the result will be -std=c++17 -std=gnu++17. To match +, you just need to use +.

Note you overescaped a lot of chars and your command is unnecessarily long because you repeated the pattern in both the LHS and RHS.

You may use the following POSIX BRE sed command with GNU sed:

sed -i 's/-std=gnu++17/-std=c++17 &/' filename

See the sed online demo:

s='CXX_FLAGS = -fPIC -Wall -Wextra -Wno-missing-braces -ffloat-store -pthread -std=gnu++17'
sed 's/-std=gnu++17/-std=c++17 &/' <<< "$s"
# => CXX_FLAGS = -fPIC -Wall -Wextra -Wno-missing-braces -ffloat-store -pthread -std=c++17 -std=gnu++17

Details

  • -std=gnu++17 - the string pattern matches -std=gnu++17 string exactly
  • -std=c++17 & - the replacement pattern is -std=c++17, space and & stands for the whole match, -std=gnu++17.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

regular expression in javascript escaping backslash

From Dev

the use of "+" in sed regular expression

From Dev

sed regular expression failure

From Dev

sed with regular expression

From Dev

the use of "+" in sed regular expression

From Dev

Regular expression with sed

From Dev

sed regular expression extraction

From Dev

sed command with regular expression

From Dev

Negation of sed regular expression

From Dev

Sed Regular Expression with /P

From Dev

+ Regular Expression not working in sed

From Dev

In a regular expression, which characters need escaping?

From Dev

Regular expression, plus vs asterisk

From Dev

sed positional replacement with regular expression

From Dev

Sed multiline regular expression issue

From Dev

understand Regular expression in a sed command

From Dev

sed regular expression failed on solaris

From Dev

sed: regular expression,how to substitute?

From Dev

sed replace regular expression match

From Dev

Using Sed in a for loop with a regular expression

From Dev

sed regular expression for escaped urls

From Dev

Escaping backlash and double quotes inside a sed expression (surrounded by double quotes)

From Dev

PHP Regular Expression - Single Quote not working - TWIG pre-escaping

From Dev

PHP Regular Expression - Single Quote not working - TWIG pre-escaping

From Dev

plus(+) sign in special character match expression of sed

From Dev

perl regular expression match scalar plus punctuation

From Dev

How to match everything but a regular expression with sed?

From Dev

Converting a sed regular expression to python code

From Java

regular expression in sed replace question mark

Related Related

HotTag

Archive