2

I want to uncomment the wal_level parameter and change the value from minimal to archive

how to perform this action by sed / perl one-liner ?

$ grep wal_level  /var/lib/pgsql/data/postgresql.conf
#wal_level = minimal                    # minimal, archive, or hot_standby

expected results:

  wal_level = archive                    # minimal, archive, or hot_standby

Note that wal_level could be couple of spaces from the "#".

For example:

#     wal_level = minimal   

or

#       wal_level = minimal   

etc.

3 Answers 3

3

This should work:

sed 's/^ *# *wal_level *= *[^ ]*/wal_level = archive/'

I also allow for a space before the comment (just in case) and 0 or more spaces everywhere else. Alternatively, in perl:

perl -pe 's/^\s*#\s*wal_level\s*=\s*\S+/wal_level = archive/' file
5
  • regarding perl - how to save the changes , I try with i flag but not works
    – yael
    Aug 1, 2017 at 13:56
  • you have mistake its should be - sed -i 's/^ *# *wal_level *= *[^ ]*/wal_level = archive/' file
    – yael
    Aug 1, 2017 at 14:03
  • another thing its remove the second # ( # minimal, archive, or hot_standby )
    – yael
    Aug 1, 2017 at 14:03
  • @yael the -i flag does work, it actually originates in perl, that's where sed took it from. Change the -pe to -i -pe and it will work. And thanks, I changed the minimal to archive. I don't understand what you mean with your last comment. Both solutions here only affect the first #. The # minimal, archive, or hot_standby is not changed.
    – terdon
    Aug 1, 2017 at 14:33
  • its remove also the second "#" that should be as comment , I will check this again
    – yael
    Aug 1, 2017 at 14:45
1
perl -pi.BAK -aF'/(\h*[#=]\h*)/,$_,4' -le '
   $F[4] = "archive";
   $_ = join $,, @F[2..$#F] if /^\h*#\h*wal_level\h*=/;
' /var/lib/pgsql/data/postgresql.conf

The line is split into fields @F and the delimiters are included as well. The parameter 4 in the field split option -F will constrain the number of fields to that. The fields from 3rd onwards are joined together using empty space, which is the default value of the OFS = $,

0
1

You can just change the whole line where wal_level appears, including the # and an arbitrary number of whitespaces:

sed '/^# *wal_level/cwal_level = archive' inputfile
1
  • That removes the comment after the value though.
    – terdon
    Aug 1, 2017 at 11:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .