hello,
in a config file in which I need to replace (or add if not existing at all) an option. Also, if the option is lead by a space(s) or tab(s) (or such mixed together). Each option is followed by = sign, but there also might be some white characters beetween the option and = sign, but such is valid for my replacmenet (in case of multiple matches all matches should be replaced with single line, all the rest not touched).
So, matching candidates could be:
and not matching like:
First I tried to find correct regex in grep:
so because it still procuded unwanted line (last one) above, I tried with this, but it removed also those two lines:
astring =abcd
astring =
Another issue is that perl:
and expected was:
so all "candidate lines" replaced with single "astring=desired" (or parameter added if not existing yet) and all other lines unchanged in config.
best regards.
in a config file in which I need to replace (or add if not existing at all) an option. Also, if the option is lead by a space(s) or tab(s) (or such mixed together). Each option is followed by = sign, but there also might be some white characters beetween the option and = sign, but such is valid for my replacmenet (in case of multiple matches all matches should be replaced with single line, all the rest not touched).
So, matching candidates could be:
Code:
option =
option=
option == =
option=
and not matching like:
Code:
optionb=
optionc=
#sdffs
bleble option=
ble option 2 =
First I tried to find correct regex in grep:
Code:
$ cat input
#astring=
optionb=
#bleble
optionc=
astring=bleble
astring =abcd
astring =
astring=
astring =
astring===
astring
bleble astring
ddddd
$
Code:
$ grep -E '(^|[[:space:]])astring([[:space:]]|=|$)' input
astring=bleble
astring =abcd
astring =
astring=
astring =
astring===
astring
bleble astring
so because it still procuded unwanted line (last one) above, I tried with this, but it removed also those two lines:
astring =abcd
astring =
Code:
$ grep -E '(^|^[[:space:]])astring([[:space:]]|=|$)' input
astring=bleble
astring=
astring =
astring===
astring
$
Another issue is that perl:
Code:
$ perl -pe '/astring/ && s/(^|^[[:space:]])astring([[:space:]]|=|$)/astring=desired/' input
#astring=
optionb=
#bleble
optionc=
astring=desiredbleble
astring =abcd
astring =
astring=desired
astring=desired=
astring=desired==
astring=desiredbleble astring
ddddd
$
and expected was:
Code:
#astring=
optionb=
#bleble
optionc=
astring=desired
astring
bleble astring
ddddd
so all "candidate lines" replaced with single "astring=desired" (or parameter added if not existing yet) and all other lines unchanged in config.
best regards.