Inserting text to existing text within brackets
Solution 1:
A few things:
-
*
in regex is a quantifier; if you want behavior similar to the shell wildcard you need.*
(any single character, zero or more times) -
.*
matches greedily, so if you want to exclude things like=
as well as the closing]
you need to do so explicitly - that's where you need non-literal[
and]
-
m
does not need to be escaped on either the LHS (regex pattern) or RHS (replacement) -
you need a capture group if you want to re-use (aka backreference) the text between the brackets
-
you don't need
g
unless you have multiple matches per line
So
$ sed -r 's|\[([^=[]*)\]|[/mame/\1]|' file
[/mame/one]
[/mame/two]
[/mame/three]
[four = something]
With Perl (which also has a -i
in-place mode), you could use lookarounds and lazy (non-greedy) matching:
$ perl -pe 's{(?<=\[)([^=]*?)(?=])}{/mame/$1}' file
[/mame/one]
[/mame/two]
[/mame/three]
[four = something]
Solution 2:
I propose this:
sed '/=/!s;^\[;[/mame/;' file
Output:
[/mame/one]
[/mame/two]
[/mame/three]
[four = something]