[SOLVED] Replace field value in Configuration File
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.