Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

edit/fix a parameter in a config file from command line

Status
Not open for further replies.

ogniemi

Technical User
Nov 7, 2003
1,041
PL
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:
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.

 
Hi

Regarding your [tt]grep[/tt], I would write it as :
Code:
grep -E '^\s*astring\s*=' input

Regarding your [tt]perl[/tt], I would write it as :
Code:
perl -pe '$f=1 if s/^(\s*astring\s*=).*/$1NEW VALUE/;END{print"astring=NEW VALUE\n"unless$f}' input

Feherke.
feherke.github.io
 
I'm afraid \s is not standard regex ...
Try this instead:
grep '^[[:space:]]*astring[[:space:]]*=' input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hello,

thank you. Finally following command produces expected output but:

1) add contition that in case there is yet no matching ""xxxxxxxxx="" option in the config file, so in case this:
/(^[[:space:]]*)xxxxxxxxx([[:space:]]*=)/ && s/(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)/xxxxxxxxx=desired/
gets nothing, then add xxxxxxxxx=desired to the input file.
2) how to combine both perl commands into one (not use pipe), to be able modify input file with perl -pi.bak...

Code:
[blue]perl -pe '/(^[[:space:]]*)xxxxxxxxx([[:space:]]*=)/ && s/(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)/xxxxxxxxx=desired/'[/blue] input|[green]perl -ne'$s{$_}++||print'[/green]


And here are some outputs from regex testings:
Code:
$ echo " xxxxxxxxx= "|grep -E '(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)'
 xxxxxxxxx=
$ echo " xxxxxxxxx="|grep -E '(^[[:space:]]*)xxxxxxxxx([[:space:]]*=)'
 xxxxxxxxx=
$ echo " xxxxxxxxx="|grep -E '(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)'
 xxxxxxxxx=
$ echo " xxxxxxxxx ="|grep -E '(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)'
 xxxxxxxxx =
$ echo " #xxxxxxxxx ="|grep -E '(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)'
$ echo " xxxxxxxx ="|grep -E '(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)'
$ echo "xxxxxxxxx ="|grep -E '(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)'
xxxxxxxxx =
$ echo "xxxxxxxxx 1="|grep -E '(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)'
$ echo "xxxxxxxxx\t 1="|grep -E '(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)'
$ echo "xxxxxxxxx\t =="|grep -E '(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)'
xxxxxxxxx        ==
$

sample input file:
Code:
$ cat input
#xxxxxxxxx= 1
yyyyyyyyyy= 2
 #bbbbbbbbb 3
#xxxxxxxxx 9999
cccccccccc= 4
xxxxxxxxx=val1 5
        xxxxxxxxx =val2 6
   xxxxxxxxx   = 7
xxxxxxxxx= 8
        xxxxxxxxx       = 9
xxxxxxxxx=== 10
xxxxxxxxx 11
vvvv xxxxxxxxx 12
ddddd 13

$


Code:
$ grep -E '(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)' input |tee 1
xxxxxxxxx=val1 5
        xxxxxxxxx =val2 6
   xxxxxxxxx   = 7
xxxxxxxxx= 8
        xxxxxxxxx       = 9
xxxxxxxxx=== 10
$ grep -vE '(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)' input |tee 2
#xxxxxxxxx= 1
yyyyyyyyyy= 2
 #bbbbbbbbb 3
#xxxxxxxxx 9999
cccccccccc= 4
xxxxxxxxx 11
vvvv xxxxxxxxx 12
ddddd 13

$ cat 1 2|sort|cksum
118428776 213
$ sort input|cksum
118428776 213

$ perl -pe '/(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)/ && s/(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)/xxxxxxxxx=desired/' input|tee output
#xxxxxxxxx= 1
yyyyyyyyyy= 2
 #bbbbbbbbb 3
#xxxxxxxxx 9999
cccccccccc= 4
xxxxxxxxx=desired
xxxxxxxxx=desired
xxxxxxxxx=desired
xxxxxxxxx=desired
xxxxxxxxx=desired
xxxxxxxxx=desired
xxxxxxxxx 11
vvvv xxxxxxxxx 12
ddddd 13

$ diff -y input output
#xxxxxxxxx= 1                                                   #xxxxxxxxx= 1
yyyyyyyyyy= 2                                                   yyyyyyyyyy= 2
 #bbbbbbbbb 3                                                    #bbbbbbbbb 3
#xxxxxxxxx 9999                                                 #xxxxxxxxx 9999
cccccccccc= 4                                                   cccccccccc= 4
xxxxxxxxx=val1 5                                              | xxxxxxxxx=desired
        xxxxxxxxx =val2 6                                     | xxxxxxxxx=desired
   xxxxxxxxx   = 7                                            | xxxxxxxxx=desired
xxxxxxxxx= 8                                                  | xxxxxxxxx=desired
        xxxxxxxxx       = 9                                   | xxxxxxxxx=desired
xxxxxxxxx=== 10                                               | xxxxxxxxx=desired
xxxxxxxxx 11                                                    xxxxxxxxx 11
vvvv xxxxxxxxx 12                                               vvvv xxxxxxxxx 12
ddddd 13                                                        ddddd 13

$ perl -pe '/(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)/ && s/(^[[:space:]]*)xxxxxxxxx([[:space:]]*=.*)/xxxxxxxxx=desired/' input|perl -ne'$s{$_}++||print'
#xxxxxxxxx= 1
yyyyyyyyyy= 2
 #bbbbbbbbb 3
#xxxxxxxxx 9999
cccccccccc= 4
[green]xxxxxxxxx=desired[/green]
xxxxxxxxx 11
vvvv xxxxxxxxx 12
ddddd 13

$

 
Please, post here: forum219

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top