LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-10-2008, 01:48 PM   #1
lusiads
LQ Newbie
 
Registered: Sep 2008
Posts: 17

Rep: Reputation: 0
Question SED edits configuration file


Hi everybody,

I want to write a simple bash script that changes specific options from a configuration file (called conf_file).
Conf_file format is somewhat similar to this:
Code:
[session1]
aaa = 0
bbb = 0
ccc = 0
[session2]
aaa = 0
I want to change aaa & bbb to 1, so I do as following:
Code:
sed '
  /\[session1\]/,/\[/ {
    s/\(\(aaa\|bbb\)\ *=.*\)$/\2 = 1/
  }' <conf_file
It works. Just one problem, when [session1] is not exist, I want to create new [session] and append it with new aaa = 1 & bbb = 1.
Could anyone tell me how to archive this?
I prefer sed but any other methods also be welcome.
 
Old 09-10-2008, 01:53 PM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 31
I would grep for session first and then do something accordingly:
Code:
if [ `grep -c "[session1]"` -gt 0 ]; then
   sed '
     /\[session1\]/,/\[/ {
       s/\(\(aaa\|bbb\)\ *=.*\)$/\2 = 1/
     }' <conf_file
else
   echo -e "[session]\naaa = 1\nbbb = 1" >> conf_file
fi
EDIT: addes " >> conf_file" after the echo statement in my script (don't know why i forgot it) lol

Last edited by CRC123; 09-10-2008 at 02:07 PM.
 
Old 09-10-2008, 02:05 PM   #3
lusiads
LQ Newbie
 
Registered: Sep 2008
Posts: 17

Original Poster
Rep: Reputation: 0
Thumbs up

Thanks CRC123.
That helps a lot.
 
Old 09-11-2008, 12:07 AM   #4
lusiads
LQ Newbie
 
Registered: Sep 2008
Posts: 17

Original Poster
Rep: Reputation: 0
That's OK. I knew it. Thanks.
 
Old 09-11-2008, 04:45 PM   #5
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

some hints:

Quote:
Originally Posted by CRC123 View Post
Code:
if [ `grep -c "[session1]"` -gt 0 ]; then
...
What do you grep here? Missing filename. And you don't have to count the matching lines and do an extra test, grep can tell you, if the pattern was found:
Code:
if grep -q "^\[session1\]" conf_file; then
...
The sed can be written much easier using extended regex and dropping some unnecessary escapes and braces. And why do you use input redirection to feed it? sed can directly read files given as command line argument:
Code:
sed -r '
  /\[session1\]/,/\[/ {
    s/(aaa|bbb) *=.*/\1 = 1/
  }' conf_file
should work exactly like your code. Last but not least: sed -i allows you to edit your file in place.

So I would prefer this code:
Code:
if grep -q "^\[session1\]" conf_file; then
  sed -ri '
  /\[session1\]/,/\[/ {
    s/(aaa|bbb) *=.*/\1 = 1/
  }' conf_file
else
   echo -e "[session]\naaa = 1\nbbb = 1" >>conf_file
fi
Jan
 
Old 09-11-2008, 05:47 PM   #6
erez.rsd
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Rep: Reputation: 0
Sed

hello everyone!

i need do drop the '/ptw' from a passwd file the users are added to directory user:x:557:503::/ptw/home/user:/bin/bash and i need to drop the '/ptw' from every user that i am adding

wasn't able to accomplish the mission

Last edited by erez.rsd; 09-11-2008 at 05:49 PM.
 
Old 09-11-2008, 06:21 PM   #7
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
Try change the default home directory for useradd :
Code:
useradd -D -b /home
This way the next user you add in your system will have his /home/username directory
A quick sed fix for the actual /etc/passwd file would be:
Code:
sed -i 's#/ptw##' /etc/passwd

Last edited by keefaz; 09-11-2008 at 06:24 PM.
 
Old 09-12-2008, 04:25 AM   #8
lusiads
LQ Newbie
 
Registered: Sep 2008
Posts: 17

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by jan61 View Post
Moin,

some hints:



What do you grep here? Missing filename. And you don't have to count the matching lines and do an extra test, grep can tell you, if the pattern was found:
Code:
if grep -q "^\[session1\]" conf_file; then
...
The sed can be written much easier using extended regex and dropping some unnecessary escapes and braces. And why do you use input redirection to feed it? sed can directly read files given as command line argument:
Code:
sed -r '
  /\[session1\]/,/\[/ {
    s/(aaa|bbb) *=.*/\1 = 1/
  }' conf_file
should work exactly like your code. Last but not least: sed -i allows you to edit your file in place.

So I would prefer this code:
Code:
if grep -q "^\[session1\]" conf_file; then
  sed -ri '
  /\[session1\]/,/\[/ {
    s/(aaa|bbb) *=.*/\1 = 1/
  }' conf_file
else
   echo -e "[session]\naaa = 1\nbbb = 1" >>conf_file
fi
Jan
Thanks jan61,
Your suggestions was great.
I'm just new to bash scripting and both your suggestions was very useful to me.
CRC123 gave me the idea and help me figure out the right solution with if else condition & command substitution.
I relied on input & output redirections because they are consistent among Unix utilities. However, I'd be glad to learn more about sed & grep, thanks a lot for advices.
 
  


Reply

Tags
bash, sed


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
sed: print section of file from string to end of file samyboy Linux - Newbie 4 02-26-2008 08:23 AM
fstab edits won't stick in SM 6.5? bluesman2333 MEPIS 7 04-17-2007 07:48 PM
filesize limit on 'cat $file | sed > $file' drkstr Linux - Software 2 07-10-2006 03:47 AM
Code edits not showing in C++ Rojon Programming 3 06-02-2006 10:09 AM
file edits not "taking" in website phlewis Linux - General 1 02-07-2004 12:31 PM


All times are GMT -5. The time now is 10:26 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration