INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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.

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

Hi

Regarding your grep, I would write it as :

CODE --> command line

grep -E '^\s*astring\s*=' input 

Regarding your perl, I would write it as :

CODE --> command line

perl -pe '$f=1 if s/^(\s*astring\s*=).*/$1NEW VALUE/;END{print"astring=NEW VALUE\n"unless$f}' input 

Feherke.
feherke.github.io

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

(OP)
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

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


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
xxxxxxxxx=desired
xxxxxxxxx 11
vvvv xxxxxxxxx 12
ddddd 13

$ 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close