Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression substitution

Tags:

regex

linux

sed

gnu

I am trying to perform some modification on a typical g++ command line. I am trying to construct a regex looking for -o object_filename.o constructs. In order to test my matching pattern, I launch it in sed with an empty string as a substitution. But it does not work as expected since it removes nothing...

$echo "-o toto.o" | sed 's/-o [^ ]+//'
-o toto.o

like image 478
yves Baumes Avatar asked Jun 08 '26 09:06

yves Baumes


1 Answers

You are missing a -E flag; without it, + is not treated as a regex meta-character:

$echo "-o toto.o" | sed -E 's/-o [^ ]+//'

The above produces an empty output, as expected.

like image 109
Sergey Kalinichenko Avatar answered Jun 11 '26 00:06

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!