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 |
This makes the required substitution:
Code:
sed "/^dn: CN=$user\$/,+2 s/AccessLevel: .*/AccessLevel: $new_value/" config.ldif In case you are in a hurry am posting this now and will critique your command later. |
Quote:
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 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. |
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 |
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:
|
Quote:
Is the Solaris 10 man page on the Infernet? |
Quote:
You can find the man page here: http://manpages.unixforum.co.uk/man-...-man-page.html Thanks JB |
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.
|
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 |
That's works perfectly.
Many thanks, really appreciate your help. |
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. |