0

I'm making replacments in PHP file and I need to change the language variable with sed. Here my WORKING code:

sed -i '' -e "s/\$config\['language'\]  = \"english\";/\$config['language'] = '$LANGUAGE';/" Sources/$APP/application/config/config.php

This is not working to match any language set:

sed -i '' -e "s/\$config\['language'\]  = \"*\";/\$config['language'] = '$LANGUAGE';/" Sources/$APP/application/config/config.php

What's wrong?

0

3 Answers 3

1

In sed, the asterisk (*) character denotes "repeat the previous thing 0 or more times." This is in contrast to a shell, where it expands to anything. What you want to do is shove a . (which means "anything"), right before the asterisk, like so:

sed -i '' -e "s/\$config\['language'\]  = \".*\";/\$config['language'] = '$LANGUAGE';/" Sources/$APP/application/config/config.php

That will then tell your program "repeat any character (.) any number of times (*)".

Sign up to request clarification or add additional context in comments.

Comments

1

That's because * doesn't do what you think it does. In a sed regular expression, as in all regular expressions, use .*? to mean any collection of characters (except newlines). That is because . means match anything once and * means match the previous item any number of times. The ? makes it non-greedy, meaning it will match as few characters as possible as long as the rest of the expression matches. I don't know what you input is so I can't tell if you need the question mark, better safe than sorry.

Comments

1

You need to replace the * in the expression with .*. * means "0 or more instances of the previous item" (in this case the "), so you want to first match . (any character), then state you want 0 or more instances of that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.