scooterx3
scooterx3

Reputation: 43

Sed regex isn't being as greedy as it ought

Trying to just let myself know if a wp-config.php file has some particular values in it. In testing I tried to add a copy of the line after a comment:

define( 'WP_ALLOW_MULTISITE', firsttrue ); //define( 'WP_ALLOW_MULTISITE', secondtrue );

And the idea is to have sed spit out 'firsttrue'. Here's what I'd been using that worked fine until this test case:

sed 's|.*\?,\(.*\?\));.*|\1|'

But it just returns 'secondtrue'. Wondering why that is and how to get it to return what I'm after. Gracias.

Upvotes: 1

Views: 47

Answers (2)

Tiago Lopo
Tiago Lopo

Reputation: 7959

Can you user perl? if so:

perl -lne '/.*?,\s+(.*?)\s+\)/ && print $1'

Ex:

[root@TIAGO-TEST tmp]# cat file
define( 'WP_ALLOW_MULTISITE', firsttrue ); //define( 'WP_ALLOW_MULTISITE', secondtrue );
[root@TIAGO-TEST tmp]# cat file | perl -lne '/.*?,\s+(.*?)\s+\)/ && print $1'
firsttrue

I never managed to get non-greedy match working for sed.

Upvotes: 1

slevin
slevin

Reputation: 318

I don't know if that is possible in sed. You might want to add some character classes.

sed 's|[^,]*,\([^);]*\));.*|\1|'

So you look for as many non-comma characters as you can, so you're guaranteed to match on the first comma, and then return the first string after , and before );. If you want to also not capture any spaces, add some space character groups.

sed 's|[^,]*,\s*\([^);]*\)\s*);.*|\1|'

Upvotes: 0

Related Questions