LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Replace field value in Configuration File (https://www.linuxquestions.org/questions/linux-newbie-8/replace-field-value-in-configuration-file-918497/)

jamesd_burgess 12-13-2011 07:17 AM

Replace field value in Configuration File
 
Hi, I'm pretty new to scripting and having some difficulties with a sed command on Solaris 10. I need to be able to select a user from a configuration file and change an AccessLevel parameter within that section (see below). I've created a script that allows me to select the user and also the correct value within that section, but I can't work out how to write the new value.

The configuration looks as below:

dn: CN=User1
ObjectClass: Person
AccessLevel: 1023

dn: CN=User2
ObjectClass: Person
AccessLevel: 1023

The script I'm using is as follows:

#!/usr/bin/bash
old_value=1023
new_value=1024
user=User1
`sed -n "/$user/,/AccessLevel/p" config.ldif | grep -w AccessLevel | head -1 | sed 's/^.* //' | sed "s/$old_value/$new_value/" config.ldif > config.ldif.tmp


I can see why this doesn't work as the final sed command is working on the original copy of the configuration and not the pattern I want it to match.

How can I amend this to write the new value?

Grateful for any help.

Many thanks
JB

catkin 12-13-2011 09:12 AM

This makes the required substitution:
Code:

sed "/^dn: CN=$user\$/,+2 s/AccessLevel: .*/AccessLevel: $new_value/" config.ldif
When you are confident it does what you want you can use sed's -i option to change the file "in place".

In case you are in a hurry am posting this now and will critique your command later.

catkin 12-13-2011 09:28 AM

Quote:

Originally Posted by jamesd_burgess (Post 4548701)
I can see why this doesn't work as the final sed command is working on the original copy of the configuration and not the pattern I want it to match.

How can I amend this to write the new value?

There's an unmatched backquote (`) before sed.

It is instructive to run the parts of that pipeline progressively:
Code:

c@CW8:/tmp$ sed -n "/$user/,/AccessLevel/p" config.ldif | grep -w AccessLevel
AccessLevel: 1023
c@CW8:/tmp$ sed -n "/$user/,/AccessLevel/p" config.ldif | grep -w AccessLevel | head -1
AccessLevel: 1023
c@CW8:/tmp$ sed -n "/$user/,/AccessLevel/p" config.ldif | grep -w AccessLevel | head -1 | sed 's/^.* //'
1023

As can be seen the head -1 does nothing.

If you did use the 1023 to modify config.ldif, it would change every occurrence of 1023, not just User1's.

Another thing to be wary of is that matching Fred will also match Frederick unless you anchor the regex to the end of line with the $ metacharacter.

jamesd_burgess 12-13-2011 09:36 AM

Many thanks for the reply. Unfortunately I can't use the -i option with Solaris 10. If I use the command I get the following message:

sed: command garbled: /^dn: CN=$user\$/,+2 s/AccessLevel: .*/AccessLevel: 1024/

Any ideas?

Thanks
JB

jamesd_burgess 12-13-2011 09:43 AM

Solaris 10 seems to use quite an old/limited version of sed and if I didn't use head -1 it was returning other lines starting with the same string which I'm assuming is because of what you mentioned below? I'll have a go at modifying that bit to return an exact match.

Quote:

As can be seen the head -1 does nothing.

If you did use the 1023 to modify config.ldif, it would change every occurrence of 1023, not just User1's.

Another thing to be wary of is that matching Fred will also match Frederick unless you anchor the regex to the end of line with the $ metacharacter.

catkin 12-13-2011 09:47 AM

Quote:

Originally Posted by jamesd_burgess (Post 4548800)
sed: command garbled: /^dn: CN=$user\$/,+2 s/AccessLevel: .*/AccessLevel: 1024/

Is that with or without the -i option?

Is the Solaris 10 man page on the Infernet?

jamesd_burgess 12-13-2011 10:12 AM

Quote:

Is that with or without the -i option?

Is the Solaris 10 man page on the Infernet?
That's without -i as it's an illegal option in Solaris 10.

You can find the man page here: http://manpages.unixforum.co.uk/man-...-man-page.html

Thanks
JB

jamesd_burgess 12-13-2011 10:51 AM

Apologies as I should have also mentioned that AccessLevel will appear beneath dn: but the number of lines below is unknown. So using the +2 option in the command wouldn't work unfortunately. The config was just an example of what I was trying to do.

catkin 12-14-2011 02:51 AM

Thanks for the Solaris sed man page. It does not describe the +n address format and anyway you cam't use it so maybe this would work:
Code:

sed "/^dn: CN=$user\$/,/^AccessLevel: / s/AccessLevel: .*/AccessLevel: $new_value/" config.ldif

jamesd_burgess 12-14-2011 03:44 AM

That's works perfectly.

Many thanks, really appreciate your help.

catkin 12-14-2011 04:02 AM

Glad you found a solution :)

Threads can be marked SOLVED via the Thread Tools menu.


All times are GMT -5. The time now is 10:02 AM.