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:
- you don't need backslash in front of
#
, nor in front of*
in replacement text. don't harm, not necessary. - you need allow spaces between
core
, number. - you didn't allow blanks , tabs in file.
- you didn't allow trailing white space on line (which might not matter).
- you didn't explicitly enable extended regular expressions, , did use old-style
\(…\)
capturing notation, means+
has no special meaning. - you didn't modify file in place
-i
option (which fine while you're gettingsed
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
Post a Comment