sed:替换数字


9

我正在尝试为我安装/升级Ubuntu时编写一些安装脚本。我在解决问题logrotate.d

/var/log/ufw.log
{
rotate 7
weekly
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
    reload rsyslog >/dev/null 2>&1 || true
endscript
}

我想更改rotate nnnrotate 99。必须使nnn为任意位数(通常为1或2),但不影响该postrotate行。

我已经尝试了这两个,但是我都不需要(并且我用nnn是1,2或3位数字进行了测试)。

sudo sed  's/rotate[0-9]*/rotate 99/' /etc/logrotate.d/ufw
sudo sed  's/^rotate[0-9]/rotate 99/' /etc/logrotate.d/ufw

Answers:



4

rotate[0-9]*替换rotate后跟零个或多个数字,与的结尾匹配postrotaterotate[0-9]仅替换一位数字。此外,您在之后缺少空格rotate。我认为这可以满足您的需求;它还处理缩进的行:

sudo sed 's/^\( *rotate *\)[0-9][0-9]*/\1 99/' /etc/logrotate.d/ufw

如果要在适当位置更改文件,请添加-i选项sed

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.