[英]sed: how to insert line after a block of text?
I have text file: file.conf 我有文本文件:file.conf
### Option: LogFile
# Name of log file.
# If not set, syslog is used.
#
# Mandatory: no
# Default:
# LogFile=
### Option: LogFileSize
# Maximum size of log file in MB.
# 0 - disable automatic log rotation.
#
# Mandatory: no
# Range: 0-1024
# Default:
# LogFileSize=1
And I have tried: 我尝试过:
sed --posix -e '/\bLogFile\b/{:a;n;/^ *$/!ba;i\LogFile=/tmp/log1.log' -e '}' file.conf
And this is working fine, for those option block which is followed by a blank line. 对于那些紧随其后的空白行的选项块,这很好用。
Problem with this is: at the end of last block if no new line is present, then it dose not insert the line. 问题是:在最后一个块的末尾,如果不存在新行,则它不会插入该行。 Here with text LogFileSize.
这里带有文本LogFileSize。
How to handle this. 如何处理。
To add after match line: 要在匹配行之后添加:
$ sed '/#.*LogFile=/a\\LogFile=/tmp/log1.log' 1.cat
will do: 会做:
# LogFile= LogFile=/tmp/log1.log
To add before first empty line: 要在第一个空行之前添加:
$ sed '/^$/i\\LogFile=/tmp/log1.log' 1.cat
will do: 会做:
# LogFileSize=1 LogFile=/tmp/log1.log
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.