LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   SED edits configuration file (https://www.linuxquestions.org/questions/programming-9/sed-edits-configuration-file-668977/)

lusiads 09-10-2008 12:48 PM

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.

CRC123 09-10-2008 12:53 PM

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

lusiads 09-10-2008 01:05 PM

Thanks CRC123.
That helps a lot.

lusiads 09-10-2008 11:07 PM

That's OK. I knew it. Thanks.

jan61 09-11-2008 03:45 PM

Moin,

some hints:

Quote:

Originally Posted by CRC123 (Post 3276080)
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

erez.rsd 09-11-2008 04:47 PM

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 :(

keefaz 09-11-2008 05:21 PM

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

lusiads 09-12-2008 03:25 AM

Quote:

Originally Posted by jan61 (Post 3277557)
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.


All times are GMT -5. The time now is 04:21 PM.