LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-13-2011, 08:17 AM   #1
jamesd_burgess
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Rep: Reputation: Disabled
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
 
Old 12-13-2011, 10:12 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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.

Last edited by catkin; 12-13-2011 at 10:13 AM. Reason: speeling
 
Old 12-13-2011, 10:28 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by jamesd_burgess View Post
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.
 
Old 12-13-2011, 10:36 AM   #4
jamesd_burgess
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-13-2011, 10:43 AM   #5
jamesd_burgess
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-13-2011, 10:47 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by jamesd_burgess View Post
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?
 
Old 12-13-2011, 11:12 AM   #7
jamesd_burgess
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-13-2011, 11:51 AM   #8
jamesd_burgess
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-14-2011, 03:51 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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
 
Old 12-14-2011, 04:44 AM   #10
jamesd_burgess
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
That's works perfectly.

Many thanks, really appreciate your help.
 
Old 12-14-2011, 05:02 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Glad you found a solution

Threads can be marked SOLVED via the Thread Tools menu.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk: how to print a field when field position is unknown? elfoozo Programming 12 08-18-2010 04:52 AM
awk printing from Nth field to last field sebelk Programming 2 01-08-2010 10:39 AM
Replace a field for a whole line in the same file, with awk. amwink Programming 12 11-13-2009 07:51 AM
awk to replace particular field vgr12386 Programming 9 06-05-2009 08:15 AM
Find text and replace another field jaysin_aus Linux - Server 9 07-26-2007 03:16 PM


All times are GMT -5. The time now is 09:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration