Use sed to change an entry of a configuration file -


i want change following line in /etc/security/limits.conf from

#*               soft    core           0 

to

*               soft    core            unlimited 

to enable core dump.

i tried use following sed command did nothing:

sed -e "s/^\#\*\([ ]+soft[ ]+core\)[0-9][0-9]*$/\*\1 unlimited/" limits.conf 

where did wrong?

mechanics of sed

there variety of issues:

  1. you don't need backslash in front of #, nor in front of * in replacement text. don't harm, not necessary.
  2. you need allow spaces between core , number.
  3. you didn't allow blanks , tabs in file.
  4. you didn't allow trailing white space on line (which might not matter).
  5. you didn't explicitly enable extended regular expressions, , did use old-style \(…\) capturing notation, means + has no special meaning.
  6. you didn't modify file in place -i option (which fine while you're getting sed command correct).

you should able use (gnu sed):

sed -r -e "s/^#\*([[:space:]]+soft[[:space:]]+core)[[:space:]]+[0-9][0-9]*[[:space:]]*$/\*\1 unlimited/" limits.conf 

the [[:space:]] notation recognizes sorts of white space (including newlines, etc); use [[:blank:]] recognizes blanks , tabs if prefer.

you use old-style notation:

sed -e "s/^#\*\([[:space:]]\{1,\}soft[[:space:]]\{1,\}core\)[[:space:]]\{1,\}[0-9][0-9]*[[:space:]]*$/*\1 unlimited/" limits.conf 

if want preserve white space between core , number, can:

sed -e "s/^#\*\([[:space:]]\{1,\}soft[[:space:]]\{1,\}core[[:space:]]\{1,\}\)[0-9][0-9]*[[:space:]]*$/*\1unlimited/" limits.conf 

the close capture moved, , there's no need add space in replacement string.

be cautious when modifying system file

note should careful when sure script ready modify production file. create new file, check it, copy old file date/time-stamped file name, , copy new file on original. alternatively, use suffix -i, date/time again.

sed -i .$(date +'%y%m%d.%h%m%s') -e "s/^#\*\([[:space:]]\{1,\}soft[[:space:]]\{1,\}core[[:space:]]\{1,\}\)[0-9][0-9]*[[:space:]]*$/*\1unlimited/" limits.conf 

the advantage of format (which based on iso 8601:2004) successive files sort date order without special sorting options. can add punctuation date/time format if wish.

either way, if new file gives problems, you've got (a copy of) original can put system running again smoothly reasonably feasible.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

ios - Possible to get UIButton sizeThatFits to work? -